001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.struts.apps.mailreader.actions;
018:
019: import org.apache.commons.beanutils.PropertyUtils;
020: import org.apache.struts.action.ActionForm;
021: import org.apache.struts.action.ActionForward;
022: import org.apache.struts.action.ActionMapping;
023: import org.apache.struts.apps.mailreader.Constants;
024: import org.apache.struts.apps.mailreader.dao.Subscription;
025: import org.apache.struts.apps.mailreader.dao.User;
026:
027: import javax.servlet.ServletException;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import javax.servlet.http.HttpSession;
031: import java.lang.reflect.InvocationTargetException;
032:
033: /**
034: * <p>
035: * Provide an Edit method for retrieving an existing subscription,
036: * and a Save method for updating or inserting a subscription.
037: * </p>
038: */
039: public final class SubscriptionAction extends BaseAction {
040:
041: // --- Public Constants --
042:
043: /**
044: * <p>
045: * Name of autoConnect field ["autoConnect"].
046: * </p>
047: */
048: public final static String AUTO_CONNECT = "autoConnect";
049:
050: /**
051: * <p>
052: * Name of host field ["host"].
053: * </p>
054: */
055: public final static String HOST = "host";
056:
057: /**
058: * <p>
059: * Name of type field ["type"].
060: * </p>
061: */
062: public final static String TYPE = "type";
063:
064: // ---- Private Methods ----
065:
066: final String LOG_SUBSCRIPTION_POPULATE = "SubscriptionForm.populate";
067:
068: /**
069: * <p>
070: * Obtain subscription matching host for the given User,
071: * or return null if not found.
072: * </p>
073: *
074: * @param user Our User object
075: * @param host The name of the mail server host
076: * @return The matching Subscription or null
077: */
078: private Subscription doFindSubscription(User user, String host) {
079:
080: Subscription subscription;
081:
082: try {
083: subscription = user.findSubscription(host);
084: } catch (NullPointerException e) {
085: subscription = null;
086: }
087:
088: if ((subscription == null) && (log.isTraceEnabled())) {
089: log.trace(" No subscription for user " + user.getUsername()
090: + " and host " + host);
091: }
092:
093: return subscription;
094: }
095:
096: /**
097: * <p>
098: * Helper method to populate the Subscription object from the input form.
099: * </p>
100: *
101: * @param subscription User object to populate
102: * @param form Form with incoming values
103: * @throws ServletException On any error
104: */
105: private void doPopulate(Subscription subscription, ActionForm form)
106: throws ServletException {
107:
108: if (log.isTraceEnabled()) {
109: log.trace(Constants.LOG_POPULATE_SUBSCRIPTION
110: + subscription);
111: }
112:
113: try {
114: PropertyUtils.copyProperties(subscription, form);
115: } catch (InvocationTargetException e) {
116: Throwable t = e.getTargetException();
117: if (t == null) {
118: t = e;
119: }
120: log.error(LOG_SUBSCRIPTION_POPULATE, t);
121: throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
122: } catch (Throwable t) {
123: log.error(LOG_SUBSCRIPTION_POPULATE, t);
124: throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
125: }
126: }
127:
128: /**
129: * <p>
130: * Helper method to populate the input form from the Subscription object.
131: * </p>
132: *
133: * @param subscription User object to populate
134: * @param form Form with incoming values
135: * @throws ServletException On any error
136: */
137: private void doPopulate(ActionForm form, Subscription subscription)
138: throws ServletException {
139:
140: final String title = Constants.EDIT;
141:
142: if (log.isTraceEnabled()) {
143: log.trace(Constants.LOG_POPULATE_FORM
144: + subscription.getHost());
145: }
146:
147: try {
148: PropertyUtils.copyProperties(form, subscription);
149: doSet(form, TASK, title);
150: } catch (InvocationTargetException e) {
151: Throwable t = e.getTargetException();
152: if (t == null) {
153: t = e;
154: }
155: log.error(LOG_SUBSCRIPTION_POPULATE, t);
156: throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
157: } catch (Throwable t) {
158: log.error(LOG_SUBSCRIPTION_POPULATE, t);
159: throw new ServletException(LOG_SUBSCRIPTION_POPULATE, t);
160: }
161: }
162:
163: /**
164: * <p>
165: * Remove the given subscription for this user.
166: * </p>
167: *
168: * @param mapping Our ActionMapping
169: * @param session Our HttpSession
170: * @param user Our User
171: * @param subscription Subscription to delete
172: * @return "Success" if delete is nominal, "Logon" if attributes are
173: * missing
174: * @throws ServletException if updates fails
175: */
176: private ActionForward doRemoveSubscription(ActionMapping mapping,
177: HttpSession session, User user, Subscription subscription)
178: throws ServletException {
179:
180: final String method = Constants.DELETE;
181: doLogProcess(mapping, method);
182:
183: if (log.isTraceEnabled()) {
184: log.trace(" Deleting subscription to mail server '"
185: + subscription.getHost() + "' for user '"
186: + user.getUsername() + "'");
187: }
188:
189: boolean missingAttributes = ((user == null) || (subscription == null));
190: if (missingAttributes) {
191: return doFindLogon(mapping);
192: }
193:
194: user.removeSubscription(subscription);
195: session.removeAttribute(Constants.SUBSCRIPTION_KEY);
196: doSaveUser(user);
197:
198: return doFindSuccess(mapping);
199: }
200:
201: // ----- Public Methods ----
202:
203: /**
204: * <p>
205: * Prepare for a Delete operation by populating the form
206: * and seting the action to Delete.
207: * </p>
208: *
209: * @param mapping Our ActionMapping
210: * @param form Our ActionForm
211: * @param request Our HttpServletRequest
212: * @param response Our HttpServletResponse
213: * @return The "Success" result for this mapping
214: * @throws Exception on any error
215: */
216: public ActionForward Delete(ActionMapping mapping, ActionForm form,
217: HttpServletRequest request, HttpServletResponse response)
218: throws Exception {
219:
220: final String method = Constants.DELETE;
221: doLogProcess(mapping, method);
222:
223: ActionForward result = Edit(mapping, form, request, response);
224:
225: doSet(form, TASK, method);
226: return result;
227: }
228:
229: /**
230: * <p>
231: * Retrieve the Subscription object to edit
232: * or null if the Subscription does not exist.
233: * </p><p>
234: * The Subscription object is bound to the User,
235: * and so if the User is not logged in,
236: * control is forwarded to the Logon result.
237: * </p>
238: *
239: * @param mapping Our ActionMapping
240: * @param form Our ActionForm
241: * @param request Our HttpServletRequest
242: * @param response Our HttpServletResponse
243: * @return The "Success" result for this mapping
244: * @throws Exception on any error
245: */
246: public ActionForward Edit(ActionMapping mapping, ActionForm form,
247: HttpServletRequest request, HttpServletResponse response)
248: throws Exception {
249:
250: final String method = Constants.EDIT;
251: doLogProcess(mapping, method);
252:
253: HttpSession session = request.getSession();
254: User user = doGetUser(session);
255: if (user == null) {
256: return doFindLogon(mapping);
257: }
258:
259: // Retrieve the subscription, if there is one
260: Subscription subscription;
261: String host = doGet(form, HOST);
262: boolean updating = (host != null);
263: if (updating) {
264: subscription = doFindSubscription(user, host);
265: if (subscription == null) {
266: return doFindFailure(mapping);
267: }
268: session.setAttribute(Constants.SUBSCRIPTION_KEY,
269: subscription);
270: doPopulate(form, subscription);
271: doSet(form, TASK, method);
272: }
273:
274: return doFindSuccess(mapping);
275: }
276:
277: /**
278: * <p>
279: * Insert or update a Subscription object to the persistent store.
280: * </p>
281: *
282: * @param mapping Our ActionMapping
283: * @param form Our ActionForm
284: * @param request Our HttpServletRequest
285: * @param response Our HttpServletResponse
286: * @return The "Success" result for this mapping
287: * @throws Exception on any error
288: */
289: public ActionForward Save(ActionMapping mapping, ActionForm form,
290: HttpServletRequest request, 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,
312: subscription);
313: }
314:
315: if (subscription == null) {
316: subscription = user.createSubscription(doGet(form, HOST));
317: session.setAttribute(Constants.SUBSCRIPTION_KEY,
318: subscription);
319: }
320:
321: doPopulate(subscription, form);
322: doSaveUser(user);
323: session.removeAttribute(Constants.SUBSCRIPTION_KEY);
324:
325: return doFindSuccess(mapping);
326: }
327:
328: }
|