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  import javax.servlet.http.HttpSession;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * PortletServletRequestWrapper
29   * 
30   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
31   * @version $Id: PortletServletRequestWrapper.java 510753 2007-02-23 01:28:50Z ate $
32   */
33  public class PortletServletRequestWrapper extends HttpServletRequestWrapper
34  {
35      private static final Log log = LogFactory.getLog(PortletServletRequestWrapper.class);
36      private ServletContext context;
37      private HttpSession session;
38      
39      public PortletServletRequestWrapper(ServletContext context, HttpServletRequest request, HttpSession proxiedSession)
40      {
41          super(request);
42          this.context = context;
43          session = proxiedSession;
44          if ( proxiedSession == null )
45          {
46              session = request.getSession();
47          }
48      }
49  
50      public String getPathInfo()
51      {
52          return (String) getAttribute("javax.servlet.include.path_info");
53      }
54  
55      public String getContextPath()
56      {
57          return (String) getAttribute("javax.servlet.include.context_path");
58      }
59  
60      public String getRequestURI()
61      {
62          return (String) getAttribute("javax.servlet.include.request_uri");
63      }
64  
65      public String getServletPath()
66      {
67          return (String) getAttribute("javax.servlet.include.servlet_path");
68      }
69  
70      public String getQueryString()
71      {
72          return (String) getAttribute("javax.servlet.include.query_string");
73      }
74  
75      public RequestDispatcher getRequestDispatcher(String relativePath)
76      {
77          // Below comment and workaround taken from
78          // org.apache.jasper.runtime.JspRuntimeLibrary.include(...)
79          // of Tomcat 4.1.29.
80          //
81          // FIXME - It is tempting to use request.getRequestDispatcher() to
82          // resolve a relative path directly, but Catalina currently does not
83          // take into account whether the caller is inside a RequestDispatcher
84          // include or not. Whether Catalina *should* take that into account
85          // is a spec issue currently under review. In the mean time,
86          String path;
87          if (!relativePath.startsWith("/"))
88          {
89              path = getServletPath();
90              path = path.substring(0, path.lastIndexOf('/')) + '/'
91                      + relativePath;
92          } else
93              path = relativePath;
94          // Because our wrapped request actually is within the Portal context
95          // using getRequest().getRequestDispatcher(path) still won't work!
96          // Therefore to keep it inside the PortletContext our own
97          // servletContext is
98          // asked for a dispatcher. The above patch ensures that all requested
99          // paths
100         // are context relative.
101         RequestDispatcher dispatcher = context.getRequestDispatcher(path);
102         if (dispatcher != null)
103             return new PortletServletRequestDispatcher(dispatcher, path, false);
104         else
105             return null;
106     }
107 
108     public HttpSession getSession()
109     {
110         return session;
111     }
112 }