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