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