Search in sources :

Example 61 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class HttpClientIdleTimeoutTest method testClientIdleTimeout.

@Test
public void testClientIdleTimeout() 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);
            }
        }
    });
    client.stop();
    client.setIdleTimeout(idleTimeout);
    client.start();
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).path("/timeout").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 62 with Request

use of org.eclipse.jetty.server.Request 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 63 with Request

use of org.eclipse.jetty.server.Request 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 64 with Request

use of org.eclipse.jetty.server.Request 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 65 with Request

use of org.eclipse.jetty.server.Request 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)

Aggregations

Request (org.eclipse.jetty.server.Request)297 HttpServletRequest (javax.servlet.http.HttpServletRequest)250 HttpServletResponse (javax.servlet.http.HttpServletResponse)225 Test (org.junit.Test)177 IOException (java.io.IOException)169 ServletException (javax.servlet.ServletException)150 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)147 CountDownLatch (java.util.concurrent.CountDownLatch)66 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)64 InterruptedIOException (java.io.InterruptedIOException)45 Server (org.eclipse.jetty.server.Server)41 InputStream (java.io.InputStream)37 AtomicReference (java.util.concurrent.atomic.AtomicReference)35 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Response (org.eclipse.jetty.client.api.Response)27 Result (org.eclipse.jetty.client.api.Result)27 ServletInputStream (javax.servlet.ServletInputStream)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ServletOutputStream (javax.servlet.ServletOutputStream)23 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)23