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 Wed, 21 Apr 2010 20:54:37 GMT
On Wed, Apr 21, 2010 at 4:25 PM, Christopher Schultz
<chris@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> André,
>
> On 4/21/2010 3:46 PM, André Warnier wrote:
>> Mark Thomas wrote:
>>> On 21/04/2010 20:24, André Warnier wrote:
>>>> Mark Thomas wrote:
>>>> ...
>>>>
>>>>> I'd just use JAD and decompile it.
>>>>>
>>>> Thanks.  But although my intentions are not obnoxious nor illegal nor
>>>> anything of the kind, I would not want to even come under suspicion of
>>>> reverse-engineering.  So is there something that just lists the standard
>>>> calls/methods used in it ?
>>>
>>> No. You can see the methods it exposes, but not the methods it uses.
>>> Time to add some  debug code to your wrapper to see what is being called?
>>>
>> Mmmm. :-(
>> How do I do that, assuming I do not know in advance which methods it
>> calls ?
>> Do I need to define all the methods of HttpServletRequest in my wrapper,
>> just to make them trace their call ?
>> Or does there exist some more dummy-user-friendly methodology ?
>
> It's pretty inaccessible for novice Java programmers, but you could use
> the "proxy" API which is jsut about the coolest thing available in Java IMO.
>
> This is how you do the wrapping of the request:

Not to potentially muddy the waters, but I think your
InvocationHandler is invoking the method on the wrong object - it
shouldn't invoke it on the proxy but on the original request. So,
something like this:

public void doFilter(final ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {

    InvocationHandler handler = new InvocationHandler() {
        public Object invoke(Object proxy, Method method, Object[]
args) throws Throwable {
            // log some stuff
            return method.invoke(request, args);
        }
    };

    HttpServletRequest proxyRequest =
(HttpServletRequest)Proxy.newProxyInstance(HttpServletRequest.class.getClassLoader(),
new Class[] { HttpServletRequest.class }, handler);

    chain.doFilter(proxyRequest, response);
}

> import java.lang.reflect.Proxy;
> import java.lang.reflect.InvocationHandler;
>
> public class RequestMethodCallLogger
>  implements InvocationHandler, Filter
> {
>  public void doFilter(...) {
>    HttpServletRequest wrappedRequest
>     = Proxy.newProxyInstance(HttpServletRequest.class.getClassLoader(),
>                              new Class[] { HttpServletRequest.class },
>                              this);
>
>    chain.doFilter(wrappedRequest, response);
>  }
>
>  public Object invoke(Object proxy, Method method, Object[] args)
>  {
>    // Log to your favorite logger here
>
>    return method.invoke(proxy, args);
>  }
> }
>
> Basically, the Proxy class allows you to intercept the calls to
> interface methods. The implementation of the "invoke" method is just a
> pass-through to the "real" method after logging (an exercise left for
> the reader).
>
> This can be optimized a bit if you need to use it long-term, but the
> above is about as compact as it gets.
>
> I'd still recommend the use of jad, honestly.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkvPX0EACgkQ9CaO5/Lv0PCKVQCdG5SMXiySnsFEowVF7/44KM8s
> b7kAoIAGSzxOIWmKt4+z6ATkqslTl5uW
> =ykwF
> -----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