users@jersey.java.net

Re: [Jersey] Re: accessing response

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 05 Nov 2009 17:58:42 +0100

Hi Jon,

Take a look at the LoggingFilter source code:

   http://fisheye4.atlassian.com/browse/~raw,r=2652/jersey/trunk/
jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/
LoggingFilter.java

or the GZIPContentEncodingFilter:

   http://fisheye4.atlassian.com/browse/~raw,r=2168/jersey/trunk/
jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/
GZIPContentEncodingFilter.java


To filter the bytes of the response representation you need to use a
ContainerResponseFilter and register an adapter, as performed in the
LoggingFilter:

     public ContainerResponse filter(ContainerRequest request,
ContainerResponse response) {
         response.setContainerResponseWriter(
                 new Adapter(response.getContainerResponseWriter()));
         return response;
     }

Registering an adapter allows one to modify the response stream
without buffering if necessary.

The LoggingFilter buffers the response entity where as the
GZIPContentEncodingFilter wraps the OutputStream around a
GZIPOutputStream.

Hth,
Paul.

On Nov 5, 2009, at 5:37 PM, Jon Iantosca wrote:

> Hope I didn't waste anyone's time! I just came across the interfaces
> ContainerResponseFilter, and ContainerRequestFilter. But of course
> I'm all ears for any additional suggestions.
>
> On Thu, Nov 5, 2009 at 10:58 AM, Jon Iantosca <digester_at_gmail.com>
> wrote:
> Howdy - I'm hoping someone can point me in the direction of how to
> go about accessing my restful responses. More specifically, I'd like
> access to the serialized response after my resource method has
> executed, but before it's written out to the client. My resource
> methods typically resemble the sample code below, producing both xml
> and json, but I'm hoping to find a solution that doesn't care about
> what's being returned. I'm hoping I can perhaps register something
> like an "about to send a response" kind of listener. Can someone
> point me int he right direction here?
>
> @GET
> @Produces({"application/xml", "application/json"})
> @Path( "account/{number}" )
> public Portfolio getSummary( @PathParam("number") String number ) {
> AccountDelegate delegate = new AccountDelegate( number );
> return delegate.getSummary();
> }
>