View Javadoc

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