Search in sources :

Example 6 with BufferingResponseListener

use of org.eclipse.jetty.client.util.BufferingResponseListener in project jetty.project by eclipse.

the class HttpClientStreamTest method testBigUploadWithOutputStreamFromInputStream.

@Test
public void testBigUploadWithOutputStreamFromInputStream() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] data = new byte[16 * 1024 * 1024];
    new Random().nextBytes(data);
    final CountDownLatch latch = new CountDownLatch(1);
    OutputStreamContentProvider content = new OutputStreamContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener(data.length) {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            Assert.assertEquals(200, result.getResponse().getStatus());
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    // Make sure we provide the content *after* the request has been "sent".
    Thread.sleep(1000);
    try (InputStream input = new ByteArrayInputStream(data);
        OutputStream output = content.getOutputStream()) {
        byte[] buffer = new byte[1024];
        while (true) {
            int read = input.read(buffer);
            if (read < 0)
                break;
            output.write(buffer, 0, read);
        }
    }
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 7 with BufferingResponseListener

use of org.eclipse.jetty.client.util.BufferingResponseListener in project jetty.project by eclipse.

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue.

@Slow
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue() throws Exception {
    final long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            try {
                TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    client.setIdleTimeout(idleTimeout);
    byte[] content = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNotNull(result.getRequestFailure());
            Assert.assertNotNull(result.getResponseFailure());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * idleTimeout, TimeUnit.MILLISECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 8 with BufferingResponseListener

use of org.eclipse.jetty.client.util.BufferingResponseListener in project jetty.project by eclipse.

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithRedirect.

@Test
public void test_Expect100Continue_WithContent_WithRedirect() throws Exception {
    final String data = "success";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (request.getRequestURI().endsWith("/done")) {
                response.getOutputStream().print(data);
            } else {
                // Send 100-Continue and consume the content
                IO.copy(request.getInputStream(), new ByteArrayOutputStream());
                // Send a redirect
                response.sendRedirect("/done");
            }
        }
    });
    byte[] content = new byte[10240];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).method(HttpMethod.POST).path("/continue").header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertEquals(200, result.getResponse().getStatus());
            Assert.assertEquals(data, getContentAsString());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 9 with BufferingResponseListener

use of org.eclipse.jetty.client.util.BufferingResponseListener in project jetty.project by eclipse.

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_After100Continue.

@Slow
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_After100Continue() throws Exception {
    final long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and consume the content
            IO.copy(request.getInputStream(), new ByteArrayOutputStream());
            try {
                TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    client.setIdleTimeout(idleTimeout);
    byte[] content = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNull(result.getRequestFailure());
            Assert.assertNotNull(result.getResponseFailure());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * idleTimeout, TimeUnit.MILLISECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 10 with BufferingResponseListener

use of org.eclipse.jetty.client.util.BufferingResponseListener in project jetty.project by eclipse.

the class HttpClientContinueTest method test_Expect100Continue_WithInitialAndConcurrentDeferredContent_Respond100Continue.

@Test
public void test_Expect100Continue_WithInitialAndConcurrentDeferredContent_Respond100Continue() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and echo the content
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] chunk1 = new byte[] { 0, 1, 2, 3 };
    final byte[] chunk2 = new byte[] { 4, 5, 6 };
    final byte[] data = new byte[chunk1.length + chunk2.length];
    System.arraycopy(chunk1, 0, data, 0, chunk1.length);
    System.arraycopy(chunk2, 0, data, chunk1.length, chunk2.length);
    final DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(chunk1));
    client.getProtocolHandlers().put(new ContinueProtocolHandler() {

        @Override
        public Response.Listener getResponseListener() {
            return new ContinueListener() {

                @Override
                public void onHeaders(Response response) {
                    super.onHeaders(response);
                    content.offer(ByteBuffer.wrap(chunk2));
                    content.close();
                }
            };
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ContinueProtocolHandler(org.eclipse.jetty.client.ContinueProtocolHandler) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Aggregations

Result (org.eclipse.jetty.client.api.Result)28 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 HttpServletRequest (javax.servlet.http.HttpServletRequest)25 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)24 Test (org.junit.Test)24 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)17 Request (org.eclipse.jetty.server.Request)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)12 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)12 InterruptedIOException (java.io.InterruptedIOException)11 Response (org.eclipse.jetty.client.api.Response)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Random (java.util.Random)7 ServletInputStream (javax.servlet.ServletInputStream)7 ServletOutputStream (javax.servlet.ServletOutputStream)7 HttpServlet (javax.servlet.http.HttpServlet)7 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)7