1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.contexts;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import org.apache.commons.chain.Context;
28 import org.apache.commons.chain.impl.ContextBase;
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionMessages;
32 import org.apache.struts.chain.Constants;
33 import org.apache.struts.config.ActionConfig;
34 import org.apache.struts.config.FormBeanConfig;
35 import org.apache.struts.config.ForwardConfig;
36 import org.apache.struts.config.ModuleConfig;
37 import org.apache.struts.util.MessageResources;
38 import org.apache.struts.util.TokenProcessor;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48
49 public abstract class ActionContextBase extends ContextWrapper
50 implements ActionContext {
51
52
53
54 public static final String ACTION_KEY = Constants.ACTION_KEY;
55
56
57
58
59 public static final String ACTION_CONFIG_KEY = Constants.ACTION_CONFIG_KEY;
60
61
62
63
64 public static final String ACTION_FORM_KEY = Constants.ACTION_FORM_KEY;
65
66
67
68
69 public static final String FORWARD_CONFIG_KEY =
70 Constants.FORWARD_CONFIG_KEY;
71
72
73
74
75 public static final String MODULE_CONFIG_KEY = Constants.MODULE_CONFIG_KEY;
76
77
78
79
80 public static final String EXCEPTION_KEY = Constants.EXCEPTION_KEY;
81
82
83
84
85
86 public static final String ERROR_ACTION_MESSAGES_KEY = "errors";
87
88
89
90
91
92 public static final String MESSAGE_ACTION_MESSAGES_KEY = "messages";
93
94
95
96
97 public static final String MESSAGE_RESOURCES_KEY =
98 Constants.MESSAGE_RESOURCES_KEY;
99
100
101
102
103 public static final String INCLUDE_KEY = Constants.INCLUDE_KEY;
104
105
106
107
108 public static final String LOCALE_KEY = Constants.LOCALE_KEY;
109
110
111
112
113 public static final String CANCEL_KEY = Constants.CANCEL_KEY;
114
115
116
117
118 public static final String VALID_KEY = Constants.VALID_KEY;
119
120
121
122
123
124 public static final String TRANSACTION_TOKEN_KEY = "TRANSACTION_TOKEN_KEY";
125
126
127
128
129
130 public static final String TOKEN_KEY = "TOKEN_KEY";
131
132
133
134
135 protected TokenProcessor token = null;
136
137
138
139
140 private Logger logger = null;
141
142
143
144
145
146
147 public ActionContextBase(Context context) {
148 super(context);
149 token = TokenProcessor.getInstance();
150 logger = LoggerFactory.getLogger(this.getClass());
151 }
152
153
154
155
156
157 public ActionContextBase() {
158 this(new ContextBase());
159 }
160
161
162
163
164 public void release() {
165 this.token = null;
166 }
167
168 public abstract Map<String, Object> getApplicationScope();
169
170 public abstract Map<String, Object> getRequestScope();
171
172 public abstract Map<String, Object> getSessionScope();
173
174 public Map<String, Object> getScope(String scopeName) {
175 if (REQUEST_SCOPE.equals(scopeName)) {
176 return this.getRequestScope();
177 }
178
179 if (SESSION_SCOPE.equals(scopeName)) {
180 return this.getSessionScope();
181 }
182
183 if (APPLICATION_SCOPE.equals(scopeName)) {
184 return this.getApplicationScope();
185 }
186
187 throw new IllegalArgumentException("Invalid scope: " + scopeName);
188 }
189
190
191
192
193 public void setAction(Action action) {
194 this.put(ACTION_KEY, action);
195 }
196
197 public Action getAction() {
198 return (Action) this.get(ACTION_KEY);
199 }
200
201 public void setActionForm(ActionForm form) {
202 this.put(ACTION_FORM_KEY, form);
203 }
204
205 public ActionForm getActionForm() {
206 return (ActionForm) this.get(ACTION_FORM_KEY);
207 }
208
209 public void setActionConfig(ActionConfig config) {
210 this.put(ACTION_CONFIG_KEY, config);
211 }
212
213 public ActionConfig getActionConfig() {
214 return (ActionConfig) this.get(ACTION_CONFIG_KEY);
215 }
216
217 public void setForwardConfig(ForwardConfig forward) {
218 this.put(FORWARD_CONFIG_KEY, forward);
219 }
220
221 public ForwardConfig getForwardConfig() {
222 return (ForwardConfig) this.get(FORWARD_CONFIG_KEY);
223 }
224
225 public void setInclude(String include) {
226 this.put(INCLUDE_KEY, include);
227 }
228
229 public String getInclude() {
230 return (String) this.get(INCLUDE_KEY);
231 }
232
233 public Boolean getFormValid() {
234 return (Boolean) this.get(VALID_KEY);
235 }
236
237 public void setFormValid(Boolean valid) {
238 this.put(VALID_KEY, valid);
239 }
240
241 public ModuleConfig getModuleConfig() {
242 return (ModuleConfig) this.get(MODULE_CONFIG_KEY);
243 }
244
245 public void setModuleConfig(ModuleConfig config) {
246 this.put(MODULE_CONFIG_KEY, config);
247 }
248
249 public Exception getException() {
250 return (Exception) this.get(EXCEPTION_KEY);
251 }
252
253 public void setException(Exception e) {
254 this.put(EXCEPTION_KEY, e);
255 }
256
257
258
259
260 public void addMessages(ActionMessages messages) {
261 this.addActionMessages(MESSAGE_ACTION_MESSAGES_KEY, messages);
262 }
263
264 public void addErrors(ActionMessages errors) {
265 this.addActionMessages(ERROR_ACTION_MESSAGES_KEY, errors);
266 }
267
268 public ActionMessages getErrors() {
269 return (ActionMessages) this.get(ERROR_ACTION_MESSAGES_KEY);
270 }
271
272 public ActionMessages getMessages() {
273 return (ActionMessages) this.get(MESSAGE_ACTION_MESSAGES_KEY);
274 }
275
276 public void saveErrors(ActionMessages errors) {
277 this.saveActionMessages(ERROR_ACTION_MESSAGES_KEY, errors);
278 }
279
280 public void saveMessages(ActionMessages messages) {
281 this.saveActionMessages(MESSAGE_ACTION_MESSAGES_KEY, messages);
282 }
283
284
285
286
287
288
289
290
291
292
293 public void addActionMessages(String key, ActionMessages messages) {
294 if (messages == null) {
295
296 return;
297 }
298
299
300 ActionMessages requestMessages = (ActionMessages) this.get(key);
301
302 if (requestMessages == null) {
303 requestMessages = new ActionMessages();
304 }
305
306
307 requestMessages.add(messages);
308
309
310 this.remove(key);
311
312
313 this.saveActionMessages(key, requestMessages);
314 }
315
316
317
318
319
320
321
322
323
324
325
326 public void saveActionMessages(String key, ActionMessages messages) {
327 this.saveActionMessages(REQUEST_SCOPE, key, messages);
328 }
329
330
331
332
333
334
335
336
337
338 public void saveActionMessages(String scopeId, String key,
339 ActionMessages messages) {
340 Map<String, Object> scope = getScope(scopeId);
341
342 if ((messages == null) || messages.isEmpty()) {
343 scope.remove(key);
344
345 return;
346 }
347
348 scope.put(key, messages);
349 }
350
351
352
353
354
355
356
357
358
359
360
361 public void saveMessages(String scope, ActionMessages messages) {
362 this.saveMessages(messages);
363 }
364
365
366
367
368
369
370
371
372
373 public void saveToken() {
374 String token = this.generateToken();
375
376 this.put(TRANSACTION_TOKEN_KEY, token);
377 }
378
379 public String generateToken() {
380 return token.generateToken(getTokenGeneratorId());
381 }
382
383
384
385
386
387
388 protected String getTokenGeneratorId() {
389 return "";
390 }
391
392 public boolean isTokenValid() {
393 return this.isTokenValid(false);
394 }
395
396 public boolean isTokenValid(boolean reset) {
397
398
399 String saved = (String) this.get(TRANSACTION_TOKEN_KEY);
400
401 if (saved == null) {
402 return false;
403 }
404
405 if (reset) {
406 this.resetToken();
407 }
408
409
410 String token = (String) this.get(TOKEN_KEY);
411
412 if (token == null) {
413 return false;
414 }
415
416 return saved.equals(token);
417 }
418
419 public void resetToken() {
420 this.remove(TRANSACTION_TOKEN_KEY);
421 }
422
423
424
425
426 public Boolean getCancelled() {
427 return (Boolean) this.get(CANCEL_KEY);
428 }
429
430 public void setCancelled(Boolean cancelled) {
431 this.put(CANCEL_KEY, cancelled);
432 }
433
434
435
436
437 public void setMessageResources(MessageResources messageResources) {
438 this.put(MESSAGE_RESOURCES_KEY, messageResources);
439 }
440
441 public MessageResources getMessageResources() {
442 return (MessageResources) this.get(MESSAGE_RESOURCES_KEY);
443 }
444
445 public MessageResources getMessageResources(String key) {
446 return (MessageResources) this.get(key);
447 }
448
449
450
451
452 public void setLocale(Locale locale) {
453 this.put(LOCALE_KEY, locale);
454 }
455
456 public Locale getLocale() {
457 return (Locale) this.get(LOCALE_KEY);
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471 public Logger getLogger() {
472 return this.logger;
473 }
474
475
476
477
478
479
480
481
482
483
484 public void setLogger(Logger logger) {
485 this.logger = logger;
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510 public ActionForm findOrCreateActionForm(String formName, String scopeName)
511 throws InstantiationException, IllegalAccessException,
512 IllegalArgumentException, InvocationTargetException,
513 NoSuchMethodException, SecurityException, ClassNotFoundException {
514
515 return this.findOrCreateActionForm(formName, scopeName,
516 this.getModuleConfig());
517 }
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 public ActionForm findOrCreateActionForm(String formName, String scopeName,
541 ModuleConfig moduleConfig)
542 throws InstantiationException, IllegalAccessException,
543 IllegalArgumentException, InvocationTargetException,
544 NoSuchMethodException, SecurityException, ClassNotFoundException {
545
546 Map<String, Object> scope = this.getScope(scopeName);
547
548 ActionForm instance;
549 FormBeanConfig formBeanConfig =
550 moduleConfig.findFormBeanConfig(formName);
551
552 if (formBeanConfig == null) {
553 throw new IllegalArgumentException("No form config found under "
554 + formName + " in module " + moduleConfig.getPrefix());
555 }
556
557 instance = (ActionForm) scope.get(formName);
558
559
560 if (instance != null) {
561 getLogger().trace("Found an instance in scope " + scopeName
562 + "; test for reusability");
563
564 if (formBeanConfig.canReuse(instance)) {
565 return instance;
566 }
567 }
568
569 ActionForm form = formBeanConfig.createActionForm(this);
570
571
572 scope.put(formName, form);
573
574 return form;
575 }
576 }