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