You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Yuri de Wit <yd...@gmail.com> on 2006/05/09 02:03:12 UTC

ClassCastException when forwarding wrapped HttpServletRequest

I am not sure I should have posted this question to the dev mailing list,
let me know if that should be the case.

I am using Tomcat 5.5.16.

I have a series of chained HttpServlets in my web app. The first servlet is
registered with the web.xml and the following ones are dispatched manually.
Before delegating the HTTP request to the next internal servlet, I am
wrapping the HttpServletRequest overriding getPathInfo() and
getServletPath(). If the pathInfo was /a/b and servletPath was /x, after
wrapping the request getPathInfo returns /b and the servletPath returns
/x/a. The reason I am doing this is that I would like forward and includes
to work relative to my internal pathInfo and servletPath (/b and /x/a)
instead of the web container one (/a/b and /x).

The first problem I encountered was that if I implemented the wrapper by
subclassing HttpServletRequestWrapper Tomcat implementation unwraps it
before forwarding and the final url contains the original path info appended
and my custom path info.

So I then changed my implementation and now I am wrapping the request using
my own subclass of HttpServletRequest instead. Now the problem is that I get
a ClassCast exception @ org.apache.catalina.core.ApplicationDispatcher line
814 ("current = ((ServletRequestWrapper) current).getRequest();").

Shouldnt that casting be conditional?

Is there a safer way of implementing the functionlity I described above?

thanks.
- yuri

Re: ClassCastException when forwarding wrapped HttpServletRequest

Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
I remember seeing the spec that you can't do this, you have to provide a 
ServletRequestWrapper implementation

Filip


Yuri de Wit wrote:
> I am not sure I should have posted this question to the dev mailing list,
> let me know if that should be the case.
>
> I am using Tomcat 5.5.16.
>
> I have a series of chained HttpServlets in my web app. The first 
> servlet is
> registered with the web.xml and the following ones are dispatched 
> manually.
> Before delegating the HTTP request to the next internal servlet, I am
> wrapping the HttpServletRequest overriding getPathInfo() and
> getServletPath(). If the pathInfo was /a/b and servletPath was /x, after
> wrapping the request getPathInfo returns /b and the servletPath returns
> /x/a. The reason I am doing this is that I would like forward and 
> includes
> to work relative to my internal pathInfo and servletPath (/b and /x/a)
> instead of the web container one (/a/b and /x).
>
> The first problem I encountered was that if I implemented the wrapper by
> subclassing HttpServletRequestWrapper Tomcat implementation unwraps it
> before forwarding and the final url contains the original path info 
> appended
> and my custom path info.
>
> So I then changed my implementation and now I am wrapping the request 
> using
> my own subclass of HttpServletRequest instead. Now the problem is that 
> I get
> a ClassCast exception @ org.apache.catalina.core.ApplicationDispatcher 
> line
> 814 ("current = ((ServletRequestWrapper) current).getRequest();").
>
> Shouldnt that casting be conditional?
>
> Is there a safer way of implementing the functionlity I described above?
>
> thanks.
> - yuri
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: ClassCastException when forwarding wrapped HttpServletRequest

Posted by Bill Barker <wb...@wilshire.com>.
>"Yuri de Wit" <yd...@gmail.com> wrote in message 
>news:bc472d90605081703s19810cf8i92af3c6da43be3b3@mail.gmail.com...
>I am not sure I should have posted this question to the dev mailing list,
>let me know if that should be the case.
>
>I am using Tomcat 5.5.16.
>
>I have a series of chained HttpServlets in my web app. The first servlet is
>registered with the web.xml and the following ones are dispatched manually.
>Before delegating the HTTP request to the next internal servlet, I am
>wrapping the HttpServletRequest overriding getPathInfo() and
>getServletPath(). If the pathInfo was /a/b and servletPath was /x, after
>wrapping the request getPathInfo returns /b and the servletPath returns
>/x/a. The reason I am doing this is that I would like forward and includes
>to work relative to my internal pathInfo and servletPath (/b and /x/a)
>instead of the web container one (/a/b and /x).
>

I assume that 'dispatched manually' means you are using getNamedDispatcher?

>The first problem I encountered was that if I implemented the wrapper by
>subclassing HttpServletRequestWrapper Tomcat implementation unwraps it
>before forwarding and the final url contains the original path info 
>appended
>and my custom path info.
>

Not strictly true.  Tomcat injects it's wrapper below yours.

It's an edge case, but it definitely a bug.  Tomcat shouldn't be trusting 
the wrapper to give it back the correct values.  Feel free to open up a BZ 
report (and, even better if it includes a simple example to reproduce :).

>So I then changed my implementation and now I am wrapping the request using
>my own subclass of HttpServletRequest instead. Now the problem is that I 
>get
>a ClassCast exception @ org.apache.catalina.core.ApplicationDispatcher line
>814 ("current = ((ServletRequestWrapper) current).getRequest();").
>
>Shouldnt that casting be conditional?
>

Nope.  Passing anything other than a ServletRequestWrapper or the orginal 
Request object is specifically forbidden by the Servlet spec.

>Is there a safer way of implementing the functionlity I described above?
>
>thanks.
>- yuri
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org