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