users@jersey.java.net

Re: [Jersey] Forwarding Responses

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 21 Jan 2010 12:41:45 +0000

On Jan 21, 2010, at 11:26 AM, Cemo Koc wrote:

>
> Hi,
>
> What is the best way to forwarding Responses in different
> Applications. My
> workflow is something like this:
>
> A ===> Client B ===> Resource B ===> Client C ===> Resource C
>
> And my Resource C is returning instance of Response
>

What client API will you be using?

Will the Resource just forward responses or make modifications to
responses (adding headers, transforming the response entity)?

For the Jersey client API we could support:

    // Direct forward
   @GET
   public ClientResponse get() {
     Client c = ...
     WebResource r = c.resource(...);
     ClientResponse cr = r.get(ClientResponse.class);
     // check for error states

     return cr;
   }


    // Forward, with header modification
   @GET
   public Response get() {
     Client c = ...
     WebResource r = c.resource(...);
     ClientResponse cr = r.get(ClientResponse.class);
     // check for error states

     return Response.ok(cr)....build();
   }

Namely, there would be a MessageBodyWriter supporting ClientResponse.

Another approach could to support a MessageBodyWriter for a
WebResource or a corresponding client response builder, if say one
will perform a GET on the resource with some simple error handling.


If one were doing things explicitly one would do:

    // Forward, with header modification
   @GET
   public Response get() {
     Client c = ...
     WebResource r = c.resource(...);
     ClientResponse cr = r.get(ClientResponse.class);
     // check for error states

     Response.ResponseBuilder rc =
Response.ok(cr.getEntity(InputStream.class));
     // set headers from ClientResponse
     return rw.build();
   }

Paul.