View Javadoc

1   /*
2    * $Id: StrutsRequestWrapper.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
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              // don't bother with the standard javax.servlet attributes, we can short-circuit this
60              // see WW-953 and the forums post linked in that issue for more info
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                  // note: we don't let # come through or else a request for
75                  // #attr.foo or #request.foo could cause an endless loop
76                  if (!alreadyIn && s.indexOf("#") == -1) {
77                      try {
78                          // If not found, then try the ValueStack
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  }