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.struts2.dispatcher;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletRequestWrapper;
26
27 import com.opensymphony.xwork2.ActionContext;
28 import com.opensymphony.xwork2.util.ValueStack;
29
30 /***
31 * <!-- START SNIPPET: javadoc -->
32 *
33 * All Struts requests are wrapped with this class, which provides simple JSTL accessibility. This is because JSTL
34 * works with request attributes, so this class delegates to the value stack except for a few cases where required to
35 * prevent infinite loops. Namely, we don't let any attribute name with "#" in it delegate out to the value stack, as it
36 * could potentially cause an infinite loop. For example, an infinite loop would take place if you called:
37 * request.getAttribute("#attr.foo").
38 *
39 * <!-- END SNIPPET: javadoc -->
40 *
41 */
42 public class StrutsRequestWrapper extends HttpServletRequestWrapper {
43
44 /***
45 * The constructor
46 * @param req The request
47 */
48 public StrutsRequestWrapper(HttpServletRequest req) {
49 super(req);
50 }
51
52 /***
53 * Gets the object, looking in the value stack if not found
54 *
55 * @param s The attribute key
56 */
57 public Object getAttribute(String s) {
58 if (s != null && s.startsWith("javax.servlet")) {
59
60
61 return super.getAttribute(s);
62 }
63
64 ActionContext ctx = ActionContext.getContext();
65 Object attribute = super.getAttribute(s);
66 if (ctx != null) {
67 if (attribute == null) {
68 boolean alreadyIn = false;
69 Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");
70 if (b != null) {
71 alreadyIn = b.booleanValue();
72 }
73
74
75
76 if (!alreadyIn && s.indexOf("#") == -1) {
77 try {
78
79 ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);
80 ValueStack stack = ctx.getValueStack();
81 if (stack != null) {
82 attribute = stack.findValue(s);
83 }
84 } finally {
85 ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);
86 }
87 }
88 }
89 }
90 return attribute;
91 }
92 }