001: /*
002: * $Id: SaveRegistrationAction.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.ActionMessage;
036: import org.apache.struts.action.ActionMessages;
037: import org.apache.struts.action.ActionForm;
038: import org.apache.struts.action.ActionForward;
039: import org.apache.struts.action.ActionMapping;
040:
041: /**
042: * Implementation of <strong>Action</strong> that validates and creates or
043: * updates the user registration information entered by the user. If a new
044: * registration is created, the user is also implicitly logged on.
045: *
046: * @author Craig R. McClanahan
047: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
048: */
049:
050: public final class SaveRegistrationAction extends Action {
051:
052: // ----------------------------------------------------- Instance Variables
053:
054: /**
055: * The <code>Log</code> instance for this application.
056: */
057: private Log log = LogFactory
058: .getLog("org.apache.struts.webapp.Example");
059:
060: // --------------------------------------------------------- Public Methods
061:
062: /**
063: * Process the specified HTTP request, and create the corresponding HTTP
064: * response (or forward to another web component that will create it).
065: * Return an <code>ActionForward</code> instance describing where and how
066: * control should be forwarded, or <code>null</code> if the response has
067: * already been completed.
068: *
069: * @param mapping The ActionMapping used to select this instance
070: * @param form The optional ActionForm bean for this request (if any)
071: * @param request The HTTP request we are processing
072: * @param response The HTTP response we are creating
073: *
074: * @exception Exception if the application business logic throws
075: * an exception
076: */
077: public ActionForward execute(ActionMapping mapping,
078: ActionForm form, HttpServletRequest request,
079: HttpServletResponse response) throws Exception {
080:
081: // Extract attributes and parameters we will need
082: HttpSession session = request.getSession();
083: RegistrationForm regform = (RegistrationForm) form;
084: String action = regform.getAction();
085: if (action == null) {
086: action = "Create";
087: }
088: UserDatabase database = (UserDatabase) servlet
089: .getServletContext().getAttribute(
090: Constants.DATABASE_KEY);
091: if (log.isDebugEnabled()) {
092: log.debug("SaveRegistrationAction: Processing " + action
093: + " action");
094: }
095:
096: // Is there a currently logged on user (unless creating)?
097: User user = (User) session.getAttribute(Constants.USER_KEY);
098: if (!"Create".equals(action) && (user == null)) {
099: if (log.isTraceEnabled()) {
100: log.trace(" User is not logged on in session "
101: + session.getId());
102: }
103: return (mapping.findForward("logon"));
104: }
105:
106: // Was this transaction cancelled?
107: if (isCancelled(request)) {
108: if (log.isTraceEnabled()) {
109: log
110: .trace(" Transaction '" + action
111: + "' was cancelled");
112: }
113: session.removeAttribute(Constants.SUBSCRIPTION_KEY);
114: return (mapping.findForward("failure"));
115: }
116:
117: // Validate the transactional control token
118: ActionMessages errors = new ActionMessages();
119: if (log.isTraceEnabled()) {
120: log.trace(" Checking transactional control token");
121: }
122: if (!isTokenValid(request)) {
123: errors.add(ActionMessages.GLOBAL_MESSAGE,
124: new ActionMessage("error.transaction.token"));
125: }
126: resetToken(request);
127:
128: // Validate the request parameters specified by the user
129: if (log.isTraceEnabled()) {
130: log.trace(" Performing extra validations");
131: }
132: String value = null;
133: value = regform.getUsername();
134: if (("Create".equals(action))
135: && (database.findUser(value) != null)) {
136: errors.add("username", new ActionMessage(
137: "error.username.unique", regform.getUsername()));
138: }
139: if ("Create".equals(action)) {
140: value = regform.getPassword();
141: if ((value == null) || (value.length() < 1)) {
142: errors.add("password", new ActionMessage(
143: "error.password.required"));
144: }
145: value = regform.getPassword2();
146: if ((value == null) || (value.length() < 1)) {
147: errors.add("password2", new ActionMessage(
148: "error.password2.required"));
149: }
150: }
151:
152: // Report any errors we have discovered back to the original form
153: if (!errors.isEmpty()) {
154: saveErrors(request, errors);
155: saveToken(request);
156: return (mapping.getInputForward());
157: }
158:
159: // Update the user's persistent profile information
160: try {
161: if ("Create".equals(action)) {
162: user = database.createUser(regform.getUsername());
163: }
164: String oldPassword = user.getPassword();
165: PropertyUtils.copyProperties(user, regform);
166: if ((regform.getPassword() == null)
167: || (regform.getPassword().length() < 1)) {
168: user.setPassword(oldPassword);
169: }
170: } catch (InvocationTargetException e) {
171: Throwable t = e.getTargetException();
172: if (t == null) {
173: t = e;
174: }
175: log.error("Registration.populate", t);
176: throw new ServletException("Registration.populate", t);
177: } catch (Throwable t) {
178: log.error("Registration.populate", t);
179: throw new ServletException("Subscription.populate", t);
180: }
181:
182: try {
183: database.save();
184: } catch (Exception e) {
185: log.error("Database save", e);
186: }
187:
188: // Log the user in if appropriate
189: if ("Create".equals(action)) {
190: session.setAttribute(Constants.USER_KEY, user);
191: if (log.isTraceEnabled()) {
192: log.trace(" User '" + user.getUsername()
193: + "' logged on in session " + session.getId());
194: }
195: }
196:
197: // Remove the obsolete form bean
198: if (mapping.getAttribute() != null) {
199: if ("request".equals(mapping.getScope()))
200: request.removeAttribute(mapping.getAttribute());
201: else
202: session.removeAttribute(mapping.getAttribute());
203: }
204:
205: // Forward control to the specified success URI
206: if (log.isTraceEnabled()) {
207: log.trace(" Forwarding to success page");
208: }
209: return (mapping.findForward("success"));
210:
211: }
212:
213: }
|