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