1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.apps.mailreader.actions;
23
24 import org.apache.commons.beanutils.PropertyUtils;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32 import org.apache.struts.action.DynaActionForm;
33 import org.apache.struts.actions.MappingDispatchAction;
34 import org.apache.struts.apps.mailreader.Constants;
35 import org.apache.struts.apps.mailreader.dao.ExpiredPasswordException;
36 import org.apache.struts.apps.mailreader.dao.Subscription;
37 import org.apache.struts.apps.mailreader.dao.User;
38 import org.apache.struts.apps.mailreader.dao.UserDatabase;
39
40 import javax.servlet.ServletException;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpSession;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public abstract class BaseAction extends MappingDispatchAction {
58
59
60
61
62
63
64
65
66 public static String USERNAME = "username";
67
68
69
70
71
72
73 public static String PASSWORD = "password";
74
75
76
77
78
79
80 public final static String TASK = "task";
81
82
83
84
85
86
87
88
89 protected Log log = LogFactory.getLog(Constants.PACKAGE);
90
91
92
93
94
95
96
97
98
99
100
101
102 void doCacheUser(HttpServletRequest request, User user) {
103
104 HttpSession session = request.getSession();
105 session.setAttribute(Constants.USER_KEY, user);
106 if (log.isDebugEnabled()) {
107 log.debug(
108 "LogonAction: User '"
109 + user.getUsername()
110 + "' logged on in session "
111 + session.getId());
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124 protected void doCancel(HttpSession session, String method, String key) {
125 if (log.isTraceEnabled()) {
126 StringBuffer sb = new StringBuffer(128);
127 sb.append(Constants.LOG_CANCEL);
128 sb.append(method);
129 log.trace(sb.toString());
130 }
131 if (key != null) {
132 session.removeAttribute(key);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145 protected ActionForward doFindFailure(ActionMapping mapping) {
146 if (log.isTraceEnabled()) {
147 log.trace(Constants.LOG_FAILURE);
148 }
149 return mapping.findForward(Constants.FAILURE);
150 }
151
152
153
154
155
156
157
158
159
160
161 protected ActionForward doFindLogon(ActionMapping mapping) {
162 if (log.isTraceEnabled()) {
163 log.trace(Constants.LOG_LOGON);
164 }
165 return mapping.findForward(Constants.LOGON);
166 }
167
168
169
170
171
172
173
174
175
176
177
178 protected ActionForward doFindSuccess(ActionMapping mapping) {
179 if (log.isTraceEnabled()) {
180 log.trace(Constants.LOG_SUCCESS);
181 }
182 return mapping.findForward(Constants.SUCCESS);
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 protected String doGet(ActionForm form, String property) {
199 String initial;
200 try {
201 initial = (String) PropertyUtils.getSimpleProperty(form, property);
202 } catch (Throwable t) {
203 initial = null;
204 }
205 String value = null;
206 if ((initial != null) && (initial.length() > 0)) {
207 value = initial.trim();
208 if (value.length() == 0) {
209 value = null;
210 }
211 }
212 return value;
213 }
214
215
216
217
218
219
220
221
222
223 protected Subscription doGetSubscription(HttpSession session) {
224 return (Subscription) session.getAttribute(Constants.SUBSCRIPTION_KEY);
225 }
226
227
228
229
230
231
232
233
234
235 protected Subscription doGetSubscription(HttpServletRequest request) {
236 HttpSession session = request.getSession();
237 return doGetSubscription(session);
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 User doGetUser(UserDatabase database, String username,
256 String password, ActionMessages errors)
257 throws ExpiredPasswordException {
258
259 User user = null;
260 if (database == null) {
261 errors.add(
262 ActionMessages.GLOBAL_MESSAGE,
263 new ActionMessage("error.database.missing"));
264 } else {
265
266 if (username.equals("Hermes")) {
267 throw new ExpiredPasswordException("Hermes");
268 }
269
270 user = database.findUser(username);
271 if ((user != null) && !user.getPassword().equals(password)) {
272 user = null;
273 }
274 if (user == null) {
275 errors.add(
276 ActionMessages.GLOBAL_MESSAGE,
277 new ActionMessage("error.password.mismatch"));
278 }
279 }
280
281 return user;
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 User doGetUser(String username,
299 String password, ActionMessages errors)
300 throws ExpiredPasswordException {
301
302 return doGetUser(doGetUserDatabase(), username, password, errors);
303 }
304
305
306
307
308
309
310
311
312
313
314 protected UserDatabase doGetUserDatabase() {
315 return (UserDatabase) servlet.getServletContext().getAttribute(
316 Constants.DATABASE_KEY);
317 }
318
319
320
321
322
323
324
325
326
327 protected User doGetUser(HttpSession session) {
328 return (User) session.getAttribute(Constants.USER_KEY);
329 }
330
331
332
333
334
335
336
337
338
339 protected User doGetUser(HttpServletRequest request) {
340 HttpSession session = request.getSession();
341 return (User) session.getAttribute(Constants.USER_KEY);
342 }
343
344
345
346
347
348
349
350
351
352
353
354
355 protected ActionForward doInputForward(ActionMapping mapping,
356 HttpServletRequest request,
357 ActionMessages errors) {
358 this.saveErrors(request, errors);
359 this.saveToken(request);
360 return (mapping.getInputForward());
361 }
362
363
364
365
366
367
368
369
370
371 protected void doLogProcess(ActionMapping mapping, String method) {
372 if (log.isDebugEnabled()) {
373 StringBuffer sb = new StringBuffer(128);
374 sb.append(" ");
375 sb.append(mapping.getPath());
376 sb.append(":");
377 sb.append(Constants.LOG_PROCESSING);
378 sb.append(method);
379 log.debug(sb.toString());
380 }
381 }
382
383
384
385
386
387
388
389
390 protected void doSaveToken(HttpServletRequest request) {
391 if (log.isTraceEnabled()) {
392 log.trace(Constants.LOG_TOKEN);
393 }
394 saveToken(request);
395 }
396
397
398
399
400
401
402
403
404
405 protected void doSaveUser(User user) throws ServletException {
406
407 final String LOG_DATABASE_SAVE_ERROR =
408 " Unexpected error when saving User: ";
409
410 try {
411 UserDatabase database = doGetUserDatabase();
412 database.save();
413 } catch (Exception e) {
414 String message = LOG_DATABASE_SAVE_ERROR + user.getUsername();
415 log.error(message, e);
416 throw new ServletException(message, e);
417 }
418 }
419
420
421
422
423
424
425
426
427
428
429
430 protected boolean doSet(ActionForm form, String property, String value) {
431 try {
432 DynaActionForm dyna = (DynaActionForm) form;
433 dyna.set(property, value);
434 } catch (Throwable t) {
435 return false;
436 }
437 return true;
438 }
439
440 }