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.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.ActionMessage;
034: import org.apache.struts.action.ActionErrors;
035: import org.apache.struts.action.ActionForm;
036: import org.apache.struts.action.ActionForward;
037: import org.apache.struts.action.ActionMapping;
038:
039: /**
040: * Implementation of <strong>Action</strong> that validates and creates or
041: * updates the user registration information entered by the user. If a new
042: * registration is created, the user is also implicitly logged on.
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 SaveRegistrationAction 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: HttpSession session = request.getSession();
081: RegistrationForm regform = (RegistrationForm) form;
082: String action = regform.getAction();
083: if (action == null) {
084: action = "Create";
085: }
086: UserDatabase database = (UserDatabase) servlet
087: .getServletContext().getAttribute(
088: Constants.DATABASE_KEY);
089: if (log.isDebugEnabled()) {
090: log.debug("SaveRegistrationAction: Processing " + action
091: + " action");
092: }
093:
094: // Is there a currently logged on user (unless creating)?
095: User user = (User) session.getAttribute(Constants.USER_KEY);
096: if (!"Create".equals(action) && (user == null)) {
097: if (log.isTraceEnabled()) {
098: log.trace(" User is not logged on in session "
099: + session.getId());
100: }
101: return (mapping.findForward("logon"));
102: }
103:
104: // Was this transaction cancelled?
105: if (isCancelled(request)) {
106: if (log.isTraceEnabled()) {
107: log
108: .trace(" Transaction '" + action
109: + "' was cancelled");
110: }
111: session.removeAttribute(Constants.SUBSCRIPTION_KEY);
112: return (mapping.findForward("failure"));
113: }
114:
115: // Validate the transactional control token
116: ActionErrors errors = new ActionErrors();
117: if (log.isTraceEnabled()) {
118: log.trace(" Checking transactional control token");
119: }
120: if (!isTokenValid(request)) {
121: errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
122: "error.transaction.token"));
123: }
124: resetToken(request);
125:
126: // Validate the request parameters specified by the user
127: if (log.isTraceEnabled()) {
128: log.trace(" Performing extra validations");
129: }
130: String value = null;
131: value = regform.getUsername();
132: if (("Create".equals(action))
133: && (database.findUser(value) != null)) {
134: errors.add("username", new ActionMessage(
135: "error.username.unique", regform.getUsername()));
136: }
137: if ("Create".equals(action)) {
138: value = regform.getPassword();
139: if ((value == null) || (value.length() < 1)) {
140: errors.add("password", new ActionMessage(
141: "error.password.required"));
142: }
143: value = regform.getPassword2();
144: if ((value == null) || (value.length() < 1)) {
145: errors.add("password2", new ActionMessage(
146: "error.password2.required"));
147: }
148: }
149:
150: // Report any errors we have discovered back to the original form
151: if (!errors.isEmpty()) {
152: saveErrors(request, errors);
153: saveToken(request);
154: return (mapping.getInputForward());
155: }
156:
157: // Update the user's persistent profile information
158: try {
159: if ("Create".equals(action)) {
160: user = database.createUser(regform.getUsername());
161: }
162: String oldPassword = user.getPassword();
163: PropertyUtils.copyProperties(user, regform);
164: if ((regform.getPassword() == null)
165: || (regform.getPassword().length() < 1)) {
166: user.setPassword(oldPassword);
167: }
168: } catch (InvocationTargetException e) {
169: Throwable t = e.getTargetException();
170: if (t == null) {
171: t = e;
172: }
173: log.error("Registration.populate", t);
174: throw new ServletException("Registration.populate", t);
175: } catch (Throwable t) {
176: log.error("Registration.populate", t);
177: throw new ServletException("Subscription.populate", t);
178: }
179:
180: try {
181: database.save();
182: } catch (Exception e) {
183: log.error("Database save", e);
184: }
185:
186: // Log the user in if appropriate
187: if ("Create".equals(action)) {
188: session.setAttribute(Constants.USER_KEY, user);
189: if (log.isTraceEnabled()) {
190: log.trace(" User '" + user.getUsername()
191: + "' logged on in session " + session.getId());
192: }
193: }
194:
195: // Remove the obsolete form bean
196: if (mapping.getAttribute() != null) {
197: if ("request".equals(mapping.getScope()))
198: request.removeAttribute(mapping.getAttribute());
199: else
200: session.removeAttribute(mapping.getAttribute());
201: }
202:
203: // Forward control to the specified success URI
204: if (log.isTraceEnabled()) {
205: log.trace(" Forwarding to success page");
206: }
207: return (mapping.findForward("success"));
208:
209: }
210:
211: }
|