View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 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.jcr.util.PropertyUtil;
45  import info.magnolia.module.admininterface.TemplatedMVCHandler;
46  import info.magnolia.module.data.commands.ImportCommand;
47  import info.magnolia.module.rssaggregator.generator.CollectStatisticsCommand;
48  import info.magnolia.module.rssaggregator.generator.PlanetDataGenerator;
49  
50  import java.io.IOException;
51  
52  import javax.jcr.Node;
53  import javax.jcr.Session;
54  import javax.servlet.ServletException;
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  /**
62   * RSS Aggregator UI.
63   * @author Jan Haderka
64   */
65  public class RSSAggregatorPage extends TemplatedMVCHandler {
66  
67      /**
68       * Stable serialVersionUID.
69       */
70      public static final long serialVersionUID = 222L;
71  
72      /**
73       * View value for executing the manual backup. (won't render anything)
74       */
75      public static final String VIEW_EXPORT="rssaggregator";
76  
77      private static final String TASKS = "/modules/data/config/importers/rssaggregator/automatedExecution";
78  
79      private static final String PLANET_DATA_PATH = "/modules/scheduler/config/jobs/generatePlanetData";
80  
81      private static final String PLANET_STATISTICS_PATH = "/modules/scheduler/config/jobs/collectPlanetStatistics";
82  
83      private static final String RSSAGGREGATOR_PAGE = "RSSAggregatorPage";
84  
85      private static Logger log = LoggerFactory.getLogger(RSSAggregatorPage.class);
86  
87      protected boolean mgnlKeepVersions;
88  
89      protected int mgnlMaxEntriesPerFile;
90  
91      private boolean manualRefresh;
92  
93      private boolean manualRefreshStatistics;
94  
95      private boolean manualRefreshData;
96  
97      private String mgnlImportRepeat;
98  
99      private String mgnlPlanetImportRepeat;
100 
101     private String mgnlPlanetStatisticsImportRepeat;
102 
103     /**
104      * Getter for <code>mgnlKeepVersions</code>.
105      * @return Returns the mgnlKeepVersions.
106      */
107     public boolean isMgnlKeepVersions() {
108         return this.mgnlKeepVersions;
109     }
110 
111     /**
112      * Setter for <code>mgnlKeepVersions</code>.
113      * @param mgnlKeepVersions The mgnlKeepVersions to set.
114      */
115     public void setMgnlKeepVersions(boolean mgnlKeepVersions) {
116         this.mgnlKeepVersions = mgnlKeepVersions;
117     }
118 
119     /**
120      * Getter for <code>maualBackup</code>.
121      * @return Returns the manualRefresh flag.
122      */
123     public boolean isManualBackup() {
124         return this.manualRefresh;
125     }
126 
127     /**
128      * Setter for <code>manualRefresh</code>.
129      * @param backup The manualRefresh to set.
130      */
131     public void setManualRefresh(boolean backup) {
132         this.manualRefresh = backup;
133     }
134 
135     /**
136      * Getter for manualRefreshStatistics.
137      * @return Returns the manualRefreshStatistics flag.
138      */
139     public boolean isManualRefreshStatistics() {
140         return manualRefreshStatistics;
141     }
142 
143     /**
144      * Setter for manualRefreshStatistics.
145      * @param manualRefreshStatistics The manualRefreshStatistics to set.
146      */
147     public void setManualRefreshStatistics(boolean manualRefreshStatistics) {
148         this.manualRefreshStatistics = manualRefreshStatistics;
149     }
150 
151     public boolean isManualRefreshData() {
152         return manualRefreshData;
153     }
154 
155     public void setManualRefreshData(boolean manualRefreshData) {
156         this.manualRefreshData = manualRefreshData;
157     }
158 
159     /**
160      * @param name
161      * @param request
162      * @param response
163      */
164     public RSSAggregatorPage(String name, HttpServletRequest request, HttpServletResponse response) {
165         super(name, request, response);
166     }
167 
168     /**
169      * @see info.magnolia.cms.servlets.MVCServletHandlerImpl#getCommand()
170      */
171     @Override
172     public String getCommand() {
173         if (this.manualRefresh) {
174             return "manualRefresh";
175         } else {
176             if (this.manualRefreshStatistics) {
177                 return "manualRefreshStatistics";
178             }
179         }
180         return super.getCommand();
181     }
182 
183     /**
184      * Actually fetch RSS feeds.
185      */
186     public String manualRefresh() throws Exception {
187         if (!checkPermissions(request, "data", "/rssaggregator", Permission.WRITE)) {
188             throw new ServletException(new AccessDeniedException(
189                 "Write permission needed for feed update. User not allowed to WRITE to data workspace.")); //$NON-NLS-1$
190         }
191         ImportCommand cmd = new ImportCommand();
192         cmd.setImporter("rssaggregator");
193         cmd.execute(MgnlContext.getInstance());
194         return RSSAGGREGATOR_PAGE;
195     }
196 
197     public String automaticUpdate() throws Exception {
198         HierarchyManager hm = MgnlContext.getHierarchyManager("config");
199         Content execution = hm.getContent(TASKS);
200         execution.getNodeData("enabled").setValue(true);
201         execution.getNodeData("cron").setValue( "0 0/" + getMgnlImportRepeat() + " * * * *");
202         hm.save();
203 
204         return RSSAGGREGATOR_PAGE;
205     }
206 
207     public String manualRefreshData() throws Exception {
208         PlanetDataGenerator pdg = new PlanetDataGenerator();
209         pdg.execute(MgnlContext.getInstance());
210 
211         return RSSAGGREGATOR_PAGE;
212     }
213 
214     public String automaticPlanetUpdate() throws Exception {
215         Session session = MgnlContext.getJCRSession("config");
216         Node planetDataScheduler = session.getNode(PLANET_DATA_PATH);
217         PropertyUtil.setProperty(planetDataScheduler, "active", true);
218         PropertyUtil.setProperty(planetDataScheduler, "cron", "0 0/" + getMgnlPlanetImportRepeat() + " * * * *");
219         session.save();
220 
221         return RSSAGGREGATOR_PAGE;
222     }
223 
224     public String manualRefreshStatistics() throws Exception {
225         CollectStatisticsCommand csc = new CollectStatisticsCommand();
226         csc.execute(MgnlContext.getInstance());
227 
228         return RSSAGGREGATOR_PAGE;
229     }
230 
231     public String automaticPlanetStatisticsUpdate() throws Exception {
232         Session session = MgnlContext.getJCRSession("config");
233         Node planetStatisticsScheduler = session.getNode(PLANET_STATISTICS_PATH);
234         PropertyUtil.setProperty(planetStatisticsScheduler, "active", true);
235         PropertyUtil.setProperty(planetStatisticsScheduler, "cron", "0 0/" + getMgnlPlanetStatisticsImportRepeat() + " * * * *");
236         session.save();
237 
238         return RSSAGGREGATOR_PAGE;
239     }
240 
241     private String getMgnlImportRepeat() {
242         return mgnlImportRepeat;
243     }
244 
245     public String getMgnlPlanetImportRepeat() {
246         return mgnlPlanetImportRepeat;
247     }
248 
249     public String getMgnlPlanetStatisticsImportRepeat() {
250         return mgnlPlanetStatisticsImportRepeat;
251     }
252 
253     public int getAutoUpdate() throws Exception {
254         HierarchyManager hm = MgnlContext.getHierarchyManager("config");
255         Content execution = hm.getContent(TASKS);
256         String[] cron = execution.getNodeData("cron").getString().split(" ");
257         if (cron[1].indexOf('/') < 0) {
258             return 0;
259         } else {
260             return Integer.parseInt(cron[1].substring(cron[1].indexOf('/') + 1));
261         }
262     }
263 
264     /**
265      * Uses access manager to authorize this request.
266      * @param request HttpServletRequest as received by the service method
267      * @return boolean true if read access is granted
268      */
269     protected boolean checkPermissions(HttpServletRequest request, String repository, String basePath,
270         long permissionType) {
271 
272         AccessManager accessManager = MgnlContext.getAccessManager(repository);
273         if (accessManager != null) {
274             if (!accessManager.isGranted(basePath, permissionType)) {
275                 return false;
276             }
277         }
278         return true;
279     }
280 
281     @Override
282     public void renderHtml(String view) throws IOException {
283         // if we are running the backup, everything is already done --> do not render
284         if(VIEW_EXPORT.equals(view)){
285             return;
286         }
287         super.renderHtml(view);
288     }
289 
290     public Messages getMessages() {
291         return MessagesManager.getMessages("info.magnolia.module.rssaggregator.messages");
292     }
293 
294     public void setMgnlImportRepeat(String mgnlImportRepeat) {
295         this.mgnlImportRepeat = mgnlImportRepeat;
296     }
297 
298     public void setMgnlPlanetImportRepeat(String mgnlPlanetImportRepeat) {
299         this.mgnlPlanetImportRepeat = mgnlPlanetImportRepeat;
300     }
301 
302     public void setMgnlPlanetStatisticsImportRepeat(String mgnlPlanetStatisticsImportRepeat) {
303         this.mgnlPlanetStatisticsImportRepeat = mgnlPlanetStatisticsImportRepeat;
304     }
305 }