Search in sources :

Example 41 with DeferredContentProvider

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

the class AsyncMiddleManServletTest method testUpstreamTransformationBufferedGzipped.

@Test
public void testUpstreamTransformationBufferedGzipped() throws Exception {
    startServer(new EchoHttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            super.service(request, response);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new GZIPContentTransformer(new BufferingContentTransformer());
        }
    });
    startClient();
    DeferredContentProvider content = new DeferredContentProvider();
    Request request = client.newRequest("localhost", serverConnector.getLocalPort());
    FutureResponseListener listener = new FutureResponseListener(request);
    request.header(HttpHeader.CONTENT_ENCODING, "gzip").content(content).send(listener);
    byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
    content.offer(ByteBuffer.wrap(gzip(bytes)));
    sleep(1000);
    content.close();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 42 with DeferredContentProvider

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

the class AsyncMiddleManServletTest method testUpstreamTransformationThrowsAfterCommittingProxyRequest.

@Test
public void testUpstreamTransformationThrowsAfterCommittingProxyRequest() throws Exception {
    try (StacklessLogging scope = new StacklessLogging(HttpChannel.class)) {
        startServer(new EchoHttpServlet());
        startProxy(new AsyncMiddleManServlet() {

            @Override
            protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
                return new ContentTransformer() {

                    private int count;

                    @Override
                    public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                        if (++count < 2)
                            output.add(input);
                        else
                            throw new NullPointerException("explicitly_thrown_by_test");
                    }
                };
            }
        });
        startClient();
        final CountDownLatch latch = new CountDownLatch(1);
        DeferredContentProvider content = new DeferredContentProvider();
        client.newRequest("localhost", serverConnector.getLocalPort()).content(content).send(new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                if (result.isSucceeded() && result.getResponse().getStatus() == 502)
                    latch.countDown();
            }
        });
        content.offer(ByteBuffer.allocate(512));
        sleep(1000);
        content.offer(ByteBuffer.allocate(512));
        content.close();
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) 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) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 43 with DeferredContentProvider

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

the class AsyncMiddleManServletTest method testProxyRequestHeadersNotSentUntilFirstContent.

@Test
public void testProxyRequestHeadersNotSentUntilFirstContent() throws Exception {
    startServer(new EchoHttpServlet());
    final CountDownLatch proxyRequestLatch = new CountDownLatch(1);
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new ContentTransformer() {

                private ByteBuffer buffer;

                @Override
                public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                    // Buffer only the first chunk.
                    if (buffer == null) {
                        buffer = ByteBuffer.allocate(input.remaining());
                        buffer.put(input).flip();
                    } else if (buffer.hasRemaining()) {
                        output.add(buffer);
                        output.add(input);
                    } else {
                        output.add(input);
                    }
                }
            };
        }

        @Override
        protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
            proxyRequestLatch.countDown();
            super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
        }
    });
    startClient();
    DeferredContentProvider content = new DeferredContentProvider();
    Request request = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).content(content);
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    // Send one chunk of content, the proxy request must not be sent.
    ByteBuffer chunk1 = ByteBuffer.allocate(1024);
    content.offer(chunk1);
    Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
    // Send another chunk of content, the proxy request must be sent.
    ByteBuffer chunk2 = ByteBuffer.allocate(512);
    content.offer(chunk2);
    Assert.assertTrue(proxyRequestLatch.await(5, TimeUnit.SECONDS));
    // Finish the content.
    content.close();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(chunk1.capacity() + chunk2.capacity(), response.getContent().length);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 44 with DeferredContentProvider

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

the class Usage method testProxyUsage.

@Test
public void testProxyUsage() throws Exception {
    // In proxies, we receive the headers but not the content, so we must be able to send the request with
    // a lazy content provider that does not block request.send(...)
    HttpClient client = new HttpClient();
    client.start();
    final AtomicBoolean sendContent = new AtomicBoolean(true);
    DeferredContentProvider async = new DeferredContentProvider(ByteBuffer.wrap(new byte[] { 0, 1, 2 }));
    client.newRequest("localhost", 8080).content(async).send(new Response.Listener.Adapter() {

        @Override
        public void onBegin(Response response) {
            if (response.getStatus() == 404)
                sendContent.set(false);
        }
    });
    Thread.sleep(100);
    if (sendContent.get())
        async.offer(ByteBuffer.wrap(new byte[] { 0 }));
    Thread.sleep(100);
    if (sendContent.get())
        async.offer(ByteBuffer.wrap(new byte[] { 0 }));
    Thread.sleep(100);
    async.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 45 with DeferredContentProvider

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

the class ProxyServletFailureTest method testProxyRequestStallsContentServerIdlesTimeout.

@Test
public void testProxyRequestStallsContentServerIdlesTimeout() throws Exception {
    final byte[] content = new byte[] { 'C', '0', 'F', 'F', 'E', 'E' };
    int expected;
    if (proxyServlet instanceof AsyncProxyServlet) {
        // TODO should this be a 502 also???
        expected = 500;
        proxyServlet = new AsyncProxyServlet() {

            @Override
            protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
                DeferredContentProvider provider = new DeferredContentProvider() {

                    @Override
                    public boolean offer(ByteBuffer buffer, Callback callback) {
                        // Send less content to trigger the test condition.
                        buffer.limit(buffer.limit() - 1);
                        return super.offer(buffer.slice(), callback);
                    }
                };
                request.getInputStream().setReadListener(newReadListener(request, response, proxyRequest, provider));
                return provider;
            }
        };
    } else {
        expected = 502;
        proxyServlet = new ProxyServlet() {

            @Override
            protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
                return new BytesContentProvider(content) {

                    @Override
                    public long getLength() {
                        // Increase the content length to trigger the test condition.
                        return content.length + 1;
                    }
                };
            }
        };
    }
    prepareProxy();
    prepareServer(new EchoHttpServlet());
    long idleTimeout = 1000;
    serverConnector.setIdleTimeout(idleTimeout);
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(content)).send();
        Assert.assertEquals(expected, response.getStatus());
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Aggregations

DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)46 Test (org.junit.Test)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 HttpServletResponse (javax.servlet.http.HttpServletResponse)34 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)28 ServletInputStream (javax.servlet.ServletInputStream)18 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)18 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)17 Request (org.eclipse.jetty.server.Request)16 Result (org.eclipse.jetty.client.api.Result)15 InterruptedIOException (java.io.InterruptedIOException)14 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)12 AsyncContext (javax.servlet.AsyncContext)11 Request (org.eclipse.jetty.client.api.Request)11 ByteBuffer (java.nio.ByteBuffer)10 Response (org.eclipse.jetty.client.api.Response)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 ReadListener (javax.servlet.ReadListener)7