tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kris Schneider <kschnei...@gmail.com>
Subject Re: a servlet-related Java question
Date Thu, 22 Apr 2010 20:48:17 GMT
On Thu, Apr 22, 2010 at 4:31 PM, Christopher Schultz
<chris@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Bill,
>
> On 4/22/2010 2:37 AM, Bill Barker wrote:
>> If [the request/filter] does a forward or include done the line, this
>> won't work with any remotely recent version of Tomcat.  These
>> versions enforce the spec requirement that the Request has to be a
>> subclass of HttpServletWrapper wrapping the original request, or the
>> original request.
>
> The following filter works as expected on Tomcat 6.0.26 (some changes
> were required to make it actually work... my off-the-cuff implementation
> was lacking a few details).
>
> I can confirm that this filter operates properly across a forward()
> call: I have a struts action handles by the Struts servlet that forwards
> to a Velocity template handled by the Velocity servlet. All calls are
> logged to stdout. (Wow, lots of calls to Request.getAttribute!)

For reference, here's the snippet from the Servlet 2.5 Spec:

SRV.8.2 Using a Request Dispatcher

To use a request dispatcher, a servlet calls either the include method
or forward method of the RequestDispatcher interface. The parameters
to these methods can be either the request and response arguments that
were passed in via the service method of the javax.servlet interface,
or instances of subclasses of the request and response wrapper classes
that were introduced for version 2.3 of the specification. In the
latter case, the wrapper instances must wrap the request or response
objects that the container passed into the service method.

But...the proxy is created prior to entering a servlet's service
method, so it may well appear to be the "original" request for the
purposes of creating and validating a dispatcher and its parameters...

> import java.io.IOException;
>
> import java.lang.reflect.InvocationHandler;
> import java.lang.reflect.Method;
> import java.lang.reflect.Proxy;
>
> import javax.servlet.Filter;
> import javax.servlet.FilterChain;
> import javax.servlet.FilterConfig;
> import javax.servlet.ServletException;
> import javax.servlet.ServletRequest;
> import javax.servlet.ServletResponse;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
>
> public class RequestMethodCallLogger
>  implements Filter
> {
>    public void doFilter(ServletRequest request,
>                         ServletResponse response,
>                         FilterChain chain)
>        throws ServletException, IOException
>    {
>        if(request instanceof HttpServletRequest)
>            request = (ServletRequest)Proxy
>
> .newProxyInstance(HttpServletRequest.class.getClassLoader(),
>                                    new Class[] {
> HttpServletRequest.class },
>                                    new Wrapper(request));
>
>        chain.doFilter(request, response);
>    }
>
>    public void init(FilterConfig config) { }
>    public void destroy() { }
>
>    static class Wrapper
>        implements InvocationHandler
>    {
>        private Object _target;
>
>        Wrapper(Object target)
>        {
>            _target = target;
>        }
>
>        public Object invoke(Object proxy, Method method, Object[] args)
>            throws Throwable
>        {
>            System.out.print("Intercepted: ");
>            System.out.println(method);
>
>            return method.invoke(_target, args);
>        }
>    }
> }
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkvQsjUACgkQ9CaO5/Lv0PA65ACgiR4tiSji6MElZr9/Z0ibXdtX
> WJQAnRoB/GZbrSwdfPjcf50IpHFmW4L9
> =Stkm
> -----END PGP SIGNATURE-----

-- 
Kris Schneider

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


Mime
View raw message