Search in sources :

Example 26 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientIdleTimeoutTest method testRequestIdleTimeout.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals("/timeout")) {
                AsyncContext asyncContext = request.startAsync();
                asyncContext.setTimeout(0);
            }
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).path("/timeout").idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    Assert.assertTrue(latch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    // Verify that after the timeout we can make another request.
    ContentResponse response = client.newRequest(newURI()).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 27 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientStreamTest method testInputStreamResponseListenerClosedBeforeReading.

@Test(expected = AsynchronousCloseException.class)
public void testInputStreamResponseListenerClosedBeforeReading() 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());
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    InputStream stream = listener.getInputStream();
    // Close the stream immediately.
    stream.close();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(new BytesContentProvider(new byte[] { 0, 1, 2, 3 })).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    // Throws
    stream.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) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) 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 28 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler 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 29 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientStreamTest method testDownloadWithCloseBeforeContent.

@Test(expected = AsynchronousCloseException.class)
public void testDownloadWithCloseBeforeContent() throws Exception {
    final byte[] data = new byte[128 * 1024];
    byte value = 3;
    Arrays.fill(data, value);
    final CountDownLatch latch = 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);
            response.flushBuffer();
            try {
                Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                throw new InterruptedIOException();
            }
            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);
    input.close();
    latch.countDown();
    // Throws
    input.read();
}
Also used : Request(org.eclipse.jetty.server.Request) InterruptedIOException(java.io.InterruptedIOException) 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) CountDownLatch(java.util.concurrent.CountDownLatch) 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 30 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientTest method testRequestAfterFailedRequest.

@Test
public void testRequestAfterFailedRequest() throws Exception {
    int length = FlowControlStrategy.DEFAULT_WINDOW_SIZE;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                response.getOutputStream().write(new byte[length]);
            } catch (IOException e) {
            }
        }
    });
    // Make a request with a large enough response buffer.
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
    FutureResponseListener listener = new FutureResponseListener(request, length);
    request.send(listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), 200);
    // Make a request with a small response buffer, should fail.
    try {
        request = client.newRequest(newURI());
        listener = new FutureResponseListener(request, length / 10);
        request.send(listener);
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertThat(x.getMessage(), Matchers.containsString("Buffering capacity exceeded"));
    }
    // Verify that we can make another request.
    request = client.newRequest(newURI());
    listener = new FutureResponseListener(request, length);
    request.send(listener);
    response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), 200);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) 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) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Aggregations

AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)214 HttpServletRequest (javax.servlet.http.HttpServletRequest)211 HttpServletResponse (javax.servlet.http.HttpServletResponse)211 IOException (java.io.IOException)203 ServletException (javax.servlet.ServletException)203 Test (org.junit.Test)183 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 Request (org.eclipse.jetty.server.Request)116 CountDownLatch (java.util.concurrent.CountDownLatch)80 InterruptedIOException (java.io.InterruptedIOException)40 Result (org.eclipse.jetty.client.api.Result)38 InputStream (java.io.InputStream)35 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)32 Response (org.eclipse.jetty.client.api.Response)32 Request (org.eclipse.jetty.client.api.Request)29 OutputStream (java.io.OutputStream)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24