Search in sources :

Example 1 with BodyDeferringInputStream

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

the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrickWithCloseConnectionAndRetry.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void deferredInputStreamTrickWithCloseConnectionAndRetry() throws Throwable {
    try (AsyncHttpClient client = asyncHttpClient(config().setMaxRequestRetry(1).setRequestTimeout(10000).build())) {
        BoundRequestBuilder r = client.prepareGet(getTargetUrl()).addHeader("X-CLOSE-CONNECTION", Boolean.TRUE.toString());
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
        Future<Response> f = r.execute(bdah);
        BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis);
        Response resp = is.getAsapResponse();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
        // "consume" the body, but our code needs input stream
        CountingOutputStream cos = new CountingOutputStream();
        try {
            try {
                copy(is, cos);
            } finally {
                is.close();
                cos.close();
            }
        } catch (IOException e) {
            throw e.getCause();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 2 with BodyDeferringInputStream

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

the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrickWithFailure.

@Test(expectedExceptions = RemotelyClosedException.class)
public void deferredInputStreamTrickWithFailure() throws Throwable {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet(getTargetUrl()).addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
        Future<Response> f = r.execute(bdah);
        BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis);
        Response resp = is.getAsapResponse();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
        // "consume" the body, but our code needs input stream
        CountingOutputStream cos = new CountingOutputStream();
        try {
            try {
                copy(is, cos);
            } finally {
                is.close();
                cos.close();
            }
        } catch (IOException e) {
            throw e.getCause();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 3 with BodyDeferringInputStream

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

the class BodyDeferringAsyncHandlerTest method testPipedStreams.

@Test
public void testPipedStreams() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        PipedOutputStream pout = new PipedOutputStream();
        try (PipedInputStream pin = new PipedInputStream(pout)) {
            BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler(pout);
            ListenableFuture<Response> respFut = client.prepareGet(getTargetUrl()).execute(handler);
            Response resp = handler.getResponse();
            if (resp.getStatusCode() == 200) {
                try (BodyDeferringInputStream is = new BodyDeferringInputStream(respFut, handler, pin)) {
                    String body = IOUtils.toString(is, StandardCharsets.UTF_8);
                    assertTrue(body.contains("ABCDEF"));
                }
            } else {
                throw new IOException("HTTP error " + resp.getStatusCode());
            }
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 4 with BodyDeferringInputStream

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

the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrick.

@Test
public void deferredInputStreamTrick() throws IOException, InterruptedException {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet(getTargetUrl());
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
        Future<Response> f = r.execute(bdah);
        BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis);
        Response resp = is.getAsapResponse();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
        // "consume" the body, but our code needs input stream
        CountingOutputStream cos = new CountingOutputStream();
        try {
            copy(is, cos);
        } finally {
            is.close();
            cos.close();
        }
        // now we don't need to be polite, since consuming and closing
        // BodyDeferringInputStream does all.
        // it all should be here now
        assertEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)4 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)4 Response (org.asynchttpclient.Response)4 BodyDeferringInputStream (org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream)4 Test (org.testng.annotations.Test)4 IOException (java.io.IOException)3 BoundRequestBuilder (org.asynchttpclient.BoundRequestBuilder)3