Search in sources :

Example 11 with BufferingResponseListener

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

the class HttpClientContinueTest method test_Expect100Continue_WithInitialAndDeferredContent_Respond100Continue.

@Slow
@Test
public void test_Expect100Continue_WithInitialAndDeferredContent_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, 7 };
    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 CountDownLatch latch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(chunk1));
    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();
        }
    });
    Thread.sleep(1000);
    content.offer(ByteBuffer.wrap(chunk2));
    content.close();
    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) 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) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 12 with BufferingResponseListener

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_RespondError.

private void test_Expect100Continue_WithContent_RespondError(final int error) throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.sendError(error);
        }
    });
    byte[] content1 = new byte[10240];
    byte[] content2 = new byte[16384];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content1, content2)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNotNull(result.getRequestFailure());
            Assert.assertNull(result.getResponseFailure());
            byte[] content = getContent();
            Assert.assertNotNull(content);
            Assert.assertTrue(content.length > 0);
            Assert.assertEquals(error, result.getResponse().getStatus());
            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) 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)

Example 13 with BufferingResponseListener

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

the class HttpClientStreamTest method testUploadWithDeferredContentProviderRacingWithIterator.

@Test
public void testUploadWithDeferredContentProviderRacingWithIterator() 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 CountDownLatch latch = new CountDownLatch(1);
    final byte[] data = new byte[512];
    final AtomicReference<DeferredContentProvider> contentRef = new AtomicReference<>();
    final DeferredContentProvider content = new DeferredContentProvider() {

        @Override
        public Iterator<ByteBuffer> iterator() {
            return new Iterator<ByteBuffer>() {

                // Data for the deferred content iterator:
                // [0] => deferred
                // [1] => deferred
                // [2] => data
                private final byte[][] iteratorData = new byte[3][];

                private final AtomicInteger index = new AtomicInteger();

                {
                    iteratorData[0] = null;
                    iteratorData[1] = null;
                    iteratorData[2] = data;
                }

                @Override
                public boolean hasNext() {
                    return index.get() < iteratorData.length;
                }

                @Override
                public ByteBuffer next() {
                    byte[] chunk = iteratorData[index.getAndIncrement()];
                    ByteBuffer result = chunk == null ? null : ByteBuffer.wrap(chunk);
                    if (index.get() < iteratorData.length) {
                        contentRef.get().offer(result == null ? BufferUtil.EMPTY_BUFFER : result);
                        contentRef.get().close();
                    }
                    return result;
                }

                @Override
                public void remove() {
                }
            };
        }
    };
    contentRef.set(content);
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded() && result.getResponse().getStatus() == 200 && Arrays.equals(data, getContent()))
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Iterator(java.util.Iterator) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 14 with BufferingResponseListener

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

the class AsyncIOServletTest method testWriteFromOnDataAvailable.

@Test
public void testWriteFromOnDataAvailable() throws Exception {
    Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
    CountDownLatch writeLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            request.getInputStream().setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    ServletInputStream input = request.getInputStream();
                    ServletOutputStream output = response.getOutputStream();
                    while (input.isReady()) {
                        byte[] buffer = new byte[512];
                        int read = input.read(buffer);
                        if (read < 0) {
                            asyncContext.complete();
                            break;
                        }
                        if (output.isReady())
                            output.write(buffer, 0, read);
                        else
                            Assert.fail();
                    }
                }

                @Override
                public void onAllDataRead() throws IOException {
                    asyncContext.complete();
                }

                @Override
                public void onError(Throwable t) {
                    errors.offer(t);
                }
            });
            response.getOutputStream().setWriteListener(new WriteListener() {

                @Override
                public void onWritePossible() throws IOException {
                    writeLatch.countDown();
                }

                @Override
                public void onError(Throwable t) {
                    errors.offer(t);
                }
            });
        }
    });
    String content = "0123456789ABCDEF";
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    contentProvider.offer(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                Response response = result.getResponse();
                assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
                assertThat(getContentAsString(), Matchers.equalTo(content));
                assertThat(errors, Matchers.hasSize(0));
                clientLatch.countDown();
            }
        }
    });
    assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
    contentProvider.close();
    assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ReadListener(javax.servlet.ReadListener) 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) ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) WriteListener(javax.servlet.WriteListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 15 with BufferingResponseListener

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

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccess.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccess() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).content(new InputStreamContentProvider(new ByteArrayInputStream(content))).timeout(2 * timeout, TimeUnit.MILLISECONDS);
    request.send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertArrayEquals(content, getContent());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
    TimeUnit.MILLISECONDS.sleep(2 * timeout);
    Assert.assertNull(request.getAbortCause());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

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