use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class ApacheConnector method apply.
@Override
public Future<?> apply(final ClientRequest request, final AsyncConnectorCallback callback) {
try {
ClientResponse response = apply(request);
callback.response(response);
return CompletableFuture.completedFuture(response);
} catch (Throwable t) {
callback.failure(t);
CompletableFuture<Object> future = new CompletableFuture<>();
future.completeExceptionally(t);
return future;
}
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class EncodingFilterTest method testClosingClientResponseStreamRetrievedByValueOnError.
/**
* Reproducer for JERSEY-2028.
*
* @see #testClosingClientResponseStreamRetrievedByResponseOnError
*/
@Test
public void testClosingClientResponseStreamRetrievedByValueOnError() {
final TestInputStream responseStream = new TestInputStream();
Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector() {
@Override
public ClientResponse apply(ClientRequest requestContext) throws ProcessingException {
final ClientResponse responseContext = new ClientResponse(Response.Status.OK, requestContext);
responseContext.header(CONTENT_ENCODING, "gzip");
responseContext.setEntityStream(responseStream);
return responseContext;
}
}).register(new EncodingFeature(GZipEncoder.class, DeflateEncoder.class)));
try {
client.target(UriBuilder.fromUri("/").build()).request().get(String.class);
fail("Exception caused by invalid gzip stream expected.");
} catch (ProcessingException ex) {
assertTrue("Response input stream not closed when exception is thrown.", responseStream.isClosed);
}
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class JdkConnector method apply.
@Override
public Future<?> apply(final ClientRequest request, final AsyncConnectorCallback callback) {
final CompletableFuture<ClientResponse> responseFuture = new CompletableFuture<>();
// just so we don't have to drag around both the future and callback
final AsyncConnectorCallback internalCallback = new AsyncConnectorCallback() {
@Override
public void response(ClientResponse response) {
callback.response(response);
responseFuture.complete(response);
}
@Override
public void failure(Throwable failure) {
callback.failure(failure);
responseFuture.completeExceptionally(failure);
}
};
final HttpRequest httpRequest = createHttpRequest(request);
if (httpRequest.getBodyMode() == HttpRequest.BodyMode.BUFFERED) {
writeBufferedEntity(request, httpRequest, internalCallback);
}
if (httpRequest.getBodyMode() == HttpRequest.BodyMode.BUFFERED || httpRequest.getBodyMode() == HttpRequest.BodyMode.NONE) {
send(request, httpRequest, internalCallback);
}
if (httpRequest.getBodyMode() == HttpRequest.BodyMode.CHUNKED) {
/* We wait with sending the request header until the body stream has been touched.
This is because of javax.ws.rs.ext.MessageBodyWriter, which says:
"The message header map is mutable but any changes must be made before writing to the output stream since
the headers will be flushed prior to writing the message body"
This means that the headers can change until body output stream is used.
*/
final InterceptingOutputStream bodyStream = new InterceptingOutputStream(httpRequest.getBodyStream(), // send the prepared request when the stream is touched for the first time
() -> send(request, httpRequest, internalCallback));
request.setStreamProvider(contentLength -> bodyStream);
try {
request.writeEntity();
} catch (IOException e) {
internalCallback.failure(e);
}
}
return responseFuture;
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class JdkConnector method translateResponse.
private ClientResponse translateResponse(final ClientRequest requestContext, final HttpResponse httpResponse, URI requestUri) {
Response.StatusType statusType = new Response.StatusType() {
@Override
public int getStatusCode() {
return httpResponse.getStatusCode();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf(httpResponse.getStatusCode());
}
@Override
public String getReasonPhrase() {
return httpResponse.getReasonPhrase();
}
};
ClientResponse responseContext = new ClientResponse(statusType, requestContext, requestUri);
Map<String, List<String>> headers = httpResponse.getHeaders();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
responseContext.getHeaders().add(entry.getKey(), value);
}
}
responseContext.setEntityStream(httpResponse.getBodyStream());
return responseContext;
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class LoopBackConnector method _apply.
private ClientResponse _apply(final ClientRequest request) {
checkNotClosed();
final ClientResponse response = new ClientResponse(LOOPBACK_STATUS, request);
// Headers.
response.headers(HeaderUtils.asStringHeaders(request.getHeaders()));
// Entity.
if (request.hasEntity()) {
response.setEntityStream(new ByteArrayInputStream(bufferEntity(request)));
}
return response;
}
Aggregations