Search in sources :

Example 1 with AsyncHandler

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

the class ZeroCopyFileTest method zeroCopyFileTest.

@Test(groups = "standalone")
public void zeroCopyFileTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
    File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
    tmp.deleteOnExit();
    try (AsyncHttpClient client = asyncHttpClient()) {
        try (FileOutputStream stream = new FileOutputStream(tmp)) {
            Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {

                public void onThrowable(Throwable t) {
                }

                public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                    stream.write(bodyPart.getBodyPartBytes());
                    return State.CONTINUE;
                }

                public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
                    return State.CONTINUE;
                }

                public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                    return State.CONTINUE;
                }

                public Response onCompleted() throws Exception {
                    return null;
                }
            }).get();
            assertNull(resp);
            assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncHandler(org.asynchttpclient.AsyncHandler) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) BasicHttpsTest(org.asynchttpclient.BasicHttpsTest) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 2 with AsyncHandler

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

the class ZeroCopyFileTest method zeroCopyFileWithBodyManipulationTest.

@Test(groups = "standalone")
public void zeroCopyFileWithBodyManipulationTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
    File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
    tmp.deleteOnExit();
    try (AsyncHttpClient client = asyncHttpClient()) {
        try (FileOutputStream stream = new FileOutputStream(tmp)) {
            Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {

                public void onThrowable(Throwable t) {
                }

                public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                    stream.write(bodyPart.getBodyPartBytes());
                    if (bodyPart.getBodyPartBytes().length == 0) {
                        return State.ABORT;
                    }
                    return State.CONTINUE;
                }

                public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
                    return State.CONTINUE;
                }

                public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                    return State.CONTINUE;
                }

                public Response onCompleted() throws Exception {
                    return null;
                }
            }).get();
            assertNull(resp);
            assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncHandler(org.asynchttpclient.AsyncHandler) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) BasicHttpsTest(org.asynchttpclient.BasicHttpsTest) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)2 AsyncHandler (org.asynchttpclient.AsyncHandler)2 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)2 BasicHttpsTest (org.asynchttpclient.BasicHttpsTest)2 HttpResponseBodyPart (org.asynchttpclient.HttpResponseBodyPart)2 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)2 HttpResponseStatus (org.asynchttpclient.HttpResponseStatus)2 Response (org.asynchttpclient.Response)2 Test (org.testng.annotations.Test)2