View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.pages;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.i18n.Messages;
39  import info.magnolia.cms.i18n.MessagesManager;
40  import info.magnolia.cms.security.AccessDeniedException;
41  import info.magnolia.cms.security.AccessManager;
42  import info.magnolia.cms.security.Permission;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.module.admininterface.TemplatedMVCHandler;
45  import info.magnolia.module.data.commands.ImportCommand;
46  
47  import java.io.IOException;
48  
49  import javax.servlet.ServletException;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  /**
57   * RSS Aggregator UI.
58   * @author Jan Haderka
59   */
60  public class RSSAggregatorPage extends TemplatedMVCHandler {
61  
62      /**
63       * Stable serialVersionUID.
64       */
65      public static final long serialVersionUID = 222L;
66  
67      /**
68       * View value for executing the manual backup. (won't render anything)
69       */
70      public static final String VIEW_EXPORT="rssaggregator";
71  
72      private static final String TASKS = "/modules/data/config/importers/rssaggregator/automatedExecution";
73  
74      private static final String RSSAGGREGATOR_PAGE = "RSSAggregatorPage";
75  
76      private static Logger log = LoggerFactory.getLogger(RSSAggregatorPage.class);
77  
78      protected boolean mgnlKeepVersions;
79  
80      protected int mgnlMaxEntriesPerFile;
81  
82      private boolean manualRefresh;
83  
84      private String mgnlImportRepeat;
85  
86      /**
87       * Getter for <code>mgnlKeepVersions</code>.
88       * @return Returns the mgnlKeepVersions.
89       */
90      public boolean isMgnlKeepVersions() {
91          return this.mgnlKeepVersions;
92      }
93  
94      /**
95       * Setter for <code>mgnlKeepVersions</code>.
96       * @param mgnlKeepVersions The mgnlKeepVersions to set.
97       */
98      public void setMgnlKeepVersions(boolean mgnlKeepVersions) {
99          this.mgnlKeepVersions = mgnlKeepVersions;
100     }
101 
102     /**
103      * Getter for <code>maualBackup</code>.
104      * @return Returns the manualRefresh flag.
105      */
106     public boolean isManualBackup() {
107         return this.manualRefresh;
108     }
109 
110     /**
111      * Setter for <code>manualRefresh</code>.
112      * @param backup The manualRefresh to set.
113      */
114     public void setManualRefresh(boolean backup) {
115         this.manualRefresh = backup;
116     }
117 
118     /**
119      * @param name
120      * @param request
121      * @param response
122      */
123     public RSSAggregatorPage(String name, HttpServletRequest request, HttpServletResponse response) {
124         super(name, request, response);
125     }
126 
127     /**
128      * @see info.magnolia.cms.servlets.MVCServletHandlerImpl#getCommand()
129      */
130     public String getCommand() {
131         if (this.manualRefresh) {
132             return "manualRefresh";
133         }
134         return super.getCommand();
135     }
136 
137     /**
138      * Actually perform backup.
139      */
140     public String manualRefresh() throws Exception {
141         if (!checkPermissions(request, "data", "/rssaggregator", Permission.WRITE)) {
142             throw new ServletException(new AccessDeniedException(
143                 "Write permission needed for feed update. User not allowed to WRITE to data workspace.")); //$NON-NLS-1$
144         }
145         ImportCommand cmd = new ImportCommand();
146         cmd.setImporter("rssaggregator");
147         cmd.execute(MgnlContext.getInstance());
148         return RSSAGGREGATOR_PAGE;
149     }
150 
151     public String automaticUpdate() throws Exception {
152         HierarchyManager hm = MgnlContext.getHierarchyManager("config");
153         Content execution = hm.getContent(TASKS);
154         execution.getNodeData("enabled").setValue(true);
155         execution.getNodeData("cron").setValue( "0 0/" + getMgnlImportRepeat() + " * * * *");
156         hm.save();
157 
158         return RSSAGGREGATOR_PAGE;
159     }
160 
161     private String getMgnlImportRepeat() {
162         return mgnlImportRepeat;
163     }
164 
165     public int getAutoUpdate() throws Exception {
166         HierarchyManager hm = MgnlContext.getHierarchyManager("config");
167         Content execution = hm.getContent(TASKS);
168         String[] cron = execution.getNodeData("cron").getString().split(" ");
169         if (cron[1].indexOf('/') < 0) {
170             return 0;
171         } else {
172             return Integer.parseInt(cron[1].substring(cron[1].indexOf('/') + 1));
173         }
174     }
175 
176     /**
177      * Uses access manager to authorize this request.
178      * @param request HttpServletRequest as received by the service method
179      * @return boolean true if read access is granted
180      */
181     protected boolean checkPermissions(HttpServletRequest request, String repository, String basePath,
182         long permissionType) {
183 
184         AccessManager accessManager = MgnlContext.getAccessManager(repository);
185         if (accessManager != null) {
186             if (!accessManager.isGranted(basePath, permissionType)) {
187                 return false;
188             }
189         }
190         return true;
191     }
192 
193     public void renderHtml(String view) throws IOException {
194         // if we are running the backup, everything is already done --> do not render
195         if(VIEW_EXPORT.equals(view)){
196             return;
197         }
198         super.renderHtml(view);
199     }
200 
201     public Messages getMessages() {
202         return MessagesManager.getMessages("info.magnolia.module.rssaggregator.messages");
203     }
204 
205     public void setMgnlImportRepeat(String mgnlImportRepeat) {
206         this.mgnlImportRepeat = mgnlImportRepeat;
207     }
208 
209 }