View Javadoc

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