001: /*
002: * $Id: SaveSubscriptionAction.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.example2;
023:
024: import java.lang.reflect.InvocationTargetException;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import javax.servlet.http.HttpSession;
029: import org.apache.commons.beanutils.PropertyUtils;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.struts.action.Action;
033: import org.apache.struts.action.ActionForm;
034: import org.apache.struts.action.ActionForward;
035: import org.apache.struts.action.ActionMapping;
036: import org.apache.struts.util.MessageResources;
037:
038: /**
039: * Implementation of <strong>Action</strong> that validates and creates or
040: * updates the mail subscription entered by the user.
041: *
042: * @author Craig R. McClanahan
043: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
044: */
045:
046: public final class SaveSubscriptionAction extends Action {
047:
048: // ----------------------------------------------------- Instance Variables
049:
050: /**
051: * The <code>Log</code> instance for this application.
052: */
053: private Log log = LogFactory
054: .getLog("org.apache.struts.webapp.Example");
055:
056: // --------------------------------------------------------- Public Methods
057:
058: /**
059: * Process the specified HTTP request, and create the corresponding HTTP
060: * response (or forward to another web component that will create it).
061: * Return an <code>ActionForward</code> instance describing where and how
062: * control should be forwarded, or <code>null</code> if the response has
063: * already been completed.
064: *
065: * @param mapping The ActionMapping used to select this instance
066: * @param form The optional ActionForm bean for this request (if any)
067: * @param request The HTTP request we are processing
068: * @param response The HTTP response we are creating
069: *
070: * @exception Exception if the application business logic throws
071: * an exception
072: */
073: public ActionForward execute(ActionMapping mapping,
074: ActionForm form, HttpServletRequest request,
075: HttpServletResponse response) throws Exception {
076:
077: // Extract attributes and parameters we will need
078: MessageResources messages = getResources(request);
079: HttpSession session = request.getSession();
080: SubscriptionForm subform = (SubscriptionForm) form;
081: String action = subform.getAction();
082: if (action == null) {
083: action = "?";
084: }
085: if (log.isDebugEnabled()) {
086: log.debug("SaveSubscriptionAction: Processing " + action
087: + " action");
088: }
089:
090: // Is there a currently logged on user?
091: User user = (User) session.getAttribute(Constants.USER_KEY);
092: if (user == null) {
093: if (log.isTraceEnabled()) {
094: log.trace(" User is not logged on in session "
095: + session.getId());
096: }
097: return (mapping.findForward("logon"));
098: }
099:
100: // Was this transaction cancelled?
101: if (isCancelled(request)) {
102: if (log.isTraceEnabled()) {
103: log
104: .trace(" Transaction '" + action
105: + "' was cancelled");
106: }
107: session.removeAttribute(Constants.SUBSCRIPTION_KEY);
108: return (mapping.findForward("success"));
109: }
110:
111: // Is there a related Subscription object?
112: Subscription subscription = (Subscription) session
113: .getAttribute(Constants.SUBSCRIPTION_KEY);
114: if ("Create".equals(action)) {
115: if (log.isTraceEnabled()) {
116: log.trace(" Creating subscription for mail server '"
117: + subform.getHost() + "'");
118: }
119: subscription = user.createSubscription(subform.getHost());
120: }
121: if (subscription == null) {
122: if (log.isTraceEnabled()) {
123: log.trace(" Missing subscription for user '"
124: + user.getUsername() + "'");
125: }
126: response.sendError(HttpServletResponse.SC_BAD_REQUEST,
127: messages.getMessage("error.noSubscription"));
128: return (null);
129: }
130:
131: // Was this transaction a Delete?
132: if (action.equals("Delete")) {
133: if (log.isTraceEnabled()) {
134: log.trace(" Deleting mail server '"
135: + subscription.getHost() + "' for user '"
136: + user.getUsername() + "'");
137: }
138: user.removeSubscription(subscription);
139: session.removeAttribute(Constants.SUBSCRIPTION_KEY);
140: try {
141: UserDatabase database = (UserDatabase) servlet
142: .getServletContext().getAttribute(
143: Constants.DATABASE_KEY);
144: database.save();
145: } catch (Exception e) {
146: log.error("Database save", e);
147: }
148: return (mapping.findForward("success"));
149: }
150:
151: // All required validations were done by the form itself
152:
153: // Update the persistent subscription information
154: if (log.isTraceEnabled()) {
155: log.trace(" Populating database from form bean");
156: }
157: try {
158: PropertyUtils.copyProperties(subscription, subform);
159: } catch (InvocationTargetException e) {
160: Throwable t = e.getTargetException();
161: if (t == null)
162: t = e;
163: log.error("Subscription.populate", t);
164: throw new ServletException("Subscription.populate", t);
165: } catch (Throwable t) {
166: log.error("Subscription.populate", t);
167: throw new ServletException("Subscription.populate", t);
168: }
169:
170: try {
171: UserDatabase database = (UserDatabase) servlet
172: .getServletContext().getAttribute(
173: Constants.DATABASE_KEY);
174: database.save();
175: } catch (Exception e) {
176: log.error("Database save", e);
177: }
178:
179: // Remove the obsolete form bean and current subscription
180: if (mapping.getAttribute() != null) {
181: if ("request".equals(mapping.getScope()))
182: request.removeAttribute(mapping.getAttribute());
183: else
184: session.removeAttribute(mapping.getAttribute());
185: }
186: session.removeAttribute(Constants.SUBSCRIPTION_KEY);
187:
188: // Forward control to the specified success URI
189: if (log.isTraceEnabled()) {
190: log.trace(" Forwarding to success page");
191: }
192: return (mapping.findForward("success"));
193:
194: }
195:
196: }
|