KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example > SaveRegistrationAction


1 /*
2  * $Id: SaveRegistrationAction.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.webapp.example;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.apache.commons.beanutils.PropertyUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.action.Action;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.ActionMessage;
36 import org.apache.struts.action.ActionMessages;
37
38 /**
39  * Implementation of <strong>Action</strong> that validates and creates or
40  * updates the user registration information entered by the user. If a new
41  * registration is created, the user is also implicitly logged on.
42  *
43  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
44  */

45
46 public final class SaveRegistrationAction extends Action {
47
48     // ----------------------------------------------------- Instance Variables
49

50     /**
51      * The <code>Log</code> instance for this application.
52      */

53     private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");
54
55     // --------------------------------------------------------- Public Methods
56

57         // See superclass for Javadoc
58
public ActionForward execute(
59         ActionMapping mapping,
60         ActionForm form,
61         HttpServletRequest JavaDoc request,
62         HttpServletResponse JavaDoc response)
63         throws Exception JavaDoc {
64
65         // Extract attributes and parameters we will need
66
HttpSession JavaDoc session = request.getSession();
67         RegistrationForm regform = (RegistrationForm) form;
68         String JavaDoc action = regform.getAction();
69         if (action == null) {
70             action = "Create";
71         }
72         
73         UserDatabase database =
74             (UserDatabase) servlet.getServletContext().getAttribute(
75                 Constants.DATABASE_KEY);
76                 
77         if (log.isDebugEnabled()) {
78             log.debug("SaveRegistrationAction: Processing " + action + " action");
79         }
80
81         // Is there a currently logged on user (unless creating)?
82
User user = (User) session.getAttribute(Constants.USER_KEY);
83         if (!"Create".equals(action) && (user == null)) {
84             if (log.isTraceEnabled()) {
85                 log.trace(" User is not logged on in session " + session.getId());
86             }
87             return (mapping.findForward("logon"));
88         }
89
90         // Was this transaction cancelled?
91
if (isCancelled(request)) {
92             if (log.isTraceEnabled()) {
93                 log.trace(" Transaction '" + action + "' was cancelled");
94             }
95             session.removeAttribute(Constants.SUBSCRIPTION_KEY);
96             return (mapping.findForward("success"));
97         }
98
99         // Validate the transactional control token
100
ActionMessages errors = new ActionMessages();
101         if (log.isTraceEnabled()) {
102             log.trace(" Checking transactional control token");
103         }
104         
105         if (!isTokenValid(request)) {
106             errors.add(
107                 ActionMessages.GLOBAL_MESSAGE,
108                 new ActionMessage("error.transaction.token"));
109         }
110         
111         resetToken(request);
112
113         // Validate the request parameters specified by the user
114
if (log.isTraceEnabled()) {
115             log.trace(" Performing extra validations");
116         }
117         
118         String JavaDoc value = null;
119         value = regform.getUsername();
120         if (("Create".equals(action)) && (database.findUser(value) != null)) {
121             errors.add(
122                 "username",
123                 new ActionMessage("error.username.unique", regform.getUsername()));
124         }
125         
126         if ("Create".equals(action)) {
127             value = regform.getPassword();
128             if ((value == null) || (value.length() < 1)) {
129                 errors.add("password", new ActionMessage("error.password.required"));
130             }
131             
132             value = regform.getPassword2();
133             
134             if ((value == null) || (value.length() < 1)) {
135                 errors.add(
136                     "password2",
137                     new ActionMessage("error.password2.required"));
138             }
139         }
140
141         // Report any errors we have discovered back to the original form
142
if (!errors.isEmpty()) {
143             this.saveErrors(request, errors);
144             this.saveToken(request);
145             return (mapping.getInputForward());
146         }
147
148         // Update the user's persistent profile information
149
try {
150             if ("Create".equals(action)) {
151                 user = database.createUser(regform.getUsername());
152             }
153             
154             String JavaDoc oldPassword = user.getPassword();
155             PropertyUtils.copyProperties(user, regform);
156             if ((regform.getPassword() == null)
157                 || (regform.getPassword().length() < 1)) {
158                     
159                 user.setPassword(oldPassword);
160             }
161             
162         } catch (InvocationTargetException JavaDoc e) {
163             Throwable JavaDoc t = e.getTargetException();
164             if (t == null) {
165                 t = e;
166             }
167             
168             log.error("Registration.populate", t);
169             throw new ServletException JavaDoc("Registration.populate", t);
170             
171         } catch (Throwable JavaDoc t) {
172             log.error("Registration.populate", t);
173             throw new ServletException JavaDoc("Subscription.populate", t);
174         }
175
176         try {
177             database.save();
178         } catch (Exception JavaDoc e) {
179             log.error("Database save", e);
180         }
181
182         // Log the user in if appropriate
183
if ("Create".equals(action)) {
184             session.setAttribute(Constants.USER_KEY, user);
185             if (log.isTraceEnabled()) {
186                 log.trace(
187                     " User '"
188                         + user.getUsername()
189                         + "' logged on in session "
190                         + session.getId());
191             }
192         }
193
194         // Remove the obsolete form bean
195
if (mapping.getAttribute() != null) {
196             if ("request".equals(mapping.getScope()))
197                 request.removeAttribute(mapping.getAttribute());
198             else
199                 session.removeAttribute(mapping.getAttribute());
200         }
201
202         // Forward control to the specified success URI
203
if (log.isTraceEnabled()) {
204             log.trace(" Forwarding to success page");
205         }
206         
207         return (mapping.findForward("success"));
208
209     }
210
211 }
212
Popular Tags