Search in sources :

Example 1 with TransferCompletionHandler

use of org.asynchttpclient.handler.TransferCompletionHandler in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method writeRequest.

public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
    NettyRequest nettyRequest = future.getNettyRequest();
    HttpRequest httpRequest = nettyRequest.getHttpRequest();
    AsyncHandler<T> handler = future.getAsyncHandler();
    // we just let it go and the channelInactive do its work
    if (!Channels.isChannelValid(channel))
        return;
    try {
        if (handler instanceof TransferCompletionHandler)
            configureTransferAdapter(handler, httpRequest);
        boolean writeBody = !future.isDontWriteBodyBecauseExpectContinue() && httpRequest.method() != HttpMethod.CONNECT && nettyRequest.getBody() != null;
        if (!future.isHeadersAlreadyWrittenOnContinue()) {
            if (handler instanceof AsyncHandlerExtensions) {
                AsyncHandlerExtensions.class.cast(handler).onRequestSend(nettyRequest);
            }
            // if the request has a body, we want to track progress
            if (writeBody) {
                ChannelProgressivePromise promise = channel.newProgressivePromise();
                ChannelFuture f = channel.write(httpRequest, promise);
                f.addListener(new WriteProgressListener(future, true, 0L));
            } else {
                // we can just track write completion
                ChannelPromise promise = channel.newPromise();
                ChannelFuture f = channel.writeAndFlush(httpRequest, promise);
                f.addListener(new WriteCompleteListener(future));
            }
        }
        if (writeBody)
            nettyRequest.getBody().write(channel, future);
        // don't bother scheduling read timeout if channel became invalid
        if (Channels.isChannelValid(channel))
            scheduleReadTimeout(future);
    } catch (Exception e) {
        LOGGER.error("Can't write request", e);
        abort(channel, future, e);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) ChannelFuture(io.netty.channel.ChannelFuture) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) ChannelPromise(io.netty.channel.ChannelPromise) AsyncHandlerExtensions(org.asynchttpclient.handler.AsyncHandlerExtensions) FilterException(org.asynchttpclient.filter.FilterException) IOException(java.io.IOException) RemotelyClosedException(org.asynchttpclient.exception.RemotelyClosedException) EXPECT(io.netty.handler.codec.http.HttpHeaderNames.EXPECT) ChannelProgressivePromise(io.netty.channel.ChannelProgressivePromise)

Example 2 with TransferCompletionHandler

use of org.asynchttpclient.handler.TransferCompletionHandler in project async-http-client by AsyncHttpClient.

the class TransferListenerTest method basicGetTest.

@Test(groups = "standalone")
public void basicGetTest() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
        final AtomicReference<Throwable> throwable = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
        final AtomicReference<byte[]> bb = new AtomicReference<>();
        final AtomicBoolean completed = new AtomicBoolean(false);
        TransferCompletionHandler tl = new TransferCompletionHandler();
        tl.addTransferListener(new TransferListener() {

            public void onRequestHeadersSent(HttpHeaders headers) {
                hSent.set(headers);
            }

            public void onResponseHeadersReceived(HttpHeaders headers) {
                hRead.set(headers);
            }

            public void onBytesReceived(byte[] b) {
                if (b.length != 0)
                    bb.set(b);
            }

            public void onBytesSent(long amount, long current, long total) {
            }

            public void onRequestResponseCompleted() {
                completed.set(true);
            }

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = c.prepareGet(getTargetUrl()).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertNull(bb.get());
        assertNull(throwable.get());
    }
}
Also used : TransferListener(org.asynchttpclient.handler.TransferListener) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 3 with TransferCompletionHandler

use of org.asynchttpclient.handler.TransferCompletionHandler in project async-http-client by AsyncHttpClient.

the class TransferListenerTest method basicPutFileTest.

@Test(groups = "standalone")
public void basicPutFileTest() throws Exception {
    final AtomicReference<Throwable> throwable = new AtomicReference<>();
    final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
    final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
    final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
    final AtomicLong bbSentLenght = new AtomicLong(0L);
    final AtomicBoolean completed = new AtomicBoolean(false);
    File file = createTempFile(1024 * 100 * 10);
    int timeout = (int) (file.length() / 1000);
    try (AsyncHttpClient client = asyncHttpClient(config().setConnectTimeout(timeout))) {
        TransferCompletionHandler tl = new TransferCompletionHandler();
        tl.addTransferListener(new TransferListener() {

            public void onRequestHeadersSent(HttpHeaders headers) {
                hSent.set(headers);
            }

            public void onResponseHeadersReceived(HttpHeaders headers) {
                hRead.set(headers);
            }

            public void onBytesReceived(byte[] b) {
                bbReceivedLenght.addAndGet(b.length);
            }

            public void onBytesSent(long amount, long current, long total) {
                bbSentLenght.addAndGet(amount);
            }

            public void onRequestResponseCompleted() {
                completed.set(true);
            }

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = client.preparePut(getTargetUrl()).setBody(file).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
        assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
    }
}
Also used : TransferListener(org.asynchttpclient.handler.TransferListener) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 4 with TransferCompletionHandler

use of org.asynchttpclient.handler.TransferCompletionHandler in project async-http-client by AsyncHttpClient.

the class TransferListenerTest method basicPutFileBodyGeneratorTest.

@Test(groups = "standalone")
public void basicPutFileBodyGeneratorTest() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient()) {
        final AtomicReference<Throwable> throwable = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
        final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
        final AtomicLong bbSentLenght = new AtomicLong(0L);
        final AtomicBoolean completed = new AtomicBoolean(false);
        File file = createTempFile(1024 * 100 * 10);
        TransferCompletionHandler tl = new TransferCompletionHandler();
        tl.addTransferListener(new TransferListener() {

            public void onRequestHeadersSent(HttpHeaders headers) {
                hSent.set(headers);
            }

            public void onResponseHeadersReceived(HttpHeaders headers) {
                hRead.set(headers);
            }

            public void onBytesReceived(byte[] b) {
                bbReceivedLenght.addAndGet(b.length);
            }

            public void onBytesSent(long amount, long current, long total) {
                bbSentLenght.addAndGet(amount);
            }

            public void onRequestResponseCompleted() {
                completed.set(true);
            }

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = client.preparePut(getTargetUrl()).setBody(new FileBodyGenerator(file)).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
        assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
    }
}
Also used : TransferListener(org.asynchttpclient.handler.TransferListener) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

TransferCompletionHandler (org.asynchttpclient.handler.TransferCompletionHandler)4 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)3 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)3 Response (org.asynchttpclient.Response)3 TransferListener (org.asynchttpclient.handler.TransferListener)3 Test (org.testng.annotations.Test)3 File (java.io.File)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 TestUtils.createTempFile (org.asynchttpclient.test.TestUtils.createTempFile)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelProgressivePromise (io.netty.channel.ChannelProgressivePromise)1 ChannelPromise (io.netty.channel.ChannelPromise)1 EXPECT (io.netty.handler.codec.http.HttpHeaderNames.EXPECT)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 IOException (java.io.IOException)1