Search in sources :

Example 16 with DeferredContentProvider

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

the class AsyncIOServletTest method testOtherThreadOnAllDataRead.

@Test
public void testOtherThreadOnAllDataRead() throws Exception {
    String success = "SUCCESS";
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            assertScope();
            response.flushBuffer();
            AsyncContext async = request.startAsync();
            async.setTimeout(0);
            ServletInputStream input = request.getInputStream();
            ServletOutputStream output = response.getOutputStream();
            if (request.getDispatcherType() == DispatcherType.ERROR)
                throw new IllegalStateException();
            input.setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    assertScope();
                    async.start(() -> {
                        assertScope();
                        try {
                            sleep(1000);
                            if (!input.isReady())
                                throw new IllegalStateException();
                            if (input.read() != 'X')
                                throw new IllegalStateException();
                            if (!input.isReady())
                                throw new IllegalStateException();
                            if (input.read() != -1)
                                throw new IllegalStateException();
                        } catch (IOException x) {
                            throw new UncheckedIOException(x);
                        }
                    });
                }

                @Override
                public void onAllDataRead() throws IOException {
                    output.write(success.getBytes(StandardCharsets.UTF_8));
                    async.complete();
                }

                @Override
                public void onError(Throwable t) {
                    assertScope();
                    t.printStackTrace();
                    async.complete();
                }
            });
        }
    });
    byte[] data = "X".getBytes(StandardCharsets.UTF_8);
    CountDownLatch clientLatch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(content).timeout(5, TimeUnit.SECONDS).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                Response response = result.getResponse();
                String content = getContentAsString();
                if (response.getStatus() == HttpStatus.OK_200 && success.equals(content))
                    clientLatch.countDown();
            }
        }
    });
    sleep(100);
    content.offer(ByteBuffer.wrap(data));
    content.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) Matchers.containsString(org.hamcrest.Matchers.containsString) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) 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) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 17 with DeferredContentProvider

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

the class AsyncIOServletTest method testOnAllDataRead.

@Test
public void testOnAllDataRead() throws Exception {
    String success = "SUCCESS";
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            assertScope();
            response.flushBuffer();
            AsyncContext async = request.startAsync();
            async.setTimeout(5000);
            ServletInputStream in = request.getInputStream();
            ServletOutputStream out = response.getOutputStream();
            in.setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    assertScope();
                    try {
                        sleep(1000);
                        if (!in.isReady())
                            throw new IllegalStateException();
                        if (in.read() != 'X')
                            throw new IllegalStateException();
                        if (!in.isReady())
                            throw new IllegalStateException();
                        if (in.read() != -1)
                            throw new IllegalStateException();
                    } catch (IOException x) {
                        throw new UncheckedIOException(x);
                    }
                }

                @Override
                public void onAllDataRead() throws IOException {
                    assertScope();
                    out.write(success.getBytes(StandardCharsets.UTF_8));
                    async.complete();
                }

                @Override
                public void onError(Throwable t) {
                    assertScope();
                    t.printStackTrace();
                    async.complete();
                }
            });
        }
    });
    byte[] data = "X".getBytes(StandardCharsets.UTF_8);
    CountDownLatch clientLatch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider() {

        @Override
        public long getLength() {
            return data.length;
        }
    };
    client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(content).timeout(5, TimeUnit.SECONDS).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                Response response = result.getResponse();
                String content = getContentAsString();
                if (response.getStatus() == HttpStatus.OK_200 && success.equals(content))
                    clientLatch.countDown();
            }
        }
    });
    sleep(100);
    content.offer(ByteBuffer.wrap(data));
    content.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) Matchers.containsString(org.hamcrest.Matchers.containsString) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) 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) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 18 with DeferredContentProvider

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

the class AsyncRequestContentTest method testDeferredContent.

@Test
public void testDeferredContent() throws Exception {
    start(new ConsumeInputHandler());
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    CountDownLatch latch = new CountDownLatch(1);
    client.POST(newURI()).content(contentProvider).send(result -> {
        if (result.isSucceeded() && result.getResponse().getStatus() == HttpStatus.OK_200)
            latch.countDown();
    });
    contentProvider.offer(ByteBuffer.wrap(new byte[1]));
    contentProvider.close();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 19 with DeferredContentProvider

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

the class HttpClientStreamTest method testUploadWithDeferredContentProviderRacingWithSend.

@Test
public void testUploadWithDeferredContentProviderRacingWithSend() 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 DeferredContentProvider content = new DeferredContentProvider() {

        @Override
        public void setListener(Listener listener) {
            super.setListener(listener);
            // Simulate a concurrent call
            offer(ByteBuffer.wrap(data));
            close();
        }
    };
    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) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) 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) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 20 with DeferredContentProvider

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

the class HttpClientStreamTest method testUploadWithDeferredContentAvailableCallbacksNotifiedOnce.

@Test
public void testUploadWithDeferredContentAvailableCallbacksNotifiedOnce() 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(), new ByteArrayOutputStream());
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger succeeds = new AtomicInteger();
    try (DeferredContentProvider content = new DeferredContentProvider()) {
        // Make the content immediately available.
        content.offer(ByteBuffer.allocate(1024), new Callback() {

            @Override
            public void succeeded() {
                succeeds.incrementAndGet();
            }
        });
        client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(result -> {
            if (result.isSucceeded() && result.getResponse().getStatus() == 200)
                latch.countDown();
        });
    }
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(1, succeeds.get());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Test(org.junit.Test)

Aggregations

DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)46 Test (org.junit.Test)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 HttpServletResponse (javax.servlet.http.HttpServletResponse)34 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)28 ServletInputStream (javax.servlet.ServletInputStream)18 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)18 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)17 Request (org.eclipse.jetty.server.Request)16 Result (org.eclipse.jetty.client.api.Result)15 InterruptedIOException (java.io.InterruptedIOException)14 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)12 AsyncContext (javax.servlet.AsyncContext)11 Request (org.eclipse.jetty.client.api.Request)11 ByteBuffer (java.nio.ByteBuffer)10 Response (org.eclipse.jetty.client.api.Response)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 ReadListener (javax.servlet.ReadListener)7