Search in sources :

Example 21 with BufferingResponseListener

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

the class HttpClientContinueTest method test_Expect100Continue_WithConcurrentDeferredContent_Respond100Continue.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and echo the content
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    final DeferredContentProvider content = new DeferredContentProvider();
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).onRequestHeaders(request -> {
        content.offer(ByteBuffer.wrap(data));
        content.close();
    }).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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 22 with BufferingResponseListener

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_During100Continue.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and consume the content
            IO.copy(request.getInputStream(), new ByteArrayOutputStream());
        }
    });
    client.getProtocolHandlers().clear();
    client.getProtocolHandlers().put(new ContinueProtocolHandler() {

        @Override
        public Response.Listener getResponseListener() {
            final Response.Listener listener = super.getResponseListener();
            return new Response.Listener.Adapter() {

                @Override
                public void onBegin(Response response) {
                    response.abort(new Exception());
                }

                @Override
                public void onFailure(Response response, Throwable failure) {
                    listener.onFailure(response, failure);
                }
            };
        }
    });
    byte[] content = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNotNull(result.getRequestFailure());
            Assert.assertNotNull(result.getResponseFailure());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ContinueProtocolHandler(org.eclipse.jetty.client.ContinueProtocolHandler) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) 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) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 23 with BufferingResponseListener

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

the class HttpClientContinueTest method test_Expect100Continue_WithDeferredContent_Respond100Continue.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and echo the content
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] chunk1 = new byte[] { 0, 1, 2, 3 };
    final byte[] chunk2 = new byte[] { 4, 5, 6, 7 };
    final byte[] data = new byte[chunk1.length + chunk2.length];
    System.arraycopy(chunk1, 0, data, 0, chunk1.length);
    System.arraycopy(chunk2, 0, data, chunk1.length, chunk2.length);
    final CountDownLatch latch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    Thread.sleep(1000);
    content.offer(ByteBuffer.wrap(chunk1));
    Thread.sleep(1000);
    content.offer(ByteBuffer.wrap(chunk2));
    content.close();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 24 with BufferingResponseListener

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

the class ProxyServletTest method testProxyWithBigResponseContentWithSlowReader.

@Test
public void testProxyWithBigResponseContentWithSlowReader() throws Exception {
    // Create a 6 MiB file
    final int length = 6 * 1024;
    Path targetTestsDir = MavenTestingUtils.getTargetTestingDir().toPath();
    Files.createDirectories(targetTestsDir);
    final Path temp = Files.createTempFile(targetTestsDir, "test_", null);
    byte[] kb = new byte[1024];
    new Random().nextBytes(kb);
    try (OutputStream output = Files.newOutputStream(temp, StandardOpenOption.CREATE)) {
        for (int i = 0; i < length; ++i) output.write(kb);
    }
    startServer(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try (InputStream input = Files.newInputStream(temp)) {
                IO.copy(input, response.getOutputStream());
            }
        }
    });
    startProxy();
    startClient();
    Request request = client.newRequest("localhost", serverConnector.getLocalPort()).path("/proxy/test");
    final CountDownLatch latch = new CountDownLatch(1);
    request.send(new BufferingResponseListener(2 * length * 1024) {

        @Override
        public void onContent(Response response, ByteBuffer content) {
            try {
                // Slow down the reader
                TimeUnit.MILLISECONDS.sleep(5);
                super.onContent(response, content);
            } catch (InterruptedException x) {
                response.abort(x);
            }
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertEquals(200, result.getResponse().getStatus());
            Assert.assertEquals(length * 1024, getContent().length);
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
}
Also used : Path(java.nio.file.Path) HttpServlet(javax.servlet.http.HttpServlet) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Random(java.util.Random) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 25 with BufferingResponseListener

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

the class ProxyServletTest method testExpect100ContinueRespond100Continue.

@Test
public void testExpect100ContinueRespond100Continue() throws Exception {
    CountDownLatch serverLatch1 = new CountDownLatch(1);
    CountDownLatch serverLatch2 = new CountDownLatch(1);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            serverLatch1.countDown();
            try {
                serverLatch2.await(5, TimeUnit.SECONDS);
            } catch (Throwable x) {
                throw new InterruptedIOException();
            }
            // Send the 100 Continue.
            ServletInputStream input = request.getInputStream();
            // Echo the content.
            IO.copy(input, response.getOutputStream());
        }
    });
    startProxy();
    startClient();
    byte[] content = new byte[1024];
    CountDownLatch contentLatch = new CountDownLatch(1);
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).onRequestContent((request, buffer) -> contentLatch.countDown()).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                if (result.getResponse().getStatus() == HttpStatus.OK_200) {
                    if (Arrays.equals(content, getContent()))
                        clientLatch.countDown();
                }
            }
        }
    });
    // Wait until we arrive on the server.
    Assert.assertTrue(serverLatch1.await(5, TimeUnit.SECONDS));
    // The client should not send the content yet.
    Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
    // Make the server send the 100 Continue.
    serverLatch2.countDown();
    // The client has sent the content.
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Arrays(java.util.Arrays) FilterChain(javax.servlet.FilterChain) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) AsyncEvent(javax.servlet.AsyncEvent) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) HttpClient(org.eclipse.jetty.client.HttpClient) HttpCookie(java.net.HttpCookie) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AsyncListener(javax.servlet.AsyncListener) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) After(org.junit.After) Filter(javax.servlet.Filter) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Server(org.eclipse.jetty.server.Server) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) Parameterized(org.junit.runners.Parameterized) PrintWriter(java.io.PrintWriter) HttpServlet(javax.servlet.http.HttpServlet) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) StandardOpenOption(java.nio.file.StandardOpenOption) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) TestTracker(org.eclipse.jetty.toolchain.test.TestTracker) IO(org.eclipse.jetty.util.IO) EOFException(java.io.EOFException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ServletResponse(javax.servlet.ServletResponse) GZIPOutputStream(java.util.zip.GZIPOutputStream) Result(org.eclipse.jetty.client.api.Result) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) ServletInputStream(javax.servlet.ServletInputStream) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) InterruptedIOException(java.io.InterruptedIOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) LinkedHashMap(java.util.LinkedHashMap) AsyncContext(javax.servlet.AsyncContext) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletOutputStream(javax.servlet.ServletOutputStream) ConnectException(java.net.ConnectException) Cookie(javax.servlet.http.Cookie) OutputStream(java.io.OutputStream) ServletRequest(javax.servlet.ServletRequest) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) 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) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpMethod(org.eclipse.jetty.http.HttpMethod) ServerConnector(org.eclipse.jetty.server.ServerConnector) Rule(org.junit.Rule) FilterConfig(javax.servlet.FilterConfig) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) InterruptedIOException(java.io.InterruptedIOException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Aggregations

Result (org.eclipse.jetty.client.api.Result)28 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 HttpServletRequest (javax.servlet.http.HttpServletRequest)25 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)24 Test (org.junit.Test)24 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)17 Request (org.eclipse.jetty.server.Request)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)12 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)12 InterruptedIOException (java.io.InterruptedIOException)11 Response (org.eclipse.jetty.client.api.Response)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Random (java.util.Random)7 ServletInputStream (javax.servlet.ServletInputStream)7 ServletOutputStream (javax.servlet.ServletOutputStream)7 HttpServlet (javax.servlet.http.HttpServlet)7 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)7