HttpServletRequestWrapper, Tomcat and forwards

I’ve been working on getting the JCatapult security framework to leverage Struts actions rather than a bunch of configuration and additional custom code. In order to pull all of this off, I had to modify the HttpServletRequest in order to make it seem like the requested URI was different than the original request URI. I used a HttpServletRequestWrapper in order to pull this off like this:

final HttpServletRequest httpRequest = (HttpServletRequest) request;

HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(httpRequest) {
  @Override
  public String getRequestURI() {
    return successfulLoginURI;
  }

  @Override
  public String getServletPath() {
    return successfulLoginURI;
  }

  @Override
  public RequestDispatcher getRequestDispatcher(String uri) {
    final RequestDispatcher rd = httpRequest.getRequestDispatcher(uri);
    return new RequestDispatcher() {
      public void forward(ServletRequest servletRequest, ServletResponse servletResponse)
      throws ServletException, IOException {
        rd.forward(httpRequest, servletResponse);
      }

      public void include(ServletRequest servletRequest, ServletResponse servletResponse)
      throws ServletException, IOException {
        rd.include(httpRequest, servletResponse);
      }
    };
  }
};

As it turns out, I had to do that little tricky part at the end with the RequestDispatcher because Tomcat fails during a forward if the incoming HttpServletRequest is a wrapper.

This entry was posted in JCatapult, Java. Bookmark the permalink.

4 Responses to HttpServletRequestWrapper, Tomcat and forwards

  1. Steve says:

    How did you discover that tomcat failed on forwards when the request had been wrapped? I can’t find any information online and the workaround you are using will not work for me since I need to have the wrapped request for the resource being forwarded to.

  2. It was pretty simple. I just wrapped the request so that the request URI was something different. This was done in a filter. The request got passed down to struts and struts pulled out the new URI correctly and invoked the action. Once the action finished Struts tried to forward the request to the JSP and this is where it failed. The container was unable to perform the forward because the HttpServletRequest that was being passed to the request dispatcher had a different request URI and was a wrapper. Tomcat doesn’t like that and was throwing some strange exceptions back.

  3. David says:

    Hi.

    Possibly is not exactly the correct place, but I need to resolve this stuff and your blog is related with this, so I ask you for some help.

    I’m using tomcat 5.0.x and trying to get the parameters in URL I get null.

    1 way:
    for (String requestName : requests.keySet()) {
    if (requests.get(requestName) instanceof HttpServletRequestWrapper) {
    request = (HttpServletRequest) ((HttpServletRequestWrapper) requests
    .get(requestName)).getRequest();
    retVal = request.getParameter(“area”);
    System.out.println(“retVal: ” + retVal);
    }
    }

    2 way:
    Map params = fc.getExternalContext().getRequestParameterMap();
    Iterator it = params.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry) it.next();
    System.out.println(pairs.getKey() + ” = ” + pairs.getValue());
    }

    3 way:
    String area = (String) FacesContext.getCurrentInstance()
    .getExternalContext().getRequestParameterMap().get(“area”);

    4 way:
    area = fc.getCurrentInstance().getExternalContext()
    .getRequestParameterMap().get(“area”);

    and the simple url in index.jsp

    Thanks

  4. Pia says:

    I am facing the same problem that you described: I have a subclassed HttpServletRequestWrapper which I am using to check/add parameters to the request. I am using a filter that wraps this request. While forwarding my filtered request, tomcat overrides the new request and keeps mine intact. Tomcat doesn’t throw any exceptions, just neatly replaces the original request.
    I tried your solution but it doesn’t work for me! Am I missing something?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>