Search in sources :

Example 1 with HttpResponseBodyPart

use of com.ning.http.client.HttpResponseBodyPart in project jersey by jersey.

the class GrizzlyConnector method apply.

@Override
public Future<?> apply(final ClientRequest request, final AsyncConnectorCallback callback) {
    final Request connectorRequest = translate(request);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest);
    final ByteBufferInputStream entityStream = new ByteBufferInputStream();
    final AtomicBoolean callbackInvoked = new AtomicBoolean(false);
    Throwable failure;
    try {
        return grizzlyClient.executeRequest(connectorRequest, new AsyncHandler<Void>() {

            private volatile HttpResponseStatus status = null;

            @Override
            public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
                status = responseStatus;
                return STATE.CONTINUE;
            }

            @Override
            public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                if (!callbackInvoked.compareAndSet(false, true)) {
                    return STATE.ABORT;
                }
                HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, request.getHeaders(), GrizzlyConnector.this.getClass().getName());
                // hand-off to grizzly's application thread pool for response processing
                processResponse(new Runnable() {

                    @Override
                    public void run() {
                        callback.response(translate(request, status, headers, entityStream));
                    }
                });
                return STATE.CONTINUE;
            }

            @Override
            public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                entityStream.put(bodyPart.getBodyByteBuffer());
                return STATE.CONTINUE;
            }

            @Override
            public Void onCompleted() throws Exception {
                entityStream.closeQueue();
                return null;
            }

            @Override
            public void onThrowable(Throwable t) {
                entityStream.closeQueue(t);
                if (callbackInvoked.compareAndSet(false, true)) {
                    t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t;
                    callback.failure(t);
                }
            }
        });
    } catch (Throwable t) {
        failure = t;
    }
    if (callbackInvoked.compareAndSet(false, true)) {
        callback.failure(failure);
    }
    CompletableFuture<Object> future = new CompletableFuture<>();
    future.completeExceptionally(failure);
    return future;
}
Also used : HttpResponseHeaders(com.ning.http.client.HttpResponseHeaders) HttpResponseStatus(com.ning.http.client.HttpResponseStatus) Request(com.ning.http.client.Request) ClientRequest(org.glassfish.jersey.client.ClientRequest) ByteBufferInputStream(org.glassfish.jersey.internal.util.collection.ByteBufferInputStream) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with HttpResponseBodyPart

use of com.ning.http.client.HttpResponseBodyPart in project jersey by jersey.

the class GrizzlyConnector method apply.

/**
     * Sends the {@link javax.ws.rs.core.Request} via Grizzly transport and returns the {@link javax.ws.rs.core.Response}.
     *
     * @param request Jersey client request to be sent.
     * @return received response.
     */
@Override
public ClientResponse apply(final ClientRequest request) {
    final Request connectorRequest = translate(request);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest);
    final CompletableFuture<ClientResponse> responseFuture = new CompletableFuture<>();
    final ByteBufferInputStream entityStream = new ByteBufferInputStream();
    final AtomicBoolean futureSet = new AtomicBoolean(false);
    try {
        grizzlyClient.executeRequest(connectorRequest, new AsyncHandler<Void>() {

            private volatile HttpResponseStatus status = null;

            @Override
            public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
                status = responseStatus;
                return STATE.CONTINUE;
            }

            @Override
            public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                if (!futureSet.compareAndSet(false, true)) {
                    return STATE.ABORT;
                }
                HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, request.getHeaders(), GrizzlyConnector.this.getClass().getName());
                responseFuture.complete(translate(request, this.status, headers, entityStream));
                return STATE.CONTINUE;
            }

            @Override
            public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                entityStream.put(bodyPart.getBodyByteBuffer());
                return STATE.CONTINUE;
            }

            @Override
            public Void onCompleted() throws Exception {
                entityStream.closeQueue();
                return null;
            }

            @Override
            public void onThrowable(Throwable t) {
                entityStream.closeQueue(t);
                if (futureSet.compareAndSet(false, true)) {
                    t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t;
                    responseFuture.completeExceptionally(t);
                }
            }
        });
        return responseFuture.get();
    } catch (ExecutionException ex) {
        Throwable e = ex.getCause() == null ? ex : ex.getCause();
        throw new ProcessingException(e.getMessage(), e);
    } catch (InterruptedException ex) {
        throw new ProcessingException(ex.getMessage(), ex);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) HttpResponseHeaders(com.ning.http.client.HttpResponseHeaders) HttpResponseStatus(com.ning.http.client.HttpResponseStatus) Request(com.ning.http.client.Request) ClientRequest(org.glassfish.jersey.client.ClientRequest) ByteBufferInputStream(org.glassfish.jersey.internal.util.collection.ByteBufferInputStream) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) ProcessingException(javax.ws.rs.ProcessingException)

Example 3 with HttpResponseBodyPart

use of com.ning.http.client.HttpResponseBodyPart in project cdap by caskdata.

the class NettyRouterPipelineTest method testChunkRequestSuccess.

@Test
public void testChunkRequestSuccess() throws Exception {
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    byte[] requestBody = generatePostData();
    final Request request = new RequestBuilder("POST").setUrl(String.format("http://%s:%d%s", HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME), "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

        @Override
        public Void onCompleted(Response response) throws Exception {
            return null;
        }

        @Override
        public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            //TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
            content.writeTo(byteArrayOutputStream);
            return super.onBodyPartReceived(content);
        }
    });
    future.get();
    Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Response(com.ning.http.client.Response) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 4 with HttpResponseBodyPart

use of com.ning.http.client.HttpResponseBodyPart in project cdap by caskdata.

the class NettyRouterTestBase method testUpload.

@Test
public void testUpload() throws Exception {
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    byte[] requestBody = generatePostData();
    final Request request = new RequestBuilder("POST").setUrl(resolveURI(DEFAULT_SERVICE, "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

        @Override
        public Void onCompleted(Response response) throws Exception {
            return null;
        }

        @Override
        public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            //TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
            content.writeTo(byteArrayOutputStream);
            return super.onBodyPartReceived(content);
        }
    });
    future.get();
    Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(com.ning.http.client.Response) HttpResponse(org.apache.http.HttpResponse) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Aggregations

HttpResponseBodyPart (com.ning.http.client.HttpResponseBodyPart)4 Request (com.ning.http.client.Request)4 IOException (java.io.IOException)4 AsyncHttpClient (com.ning.http.client.AsyncHttpClient)2 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)2 HttpResponseHeaders (com.ning.http.client.HttpResponseHeaders)2 HttpResponseStatus (com.ning.http.client.HttpResponseStatus)2 RequestBuilder (com.ning.http.client.RequestBuilder)2 Response (com.ning.http.client.Response)2 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ProcessingException (javax.ws.rs.ProcessingException)2 ClientRequest (org.glassfish.jersey.client.ClientRequest)2 ByteBufferInputStream (org.glassfish.jersey.internal.util.collection.ByteBufferInputStream)2 Test (org.junit.Test)2 URISyntaxException (java.net.URISyntaxException)1 HttpResponse (org.apache.http.HttpResponse)1