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     @Override
131     public String getCommand() {
132         if (this.manualRefresh) {
133             return "manualRefresh";
134         }
135         return super.getCommand();
136     }
137 
138     /**
139      * Actually perform backup.
140      */
141     public String manualRefresh() throws Exception {
142         if (!checkPermissions(request, "data", "/rssaggregator", Permission.WRITE)) {
143             throw new ServletException(new AccessDeniedException(
144                 "Write permission needed for feed update. User not allowed to WRITE to data workspace.")); //$NON-NLS-1$
145         }
146         ImportCommand cmd = new ImportCommand();
147         cmd.setImporter("rssaggregator");
148         cmd.execute(MgnlContext.getInstance());
149         return RSSAGGREGATOR_PAGE;
150     }
151 
152     public String automaticUpdate() throws Exception {
153         HierarchyManager hm = MgnlContext.getHierarchyManager("config");
154         Content execution = hm.getContent(TASKS);
155         execution.getNodeData("enabled").setValue(true);
156         execution.getNodeData("cron").setValue( "0 0/" + getMgnlImportRepeat() + " * * * *");
157         hm.save();
158 
159         return RSSAGGREGATOR_PAGE;
160     }
161 
162     private String getMgnlImportRepeat() {
163         return mgnlImportRepeat;
164     }
165 
166     public int getAutoUpdate() throws Exception {
167         HierarchyManager hm = MgnlContext.getHierarchyManager("config");
168         Content execution = hm.getContent(TASKS);
169         String[] cron = execution.getNodeData("cron").getString().split(" ");
170         if (cron[1].indexOf('/') < 0) {
171             return 0;
172         } else {
173             return Integer.parseInt(cron[1].substring(cron[1].indexOf('/') + 1));
174         }
175     }
176 
177     /**
178      * Uses access manager to authorize this request.
179      * @param request HttpServletRequest as received by the service method
180      * @return boolean true if read access is granted
181      */
182     protected boolean checkPermissions(HttpServletRequest request, String repository, String basePath,
183         long permissionType) {
184 
185         AccessManager accessManager = MgnlContext.getAccessManager(repository);
186         if (accessManager != null) {
187             if (!accessManager.isGranted(basePath, permissionType)) {
188                 return false;
189             }
190         }
191         return true;
192     }
193 
194     @Override
195     public void renderHtml(String view) throws IOException {
196         // if we are running the backup, everything is already done --> do not render
197         if(VIEW_EXPORT.equals(view)){
198             return;
199         }
200         super.renderHtml(view);
201     }
202 
203     public Messages getMessages() {
204         return MessagesManager.getMessages("info.magnolia.module.rssaggregator.messages");
205     }
206 
207     public void setMgnlImportRepeat(String mgnlImportRepeat) {
208         this.mgnlImportRepeat = mgnlImportRepeat;
209     }
210 
211 }