Search in sources :

Example 31 with Result

use of org.eclipse.jetty.client.api.Result 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 32 with Result

use of org.eclipse.jetty.client.api.Result 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 33 with Result

use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.

the class HttpClientStreamTest method testDownload.

@Test
public void testDownload() throws Exception {
    final byte[] data = new byte[128 * 1024];
    byte value = 1;
    Arrays.fill(data, value);
    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);
            response.getOutputStream().write(data);
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    InputStream input = listener.getInputStream();
    Assert.assertNotNull(input);
    int length = 0;
    while (input.read() == value) {
        if (length % 100 == 0)
            Thread.sleep(1);
        ++length;
    }
    Assert.assertEquals(data.length, length);
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertNotNull(result);
    Assert.assertFalse(result.isFailed());
    Assert.assertSame(response, result.getResponse());
}
Also used : Request(org.eclipse.jetty.server.Request) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) 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) Test(org.junit.Test)

Example 34 with Result

use of org.eclipse.jetty.client.api.Result 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 35 with Result

use of org.eclipse.jetty.client.api.Result 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)

Aggregations

Result (org.eclipse.jetty.client.api.Result)100 Test (org.junit.Test)80 CountDownLatch (java.util.concurrent.CountDownLatch)79 HttpServletResponse (javax.servlet.http.HttpServletResponse)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)66 Response (org.eclipse.jetty.client.api.Response)62 IOException (java.io.IOException)60 ServletException (javax.servlet.ServletException)56 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)44 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)41 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)39 Request (org.eclipse.jetty.client.api.Request)31 ByteBuffer (java.nio.ByteBuffer)29 Request (org.eclipse.jetty.server.Request)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)24 InterruptedIOException (java.io.InterruptedIOException)21 ServletOutputStream (javax.servlet.ServletOutputStream)20 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)16