Search in sources :

Example 41 with Response

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

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

the class HttpClientStreamTest method testDownloadWithCloseEndOfContent.

@Test
public void testDownloadWithCloseEndOfContent() throws Exception {
    final byte[] data = new byte[1024];
    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);
            response.flushBuffer();
        }
    });
    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);
    for (byte datum : data) Assert.assertEquals(datum, input.read());
    // Read EOF
    Assert.assertEquals(-1, input.read());
    input.close();
    // Must not throw
    Assert.assertEquals(-1, input.read());
}
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) 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 43 with Response

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

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

the class HttpClientStreamTest method testInputStreamResponseListenerClosedWhileWaiting.

@Test
public void testInputStreamResponseListenerClosedWhileWaiting() throws Exception {
    byte[] chunk1 = new byte[] { 0, 1 };
    byte[] chunk2 = new byte[] { 2, 3 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setContentLength(chunk1.length + chunk2.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            output.flush();
            output.write(chunk2);
        }
    });
    CountDownLatch failedLatch = new CountDownLatch(1);
    CountDownLatch contentLatch = new CountDownLatch(1);
    InputStreamResponseListener listener = new InputStreamResponseListener() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            super.onContent(response, content, new Callback() {

                @Override
                public void failed(Throwable x) {
                    failedLatch.countDown();
                    callback.failed(x);
                }
            });
            contentLatch.countDown();
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    // Wait until we get some content.
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    // Close the stream.
    InputStream stream = listener.getInputStream();
    stream.close();
    // Make sure that the callback has been invoked.
    Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) 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) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Example 45 with Response

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

the class HttpClientStreamTest method testUploadWithConcurrentServerCloseClosesStream.

@Test
public void testUploadWithConcurrentServerCloseClosesStream() throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    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);
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            serverLatch.countDown();
        }
    });
    final AtomicBoolean commit = new AtomicBoolean();
    final CountDownLatch closeLatch = new CountDownLatch(1);
    InputStream stream = new InputStream() {

        @Override
        public int read() throws IOException {
            if (commit.get()) {
                try {
                    Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
                    connector.stop();
                    return 0;
                } catch (Throwable x) {
                    throw new IOException(x);
                }
            } else {
                return connector.isStopped() ? -1 : 0;
            }
        }

        @Override
        public void close() throws IOException {
            super.close();
            closeLatch.countDown();
        }
    };
    InputStreamContentProvider provider = new InputStreamContentProvider(stream, 1);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(provider).onRequestCommit(request -> commit.set(true)).send(result -> {
        Assert.assertTrue(result.isFailed());
        completeLatch.countDown();
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) Request(org.eclipse.jetty.server.Request) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) BufferUtil(org.eclipse.jetty.util.BufferUtil) Result(org.eclipse.jetty.client.api.Result) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Paths(java.nio.file.Paths) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)105 Test (org.junit.Test)89 HttpServletResponse (javax.servlet.http.HttpServletResponse)88 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)83 IOException (java.io.IOException)72 HttpServletRequest (javax.servlet.http.HttpServletRequest)69 ServletException (javax.servlet.ServletException)67 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)33 InputStream (java.io.InputStream)32 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 Request (org.eclipse.jetty.server.Request)26 Request (org.eclipse.jetty.client.api.Request)25 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)24 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22