Search in sources :

Example 16 with ClientResponse

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;
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 17 with ClientResponse

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);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) ClientRequest(org.glassfish.jersey.client.ClientRequest) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 18 with ClientResponse

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;
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncConnectorCallback(org.glassfish.jersey.client.spi.AsyncConnectorCallback) IOException(java.io.IOException)

Example 19 with ClientResponse

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;
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) ClientResponse(org.glassfish.jersey.client.ClientResponse) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 20 with ClientResponse

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;
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

ClientResponse (org.glassfish.jersey.client.ClientResponse)21 ProcessingException (javax.ws.rs.ProcessingException)10 IOException (java.io.IOException)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 Response (javax.ws.rs.core.Response)7 ClientRequest (org.glassfish.jersey.client.ClientRequest)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)3 Header (org.apache.http.Header)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 Test (org.junit.Test)3 URI (java.net.URI)2 List (java.util.List)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Client (javax.ws.rs.client.Client)2 HttpEntity (org.apache.http.HttpEntity)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2