users@jersey.java.net

Re: [Jersey] Getting at the bytes of a response

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 26 Jan 2010 18:15:29 +0100

On Jan 26, 2010, at 4:26 PM, Felipe Gaścho wrote:

> this filters definitely should come as a Glassfish configuration ..
> because everyone is coding this everywhere.. it is a commodity coded
> repeatedly ...
>

Agreed it would be useful for GF to have such a Servlet filter but
there are advantages in Jersey too.

Such a Jersey based filter could also be reused by a
ResourceFilterFactory to decide when to apply the filter or not. Also
Jersey supports additional containers where such a filter could also
apply.

There is also scope for such filters to work well for async-based
applications (e.g. using Atmosphere) where as servlet filters for
async applications modifying responses can be problematic because
servlet filters are stack-based (when you return control, but not
return the response which happens later, the stack of filters needs to
be unwound).

Paul.


> On Tue, Jan 26, 2010 at 4:23 PM, Santiago Pericas-Geertsen
> <Santiago.Pericasgeertsen_at_sun.com> wrote:
>> Brian,
>>
>> Have a look at how the GZIP filter works:
>>
>> http://fisheye4.atlassian.com/browse/jersey/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/GZIPContentEncodingFilter.java?r=HEAD
>>
>> You could use a similar Adapter to write into a
>> ByteArrayOutputStream and then compute what you need in the
>> finish() method.
>>
>> -- Santiago
>>
>> On Jan 25, 2010, at 11:26 PM, Brian Gilstrap wrote:
>>
>>> Hello,
>>>
>>> I'm attempting to implement ETags using a ContainerRequestFilter
>>> and a ContainerResponseFilter. At first, all seemed to be well,
>>> with code like this for the response filter:
>>>
>>> public class EtagContainerResponseFilter extends Caching
>>> implements ContainerResponseFilter {
>>> public ContainerResponse filter(final ContainerRequest request,
>>> final ContainerResponse response) {
>>> // We only cache an ETag if the response is 200
>>> if (response.getStatus() == 200) {
>>> URI requestURI = request.getRequestUri();
>>> String uriString = requestURI.toString();
>>> MultivaluedMap<String, Object> httpHeaders =
>>> response.getHttpHeaders();
>>> MediaType contentType = (MediaType)
>>> httpHeaders.getFirst(CONTENT_TYPE);
>>>
>>> Object responseEntity = response.getEntity();
>>> if (responseEntity instanceof Serializable) {
>>> Serializable entity = (Serializable)
>>> response.getEntity();
>>> // calculate and cache the etag for this response
>>> entity
>>> }
>>> else {
>>> // TODO - logging
>>> System.err.println( "Not caching '" +
>>> requestURI.toString() + "' - not serializable..." );
>>> }
>>> }
>>> return response;
>>> }
>>> }
>>>
>>> But I am using mappers for things like a List<Foo>. I was
>>> surprised that the responseEntity is a List<Foo> in the response.
>>> This poses a problem since my Foo class is not Serializable.
>>>
>>> I could, of course, demand that all things inside lists and such
>>> be serializable. But I suspect that is error-prone (what happens
>>> when object serialization changes but the way that object is
>>> rendered into the specified media type doesn't change, for
>>> example). Plus, it means I have to force the rest of my code to
>>> make my model objects (Foo objects in this case) Serializable when
>>> they otherwise wouldn't need to be so.
>>>
>>> What I would really like to be able to do is to get ahold of the
>>> bytes written out (or getting ready to be written out) and use
>>> that to calculate my ETag. I'm hoping I've taken the wrong
>>> approach and you can point me at a fairly simple way to do this.
>>>
>>> Thanks!
>>> Brian
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
>
>
> --
> ------------------------------------------
> Felipe Gaścho
> 10+ Java Programmer
> CEJUG Senior Advisor
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>