1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts.webapp.example2;
24
25
26 import java.lang.reflect.InvocationTargetException;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
31 import org.apache.commons.beanutils.PropertyUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.struts.action.Action;
35 import org.apache.struts.action.ActionMessage;
36 import org.apache.struts.action.ActionErrors;
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionForward;
39 import org.apache.struts.action.ActionMapping;
40
41
42
43
44
45
46
47
48
49
50
51 public final class SaveRegistrationAction extends Action {
52
53
54
55
56
57
58
59
60 private Log log =
61 LogFactory.getLog("org.apache.struts.webapp.Example");
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public ActionForward execute(ActionMapping mapping,
83 ActionForm form,
84 HttpServletRequest request,
85 HttpServletResponse response)
86 throws Exception {
87
88
89 HttpSession session = request.getSession();
90 RegistrationForm regform = (RegistrationForm) form;
91 String action = regform.getAction();
92 if (action == null) {
93 action = "Create";
94 }
95 UserDatabase database = (UserDatabase)
96 servlet.getServletContext().getAttribute(Constants.DATABASE_KEY);
97 if (log.isDebugEnabled()) {
98 log.debug("SaveRegistrationAction: Processing " + action +
99 " action");
100 }
101
102
103 User user = (User) session.getAttribute(Constants.USER_KEY);
104 if (!"Create".equals(action) && (user == null)) {
105 if (log.isTraceEnabled()) {
106 log.trace(" User is not logged on in session "
107 + session.getId());
108 }
109 return (mapping.findForward("logon"));
110 }
111
112
113 if (isCancelled(request)) {
114 if (log.isTraceEnabled()) {
115 log.trace(" Transaction '" + action +
116 "' was cancelled");
117 }
118 session.removeAttribute(Constants.SUBSCRIPTION_KEY);
119 return (mapping.findForward("failure"));
120 }
121
122
123 ActionErrors errors = new ActionErrors();
124 if (log.isTraceEnabled()) {
125 log.trace(" Checking transactional control token");
126 }
127 if (!isTokenValid(request)) {
128 errors.add(ActionErrors.GLOBAL_MESSAGE,
129 new ActionMessage("error.transaction.token"));
130 }
131 resetToken(request);
132
133
134 if (log.isTraceEnabled()) {
135 log.trace(" Performing extra validations");
136 }
137 String value = null;
138 value = regform.getUsername();
139 if (("Create".equals(action)) &&
140 (database.findUser(value) != null)) {
141 errors.add("username",
142 new ActionMessage("error.username.unique",
143 regform.getUsername()));
144 }
145 if ("Create".equals(action)) {
146 value = regform.getPassword();
147 if ((value == null) || (value.length() <1)) {
148 errors.add("password",
149 new ActionMessage("error.password.required"));
150 }
151 value = regform.getPassword2();
152 if ((value == null) || (value.length() < 1)) {
153 errors.add("password2",
154 new ActionMessage("error.password2.required"));
155 }
156 }
157
158
159 if (!errors.isEmpty()) {
160 saveErrors(request, errors);
161 saveToken(request);
162 return (mapping.getInputForward());
163 }
164
165
166 try {
167 if ("Create".equals(action)) {
168 user = database.createUser(regform.getUsername());
169 }
170 String oldPassword = user.getPassword();
171 PropertyUtils.copyProperties(user, regform);
172 if ((regform.getPassword() == null) ||
173 (regform.getPassword().length() < 1)) {
174 user.setPassword(oldPassword);
175 }
176 } catch (InvocationTargetException e) {
177 Throwable t = e.getTargetException();
178 if (t == null) {
179 t = e;
180 }
181 log.error("Registration.populate", t);
182 throw new ServletException("Registration.populate", t);
183 } catch (Throwable t) {
184 log.error("Registration.populate", t);
185 throw new ServletException("Subscription.populate", t);
186 }
187
188 try {
189 database.save();
190 } catch (Exception e) {
191 log.error("Database save", e);
192 }
193
194
195 if ("Create".equals(action)) {
196 session.setAttribute(Constants.USER_KEY, user);
197 if (log.isTraceEnabled()) {
198 log.trace(" User '" + user.getUsername() +
199 "' logged on in session " + session.getId());
200 }
201 }
202
203
204 if (mapping.getAttribute() != null) {
205 if ("request".equals(mapping.getScope()))
206 request.removeAttribute(mapping.getAttribute());
207 else
208 session.removeAttribute(mapping.getAttribute());
209 }
210
211
212 if (log.isTraceEnabled()) {
213 log.trace(" Forwarding to success page");
214 }
215 return (mapping.findForward("success"));
216
217 }
218
219
220 }