1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.struts.apps.mailreader.actions;
18
19 import java.lang.reflect.InvocationTargetException;
20
21 import jakarta.servlet.ServletException;
22 import jakarta.servlet.http.HttpServletRequest;
23 import jakarta.servlet.http.HttpServletResponse;
24 import jakarta.servlet.http.HttpSession;
25
26 import org.apache.commons.beanutils.PropertyUtils;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.apps.mailreader.Constants;
31 import org.apache.struts.apps.mailreader.dao.Subscription;
32 import org.apache.struts.apps.mailreader.dao.User;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42 public final class SubscriptionAction extends BaseAction {
43 private static final long serialVersionUID = 481958664313412332L;
44
45
46
47
48 private final static Logger LOG =
49 LoggerFactory.getLogger(SubscriptionAction.class);
50
51
52
53
54
55
56
57
58 public final static String AUTO_CONNECT = "autoConnect";
59
60
61
62
63
64
65 public final static String HOST = "host";
66
67
68
69
70
71
72 public final static String TYPE = "type";
73
74
75
76 final String LOG_SUBSCRIPTION_POPULATE = "SubscriptionForm.populate";
77
78
79
80
81
82
83
84
85
86
87
88 private Subscription doFindSubscription(User user, String host) {
89
90 Subscription subscription;
91
92 try {
93 subscription = user.findSubscription(host);
94 }
95 catch (NullPointerException e) {
96 subscription = null;
97 }
98
99 if (subscription == null) {
100 LOG.trace(" No subscription for user {} and host {}",
101 user.getUsername(), host);
102 }
103
104 return subscription;
105 }
106
107
108
109
110
111
112
113
114
115
116 private void doPopulate(Subscription subscription, ActionForm form)
117 throws ServletException {
118
119 LOG.trace("{}{}", Constants.LOG_POPULATE_SUBSCRIPTION, subscription);
120
121 try {
122 PropertyUtils.copyProperties(subscription, form);
123 } catch (InvocationTargetException e) {
124 Throwable t = e.getTargetException();
125 if (t == null) {
126 t = e;
127 }
128 LOG.error(LOG_SUBSCRIPTION_POPULATE, t);
129 throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
130 } catch (Throwable t) {
131 LOG.error(LOG_SUBSCRIPTION_POPULATE, t);
132 throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145 private void doPopulate(ActionForm form, Subscription subscription)
146 throws ServletException {
147
148 final String title = Constants.EDIT;
149
150 LOG.trace("{}{}", Constants.LOG_POPULATE_FORM, subscription.getHost());
151
152 try {
153 PropertyUtils.copyProperties(form, subscription);
154 doSet(form, TASK, title);
155 } catch (InvocationTargetException e) {
156 Throwable t = e.getTargetException();
157 if (t == null) {
158 t = e;
159 }
160 LOG.error(LOG_SUBSCRIPTION_POPULATE, t);
161 throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
162 } catch (Throwable t) {
163 LOG.error(LOG_SUBSCRIPTION_POPULATE, t);
164 throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 private ActionForward doRemoveSubscription(
182 ActionMapping mapping,
183 HttpSession session,
184 User user,
185 Subscription subscription)
186 throws ServletException {
187
188 final String method = Constants.DELETE;
189 doLogProcess(mapping, method);
190
191 LOG.trace(" Deleting subscription to mail server '{}' for user '{}'",
192 subscription.getHost(), user.getUsername());
193
194 boolean missingAttributes = ((user == null) || (subscription == null));
195 if (missingAttributes) {
196 return doFindLogon(mapping);
197 }
198
199 user.removeSubscription(subscription);
200 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
201 doSaveUser(user);
202
203 return doFindSuccess(mapping);
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public ActionForward Delete(
222 ActionMapping mapping,
223 ActionForm form,
224 HttpServletRequest request,
225 HttpServletResponse response)
226 throws Exception {
227
228 final String method = Constants.DELETE;
229 doLogProcess(mapping, method);
230
231 ActionForward result = Edit(mapping, form, request, response);
232
233 doSet(form, TASK, method);
234 return result;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public ActionForward Edit(
255 ActionMapping mapping,
256 ActionForm form,
257 HttpServletRequest request,
258 HttpServletResponse response)
259 throws Exception {
260
261 final String method = Constants.EDIT;
262 doLogProcess(mapping, method);
263
264 HttpSession session = request.getSession();
265 User user = doGetUser(session);
266 if (user == null) {
267 return doFindLogon(mapping);
268 }
269
270
271 Subscription subscription;
272 String host = doGet(form, HOST);
273 boolean updating = (host != null);
274 if (updating) {
275 subscription = doFindSubscription(user, host);
276 if (subscription == null) {
277 return doFindFailure(mapping);
278 }
279 session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription);
280 doPopulate(form, subscription);
281 doSet(form, TASK, method);
282 }
283
284 return doFindSuccess(mapping);
285 }
286
287
288
289
290
291
292
293
294
295
296
297
298
299 public ActionForward Save(
300 ActionMapping mapping,
301 ActionForm form,
302 HttpServletRequest request,
303 HttpServletResponse response)
304 throws Exception {
305
306 final String method = Constants.SAVE;
307 doLogProcess(mapping, method);
308
309 User user = doGetUser(request);
310 if (user == null) {
311 return doFindLogon(mapping);
312 }
313
314 HttpSession session = request.getSession();
315 if (isCancelled(request)) {
316 doCancel(session, method, Constants.SUBSCRIPTION_KEY);
317 return doFindSuccess(mapping);
318 }
319
320 String action = doGet(form, TASK);
321 Subscription subscription = doGetSubscription(request);
322 boolean isDelete = action.equals(Constants.DELETE);
323 if (isDelete) {
324 return doRemoveSubscription(mapping, session, user, subscription);
325 }
326
327 if (subscription == null) {
328 subscription = user.createSubscription(doGet(form, HOST));
329 session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription);
330 }
331
332 doPopulate(subscription, form);
333 doSaveUser(user);
334 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
335
336 return doFindSuccess(mapping);
337 }
338 }