users@jersey.java.net

Re: [Jersey] How to write a proxy operation

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 24 Mar 2009 11:00:26 +0100

On Mar 23, 2009, at 10:25 PM, Farrukh Najmi wrote:

>
> I have a REST interface to my backend server. I also have a REST
> interface for the server side of my Web UI.
> In some cases a REST interface operation in my UI server needs to
> simply proxy for and pass-through to my backend server.
>
> Are there any examples on how to do this sort of proxy operation?
>
> my method looks something like this:
>
> @Path("/search")
> @GET
> @Produces({"application/xml","application/json"})
> public Response search(@QueryParam("format") String format,
> @QueryParam("queryId") String queryId) throws JAXBException {
>
> Response response=null;
>
> ....
> return response;
> }
>

You could use the Jersey Client API to build up the request and then
map the response:

     Client c = Client.create();

     UriInfo ui = ...

     URI u = ui.getRequestUriBuilder().host(...).port(...).build();
     WebResource r = c.resource(ui);
     ClientResponse cr = r.get(ClientResponse.class);
     // Check status

     // Build Response from ClientResponse.
     Response r = Response.entiy(cr.getEntityInputStream()).build();

     return r;


I could imagine some support for making it easier to construct a
client request from a server request and vice versa for a client
response to a server response. But it would also depend on how much
information you want to copy from the server request and from the
client response. At a minimum it requires some changes to the request
URI (host, port, anything else?).

For example one could support a message body writer that writes the
ClientResponse so you could return that directly.

In general i could see this behaviour:

   @Path("/search")
   @GET
   @Produces({"application/xml","application/json"})
   public Response search(@Context Proxy p) throws JAXBException {
       URI u = ...
       WebResource r = ...
       return p.request(r);
   }

Paul.