1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
60 public class RSSAggregatorPage extends TemplatedMVCHandler {
61
62
63
64
65 public static final long serialVersionUID = 222L;
66
67
68
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
88
89
90 public boolean isMgnlKeepVersions() {
91 return this.mgnlKeepVersions;
92 }
93
94
95
96
97
98 public void setMgnlKeepVersions(boolean mgnlKeepVersions) {
99 this.mgnlKeepVersions = mgnlKeepVersions;
100 }
101
102
103
104
105
106 public boolean isManualBackup() {
107 return this.manualRefresh;
108 }
109
110
111
112
113
114 public void setManualRefresh(boolean backup) {
115 this.manualRefresh = backup;
116 }
117
118
119
120
121
122
123 public RSSAggregatorPage(String name, HttpServletRequest request, HttpServletResponse response) {
124 super(name, request, response);
125 }
126
127
128
129
130 public String getCommand() {
131 if (this.manualRefresh) {
132 return "manualRefresh";
133 }
134 return super.getCommand();
135 }
136
137
138
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."));
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
178
179
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
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 }