View Javadoc

1   /*
2    * $Id: StrutsRequestWrapper.java 454501 2006-10-09 20:49:26Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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              // don't bother with the standard javax.servlet attributes, we can short-circuit this
56              // see WW-953 and the forums post linked in that issue for more info
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          // note: we don't let # come through or else a request for
70          // #attr.foo or #request.foo could cause an endless loop
71          if (!alreadyIn && attribute == null && s.indexOf("#") == -1) {
72              try {
73                  // If not found, then try the ValueStack
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  }