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 @Override
131 public String getCommand() {
132 if (this.manualRefresh) {
133 return "manualRefresh";
134 }
135 return super.getCommand();
136 }
137
138
139
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."));
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
179
180
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
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 }