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