1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.struts.apps.mailreader.actions;
18
19 import org.apache.commons.beanutils.PropertyUtils;
20 import org.apache.struts.action.ActionForm;
21 import org.apache.struts.action.ActionForward;
22 import org.apache.struts.action.ActionMapping;
23 import org.apache.struts.action.ActionMessage;
24 import org.apache.struts.action.ActionMessages;
25 import org.apache.struts.action.DynaActionForm;
26 import org.apache.struts.apps.mailreader.Constants;
27 import org.apache.struts.apps.mailreader.dao.ExpiredPasswordException;
28 import org.apache.struts.apps.mailreader.dao.User;
29 import org.apache.struts.apps.mailreader.dao.UserDatabase;
30
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.servlet.http.HttpSession;
35 import java.lang.reflect.InvocationTargetException;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public final class RegistrationAction extends BaseAction {
50
51
52
53
54
55
56
57
58 public final static String FROM_ADDRESS = "fromAddress";
59
60
61
62
63
64
65 public final static String FULL_NAME = "fullName";
66
67
68
69
70
71
72 public final static String PASSWORD2 = "password2";
73
74
75
76
77
78
79 public final static String REPLY_TO_ADDRESS = "replyToAddress";
80
81
82
83
84
85
86
87
88 final String LOG_REGISTRATION_POPULATE = "RegistrationForm.populate";
89
90
91
92
93
94
95
96
97
98 private void errorUsernameUnique(String username,
99 ActionMessages errors) {
100 errors.add(
101 USERNAME,
102 new org.apache.struts.action.ActionMessage(
103 "error.username.unique", username));
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118 private User doCreateUser(
119 ActionForm form,
120 HttpServletRequest request,
121 ActionMessages errors) {
122
123 if (log.isTraceEnabled()) {
124 log.trace(" Perform additional validations on Create");
125 }
126
127 UserDatabase database = doGetUserDatabase();
128 String username = doGet(form, USERNAME);
129 try {
130 if (database.findUser(username) != null) {
131 errorUsernameUnique(username, errors);
132 }
133 }
134 catch (ExpiredPasswordException e) {
135 errorUsernameUnique(username, errors);
136 errors.add("errors.literal", new ActionMessage(e.getMessage()));
137 }
138
139 String password = doGet(form, PASSWORD);
140 if ((password == null) || (password.length() < 1)) {
141 errors.add(PASSWORD, new ActionMessage("error.password.required"));
142
143 String password2 = doGet(form, PASSWORD2);
144 if ((password2 == null) || (password2.length() < 1)) {
145 errors.add(
146 PASSWORD2,
147 new ActionMessage("error.password2.required"));
148 }
149 }
150
151 if (!errors.isEmpty()) {
152 return null;
153 }
154
155 User user = database.createUser(username);
156
157
158 HttpSession session = request.getSession();
159 session.setAttribute(Constants.USER_KEY, user);
160 if (log.isTraceEnabled()) {
161 log.trace(
162 " User: '"
163 + user.getUsername()
164 + "' logged on in session: "
165 + session.getId());
166 }
167
168 return user;
169 }
170
171
172
173
174
175
176
177
178
179
180 private void doPopulate(ActionForm form, User user)
181 throws ServletException {
182
183 final String title = Constants.EDIT;
184
185 if (log.isTraceEnabled()) {
186 log.trace(Constants.LOG_POPULATE_FORM + user);
187 }
188
189 try {
190 PropertyUtils.copyProperties(form, user);
191 DynaActionForm dyna = (DynaActionForm) form;
192 dyna.set(TASK, title);
193 dyna.set(PASSWORD, null);
194 dyna.set(PASSWORD2, null);
195 } catch (InvocationTargetException e) {
196 Throwable t = e.getTargetException();
197 if (t == null) {
198 t = e;
199 }
200 log.error(LOG_REGISTRATION_POPULATE, t);
201 throw new ServletException(LOG_REGISTRATION_POPULATE, t);
202 } catch (Throwable t) {
203 log.error(LOG_REGISTRATION_POPULATE, t);
204 throw new ServletException(LOG_REGISTRATION_POPULATE, t);
205 }
206 }
207
208
209
210
211
212
213
214
215
216
217 private void doPopulate(User user, ActionForm form)
218 throws ServletException {
219
220 if (log.isTraceEnabled()) {
221 log.trace(Constants.LOG_POPULATE_USER + user);
222 }
223
224 try {
225 String oldPassword = user.getPassword();
226 PropertyUtils.copyProperties(user, form);
227 String password = doGet(form, PASSWORD);
228 if ((password == null)
229 || (password.length() < 1)) {
230
231 user.setPassword(oldPassword);
232 }
233
234 } catch (InvocationTargetException e) {
235 Throwable t = e.getTargetException();
236 if (t == null) {
237 t = e;
238 }
239
240 log.error(LOG_REGISTRATION_POPULATE, t);
241 throw new ServletException(LOG_REGISTRATION_POPULATE, t);
242
243 } catch (Throwable t) {
244 log.error(LOG_REGISTRATION_POPULATE, t);
245 throw new ServletException(LOG_REGISTRATION_POPULATE, t);
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258 private void doValidateToken(HttpServletRequest request,
259 ActionMessages errors) {
260
261 if (log.isTraceEnabled()) {
262 log.trace(Constants.LOG_TOKEN_CHECK);
263 }
264
265 if (!isTokenValid(request)) {
266 errors.add(
267 ActionMessages.GLOBAL_MESSAGE,
268 new ActionMessage(Constants.MSG_TRANSACTION_TOKEN));
269 }
270
271 resetToken(request);
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 public ActionForward Edit(
290 ActionMapping mapping,
291 ActionForm form,
292 HttpServletRequest request,
293 HttpServletResponse response)
294 throws Exception {
295
296 final String method = Constants.EDIT;
297 doLogProcess(mapping, method);
298
299 HttpSession session = request.getSession();
300 User user = doGetUser(session);
301 boolean updating = (user != null);
302 if (updating) {
303 doPopulate(form, user);
304 }
305
306 doSaveToken(request);
307 return doFindSuccess(mapping);
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326 public ActionForward Save(
327 ActionMapping mapping,
328 ActionForm form,
329 HttpServletRequest request,
330 HttpServletResponse response)
331 throws Exception {
332
333 final String method = Constants.SAVE;
334 doLogProcess(mapping, method);
335
336 HttpSession session = request.getSession();
337 if (isCancelled(request)) {
338 doCancel(session, method, Constants.SUBSCRIPTION_KEY);
339 return doFindSuccess(mapping);
340 }
341
342 ActionMessages errors = new ActionMessages();
343 doValidateToken(request, errors);
344
345 if (!errors.isEmpty()) {
346 return doInputForward(mapping, request, errors);
347 }
348
349 User user = doGetUser(session);
350 if (user == null) {
351 user = doCreateUser(form, request, errors);
352 if (!errors.isEmpty()) {
353 return doInputForward(mapping, request, errors);
354 }
355 }
356
357 doPopulate(user, form);
358 doSaveUser(user);
359
360 return doFindSuccess(mapping);
361 }
362
363 }