1 package org.apache.velocity.tools.struts;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Locale;
23 import java.util.Iterator;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpSession;
28
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.action.ActionMessages;
33 import org.apache.struts.config.ModuleConfig;
34 import org.apache.struts.config.ForwardConfig;
35 import org.apache.struts.config.ActionConfig;
36 import org.apache.struts.util.MessageResources;
37 import org.apache.struts.util.RequestUtils;
38 import org.apache.struts.taglib.TagUtils;
39 import org.apache.struts.util.ModuleUtils;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class StrutsUtils
63 {
64 public static final StrutsUtils INSTANCE = new StrutsUtils();
65
66 private StrutsUtils() {}
67
68 public StrutsUtils getInstance()
69 {
70 return INSTANCE;
71 }
72
73
74
75
76
77
78
79
80
81
82 public static MessageResources getMessageResources(HttpServletRequest request,
83 ServletContext app)
84 {
85 return getMessageResources(request, app, null);
86 }
87
88
89
90
91
92
93
94
95
96
97
98 public static MessageResources getMessageResources(HttpServletRequest request,
99 ServletContext app,
100 String bundle)
101 {
102
103 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, app);
104
105 if (bundle == null) {
106 bundle = Globals.MESSAGES_KEY;
107 }
108
109
110 MessageResources resources =
111 (MessageResources)request.getAttribute(bundle + moduleConfig.getPrefix());
112
113 if (resources == null) {
114 resources = (MessageResources) app.getAttribute(bundle + moduleConfig.getPrefix());
115 }
116 return resources;
117 }
118
119
120
121
122
123
124
125
126
127
128
129 public static ModuleConfig selectModule(String urlPath,
130 ServletContext app)
131 {
132
133 String prefix = ModuleUtils.getInstance().getModuleName(urlPath, app);
134
135
136 ModuleConfig config = (ModuleConfig)
137 app.getAttribute(Globals.MODULE_KEY + prefix);
138
139 return config;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153 public static Locale getLocale(HttpServletRequest request,
154 HttpSession session)
155 {
156 Locale locale = null;
157
158 if (session != null)
159 {
160 locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
161 }
162 if (locale == null)
163 {
164 locale = request.getLocale();
165 }
166 return locale;
167 }
168
169
170
171
172
173
174
175
176 public static String getToken(HttpSession session)
177 {
178 if (session == null)
179 {
180 return null;
181 }
182 return (String)session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public static ActionMessages getErrors(HttpServletRequest request)
198 {
199 ActionMessages errors = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
200 if (errors == null || errors.isEmpty())
201 {
202
203 HttpSession session = request.getSession(false);
204 if (session != null)
205 {
206 errors = (ActionMessages)session.getAttribute(Globals.ERROR_KEY);
207 }
208 }
209 return errors;
210 }
211
212
213
214
215
216
217
218
219
220
221 public static ActionMessages getMessages(HttpServletRequest request)
222 {
223 ActionMessages messages = (ActionMessages)request.getAttribute(Globals.MESSAGE_KEY);
224 if (messages == null || messages.isEmpty())
225 {
226
227 HttpSession session = request.getSession(false);
228 if (session != null)
229 {
230 messages = (ActionMessages)session.getAttribute(Globals.MESSAGE_KEY);
231 }
232 }
233 return messages;
234 }
235
236
237
238
239
240
241
242
243 public static ActionForm getActionForm(HttpServletRequest request,
244 HttpSession session)
245 {
246
247 ActionConfig mapping =
248 (ActionConfig)request.getAttribute(Globals.MAPPING_KEY);
249 if (mapping == null)
250 {
251 return null;
252 }
253
254
255 String attribute = mapping.getAttribute();
256 if (attribute == null)
257 {
258 return null;
259 }
260
261
262 if ("request".equals(mapping.getScope()))
263 {
264 return (ActionForm)request.getAttribute(attribute);
265 }
266 if (session != null)
267 {
268 return (ActionForm)session.getAttribute(attribute);
269 }
270 return null;
271 }
272
273
274
275
276
277
278
279
280 public static String getActionFormName(HttpServletRequest request,
281 HttpSession session)
282 {
283
284 ActionConfig mapping =
285 (ActionConfig)request.getAttribute(Globals.MAPPING_KEY);
286 if (mapping == null)
287 {
288 return null;
289 }
290
291 return mapping.getAttribute();
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 public static String getActionMappingName(String action) {
310
311 String value = action;
312 int question = action.indexOf('?');
313 if (question >= 0) {
314 value = value.substring(0, question);
315 }
316
317 int slash = value.lastIndexOf('/');
318 int period = value.lastIndexOf('.');
319 if ((period >= 0) && (period > slash)) {
320 value = value.substring(0, period);
321 }
322
323 return value.startsWith("/") ? value : ("/" + value);
324 }
325
326
327
328
329
330
331
332
333
334 public static String getActionMappingURL(ServletContext application,
335 HttpServletRequest request,
336 String action)
337 {
338 StringBuilder value = new StringBuilder(request.getContextPath());
339 ModuleConfig config =
340 (ModuleConfig)request.getAttribute(Globals.MODULE_KEY);
341 if (config != null)
342 {
343 value.append(config.getPrefix());
344 }
345
346
347 String servletMapping =
348 (String)application.getAttribute(Globals.SERVLET_KEY);
349
350 if (servletMapping != null)
351 {
352 String queryString = null;
353 int question = action.indexOf('?');
354
355 if (question >= 0)
356 {
357 queryString = action.substring(question);
358 }
359
360 String actionMapping = TagUtils.getInstance().getActionMappingName(action);
361
362 if (servletMapping.startsWith("*."))
363 {
364 value.append(actionMapping);
365 value.append(servletMapping.substring(1));
366 }
367 else if (servletMapping.endsWith("/*"))
368 {
369 value.append(servletMapping.substring
370 (0, servletMapping.length() - 2));
371 value.append(actionMapping);
372 }
373
374 if (queryString != null)
375 {
376 value.append(queryString);
377 }
378 }
379 else
380 {
381
382
383 if (!action.startsWith("/"))
384 {
385 value.append("/");
386 }
387 value.append(action);
388 }
389
390
391 return value.toString();
392 }
393
394
395
396
397
398
399
400
401
402
403 public static String getForwardURL(HttpServletRequest request,
404 ServletContext app,
405 String forward)
406 {
407 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, app);
408
409
410 ActionConfig actionConfig =
411 (ActionConfig)request.getAttribute(Globals.MAPPING_KEY);
412
413
414 ForwardConfig fc = null;
415 if(actionConfig != null)
416 {
417 fc = actionConfig.findForwardConfig(forward);
418 }
419
420
421
422 if(fc == null)
423 {
424 fc = moduleConfig.findForwardConfig(forward);
425
426
427 if (fc == null)
428 {
429 return null;
430 }
431 }
432
433 StringBuilder url = new StringBuilder();
434 if (fc.getPath().startsWith("/"))
435 {
436 url.append(request.getContextPath());
437 url.append(RequestUtils.forwardURL(request, fc, moduleConfig));
438 }
439 else
440 {
441 url.append(fc.getPath());
442 }
443 return url.toString();
444 }
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462 public static String errorMarkup(String property,
463 HttpServletRequest request,
464 HttpSession session,
465 ServletContext application)
466 {
467 return errorMarkup(property, null, request, session, application);
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487 public static String errorMarkup(String property,
488 String bundle,
489 HttpServletRequest request,
490 HttpSession session,
491 ServletContext application)
492 {
493 ActionMessages errors = getErrors(request);
494 if (errors == null)
495 {
496 return "";
497 }
498
499
500 Iterator reports = null;
501 if (property == null)
502 {
503 reports = errors.get();
504 }
505 else
506 {
507 reports = errors.get(property);
508 }
509
510 if (!reports.hasNext())
511 {
512 return "";
513 }
514
515
516 StringBuilder results = new StringBuilder();
517 String header = null;
518 String footer = null;
519 String prefix = null;
520 String suffix = null;
521 Locale locale = getLocale(request, session);
522
523 MessageResources resources =
524 getMessageResources(request, application, bundle);
525 if (resources != null)
526 {
527 header = resources.getMessage(locale, "errors.header");
528 footer = resources.getMessage(locale, "errors.footer");
529 prefix = resources.getMessage(locale, "errors.prefix");
530 suffix = resources.getMessage(locale, "errors.suffix");
531 }
532 if (header == null)
533 {
534 header = "errors.header";
535 }
536 if (footer == null)
537 {
538 footer = "errors.footer";
539 }
540
541 if (prefix == null)
542 {
543 prefix = "";
544 }
545 if (suffix == null)
546 {
547 suffix = "";
548 }
549
550 results.append(header);
551 results.append("\r\n");
552
553 String message;
554 while (reports.hasNext())
555 {
556 message = null;
557 ActionMessage report = (ActionMessage)reports.next();
558 if (resources != null && report.isResource())
559 {
560 message = resources.getMessage(locale,
561 report.getKey(),
562 report.getValues());
563 }
564
565 results.append(prefix);
566
567 if (message != null)
568 {
569 results.append(message);
570 }
571 else
572 {
573 results.append(report.getKey());
574 }
575
576 results.append(suffix);
577 results.append("\r\n");
578 }
579
580 results.append(footer);
581 results.append("\r\n");
582
583
584 return results.toString();
585 }
586
587 }