View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.struts;
17  
18  import javax.servlet.RequestDispatcher;
19  import javax.servlet.ServletContext;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletRequestWrapper;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /***
27   * PortletServletRequestWrapper
28   * 
29   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
30   * @version $Id: PortletServletRequestWrapper.java 188278 2005-01-28 01:09:23 +0100 (Fri, 28 Jan 2005) ate $
31   */
32  public class PortletServletRequestWrapper extends HttpServletRequestWrapper
33  {
34      private static final Log log = LogFactory.getLog(PortletServletRequestWrapper.class);
35      private ServletContext context;
36      public PortletServletRequestWrapper(ServletContext context, HttpServletRequest request)
37      {
38          super(request);
39          this.context = context;
40      }
41  
42      public String getPathInfo()
43      {
44          return (String) getAttribute("javax.servlet.include.path_info");
45      }
46  
47      public String getContextPath()
48      {
49          return (String) getAttribute("javax.servlet.include.context_path");
50      }
51  
52      public String getRequestURI()
53      {
54          return (String) getAttribute("javax.servlet.include.request_uri");
55      }
56  
57      public String getServletPath()
58      {
59          return (String) getAttribute("javax.servlet.include.servlet_path");
60      }
61  
62      public String getQueryString()
63      {
64          return (String) getAttribute("javax.servlet.include.query_string");
65      }
66  
67      public RequestDispatcher getRequestDispatcher(String relativePath)
68      {
69          // Below comment and workaround taken from
70          // org.apache.jasper.runtime.JspRuntimeLibrary.include(...)
71          // of Tomcat 4.1.29.
72          //
73          // FIXME - It is tempting to use request.getRequestDispatcher() to
74          // resolve a relative path directly, but Catalina currently does not
75          // take into account whether the caller is inside a RequestDispatcher
76          // include or not. Whether Catalina *should* take that into account
77          // is a spec issue currently under review. In the mean time,
78          String path;
79          if (!relativePath.startsWith("/"))
80          {
81              path = getServletPath();
82              path = path.substring(0, path.lastIndexOf('/')) + '/'
83                      + relativePath;
84          } else
85              path = relativePath;
86          // Because our wrapped request actually is within the Portal context
87          // using getRequest().getRequestDispatcher(path) still won't work!
88          // Therefore to keep it inside the PortletContext our own
89          // servletContext is
90          // asked for a dispatcher. The above patch ensures that all requested
91          // paths
92          // are context relative.
93          RequestDispatcher dispatcher = context.getRequestDispatcher(path);
94          if (dispatcher != null)
95              return new PortletServletRequestDispatcher(dispatcher, path, false);
96          else
97              return null;
98      }
99  }