简体   繁体   中英

Jersey 2 - return Client's Response from resource method

I built a reverse proxy (P) to an upstream server (U). A client application (C) will issue a request to P, which will in turn issue a request to U, and the result returned by U should be returned to the client C by proxy P.

When I write the code in P like this (I want the proxy to be as generic as possible, and support multiple result types):

Client client = // get the client
Invocation.Builder builder = // configure the call to U
return builder.get(InputStream.class);

it works for both JSON and binary data, the result is returned, but the Content-Type header is always set to application/octet-stream , which is wrong. I could check the result from U for the type and set it to in the response from my proxy P, but then I would have to mess around with error handling etc. whereas when I just return the InputStream and an error occurs, the builder.get() method throws an exception which is then propagated to the client.

I would actually like to just take the Response returned by U and use it as the return value of P, like this:

Client client = // get the client
Invocation.Builder builder = // configure the call to U
return builder.get(); // returns Response

the client C, in my case a Python 3 requests application, get the following error:

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

I tried the following code:

Response upstreamResponse = client./* code code */.get();
upstreamResponse.bufferEntity();
return Response.fromResponse(upstreamResponse);

but, alas, the result is the same.

What am I missing?

I would have expected the proxy to pass the content type through (and maybe other things like content-length and status). So it would look a bit more like:

Response upstreamResponse = client./* code code */.get();
upstreamResponse.bufferEntity();
return Response.status(upstreamResponse.status())
               .type(upstreamResponse.getMediaType()
                // and so on

Realistically, you may or may not want many of the things from the upstreamResponse header too - what about Cookies for example?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM