2
* Copyright 1999-2002,2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
package org.apache.struts.webapp.example2;
21
import java.lang.reflect.InvocationTargetException;
22
import java.util.Locale;
23
import javax.servlet.ServletException;
24
import javax.servlet.http.HttpServletRequest;
25
import javax.servlet.http.HttpServletResponse;
26
import javax.servlet.http.HttpSession;
27
import org.apache.commons.beanutils.PropertyUtils;
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
30
import org.apache.struts.action.Action;
31
import org.apache.struts.action.ActionError;
32
import org.apache.struts.action.ActionErrors;
33
import org.apache.struts.action.ActionForm;
34
import org.apache.struts.action.ActionForward;
35
import org.apache.struts.action.ActionMapping;
36
import org.apache.struts.util.MessageResources;
40
* Implementation of <strong>Action</strong> that validates and creates or
41
* updates the user registration information entered by the user. If a new
42
* registration is created, the user is also implicitly logged on.
44
* @author Craig R. McClanahan
45
* @version $Revision: 1.3 $ $Date: 2004/04/29 03:58:40 $
48
public final class SaveRegistrationAction extends Action {
51
// ----------------------------------------------------- Instance Variables
55
* The <code>Log</code> instance for this application.
58
LogFactory.getLog("org.apache.struts.webapp.Example");
61
// --------------------------------------------------------- Public Methods
65
* Process the specified HTTP request, and create the corresponding HTTP
66
* response (or forward to another web component that will create it).
67
* Return an <code>ActionForward</code> instance describing where and how
68
* control should be forwarded, or <code>null</code> if the response has
69
* already been completed.
71
* @param mapping The ActionMapping used to select this instance
72
* @param actionForm The optional ActionForm bean for this request (if any)
73
* @param request The HTTP request we are processing
74
* @param response The HTTP response we are creating
76
* @exception Exception if the application business logic throws
79
public ActionForward execute(ActionMapping mapping,
81
HttpServletRequest request,
82
HttpServletResponse response)
85
// Extract attributes and parameters we will need
86
Locale locale = getLocale(request);
87
MessageResources messages = getResources(request);
88
HttpSession session = request.getSession();
89
RegistrationForm regform = (RegistrationForm) form;
90
String action = regform.getAction();
94
UserDatabase database = (UserDatabase)
95
servlet.getServletContext().getAttribute(Constants.DATABASE_KEY);
96
if (log.isDebugEnabled()) {
97
log.debug("SaveRegistrationAction: Processing " + action +
101
// Is there a currently logged on user (unless creating)?
102
User user = (User) session.getAttribute(Constants.USER_KEY);
103
if (!"Create".equals(action) && (user == null)) {
104
if (log.isTraceEnabled()) {
105
log.trace(" User is not logged on in session "
108
return (mapping.findForward("logon"));
111
// Was this transaction cancelled?
112
if (isCancelled(request)) {
113
if (log.isTraceEnabled()) {
114
log.trace(" Transaction '" + action +
117
session.removeAttribute(Constants.SUBSCRIPTION_KEY);
118
return (mapping.findForward("success"));
121
// Validate the transactional control token
122
ActionErrors errors = new ActionErrors();
123
if (log.isTraceEnabled()) {
124
log.trace(" Checking transactional control token");
126
if (!isTokenValid(request)) {
127
errors.add(ActionErrors.GLOBAL_ERROR,
128
new ActionError("error.transaction.token"));
132
// Validate the request parameters specified by the user
133
if (log.isTraceEnabled()) {
134
log.trace(" Performing extra validations");
137
value = regform.getUsername();
138
if (("Create".equals(action)) &&
139
(database.findUser(value) != null)) {
140
errors.add("username",
141
new ActionError("error.username.unique",
142
regform.getUsername()));
144
if ("Create".equals(action)) {
145
value = regform.getPassword();
146
if ((value == null) || (value.length() <1)) {
147
errors.add("password",
148
new ActionError("error.password.required"));
150
value = regform.getPassword2();
151
if ((value == null) || (value.length() < 1)) {
152
errors.add("password2",
153
new ActionError("error.password2.required"));
157
// Report any errors we have discovered back to the original form
158
if (!errors.isEmpty()) {
159
saveErrors(request, errors);
161
return (mapping.getInputForward());
164
// Update the user's persistent profile information
166
if ("Create".equals(action)) {
167
user = database.createUser(regform.getUsername());
169
String oldPassword = user.getPassword();
170
PropertyUtils.copyProperties(user, regform);
171
if ((regform.getPassword() == null) ||
172
(regform.getPassword().length() < 1)) {
173
user.setPassword(oldPassword);
175
} catch (InvocationTargetException e) {
176
Throwable t = e.getTargetException();
180
log.error("Registration.populate", t);
181
throw new ServletException("Registration.populate", t);
182
} catch (Throwable t) {
183
log.error("Registration.populate", t);
184
throw new ServletException("Subscription.populate", t);
189
} catch (Exception e) {
190
log.error("Database save", e);
193
// Log the user in if appropriate
194
if ("Create".equals(action)) {
195
session.setAttribute(Constants.USER_KEY, user);
196
if (log.isTraceEnabled()) {
197
log.trace(" User '" + user.getUsername() +
198
"' logged on in session " + session.getId());
202
// Remove the obsolete form bean
203
if (mapping.getAttribute() != null) {
204
if ("request".equals(mapping.getScope()))
205
request.removeAttribute(mapping.getAttribute());
207
session.removeAttribute(mapping.getAttribute());
210
// Forward control to the specified success URI
211
if (log.isTraceEnabled()) {
212
log.trace(" Forwarding to success page");
214
return (mapping.findForward("success"));