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