users@jersey.java.net

[Jersey] Re: How to intercept response and get original entitiy object?

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Thu, 22 Aug 2013 14:19:03 +0200

Hi M,

My understanding was that you just wanted to wrap the original
content with some additional stuff. Similar functionality has been implemented
e.g. in the Jersey JSONP wrapper (see [1]).
Using an entity interceptor in such case is quite straight-forward.

Should you want to manipulate the entity instance (or set a completely different one),
i would suggest you consider implementing a ContainerResponseFilter,
where you get direct access to both request and response data (see [2]).

~Jakub

[1]https://github.com/jersey/jersey/blob/master/core-server/src/main/java/org/glassfish/jersey/server/internal/JsonWithPaddingInterceptor.java
[2]https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/container/ContainerResponseFilter.html

On Aug 22, 2013, at 12:30 PM, marceloverdijk_at_gmail.com wrote:

> Hi Jakub,
>
> Thanks for your reply. So with @Context UriInfo uriInfo I can get the
> "suppress response codes" request parameter.
> But what I wonder more is how to get the response body to enhance it?
> To be more concrete I send a a Response in my resource using:
>
> javax.json.JsonObject json = ..
> return Response.ok().entity(json).build()
>
> Can I get hold on this json (JsonObject) in a response interceptor?
>
>
> /M
>
>
>
> =======================================================
> Hi,
>
> Confirmed this should be possible using an interceptor.
>
> You only need to inject e.g. UriInfo (with @Context UriInfo uriInfo)
> into your interceptor to get access to the query parameters.
>
> HTH,
>
> ~Jakub
>
>
> On Aug 22, 2013, at 8:48 AM, marceloverdijk@... wrote:
>
>> My resource method is returning a javax.json.JsonObject containing
>> data.
>>
>> I want to intercept this response, and enhance the JsonObject with
>> additional data.
>>
>> Is this possible in an interceptor?
>>
>>
>> The use case is I want to implement "suppress response codes"
>> functionality similar to Twitters api.
>> If a certain request parameter is present in the api request (e.g.
>> ?suppress_response_codes=true) I want to wrap my actual response in
>> something like:
>> { "status": 200,
>> "response": { .. }
>> }