Search in sources :

Example 1 with Response

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

the class AsyncMiddleManServletTest method testAfterContentTransformerDoNoTransform.

private void testAfterContentTransformerDoNoTransform(final boolean readSource, final boolean useDisk) throws Exception {
    final String key0 = "id";
    long value0 = 1;
    final String key1 = "channel";
    String value1 = "foo";
    final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new AfterContentTransformer() {

                {
                    if (useDisk)
                        setMaxInputBufferSize(0);
                }

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    if (readSource) {
                        InputStream input = source.getInputStream();
                        JSON.parse(new InputStreamReader(input, "UTF-8"));
                    }
                    // No transformation.
                    return false;
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
    Assert.assertNotNull(obj);
    Assert.assertEquals(2, obj.size());
    Assert.assertEquals(value0, obj.get(key0));
    Assert.assertEquals(value1, obj.get(key1));
}
Also used : InputStreamReader(java.io.InputStreamReader) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) GZIPInputStream(java.util.zip.GZIPInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) 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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Response

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

the class AsyncMiddleManServletTest method testAfterContentTransformerInputStreamReset.

private void testAfterContentTransformerInputStreamReset(final boolean overflow) throws Exception {
    final byte[] data = new byte[] { 'c', 'o', 'f', 'f', 'e', 'e' };
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // Write the content in two chunks.
            int chunk = data.length / 2;
            ServletOutputStream output = response.getOutputStream();
            output.write(data, 0, chunk);
            sleep(1000);
            output.write(data, chunk, data.length - chunk);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new AfterContentTransformer() {

                {
                    setMaxInputBufferSize(overflow ? data.length / 2 : data.length * 2);
                }

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    // Consume the stream once.
                    InputStream input = source.getInputStream();
                    IO.copy(input, IO.getNullStream());
                    // Reset the stream and re-read it.
                    input.reset();
                    IO.copy(input, sink.getOutputStream());
                    return true;
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) GZIPInputStream(java.util.zip.GZIPInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) 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)

Example 3 with Response

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

the class AsyncMiddleManServletTest method testAfterContentTransformer.

@Test
public void testAfterContentTransformer() throws Exception {
    final String key0 = "id";
    long value0 = 1;
    final String key1 = "channel";
    String value1 = "foo";
    final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        }
    });
    final String key2 = "c";
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new AfterContentTransformer() {

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    InputStream input = source.getInputStream();
                    @SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(new InputStreamReader(input, "UTF-8"));
                    // Transform the object.
                    obj.put(key2, obj.remove(key1));
                    try (OutputStream output = sink.getOutputStream()) {
                        output.write(JSON.toString(obj).getBytes(StandardCharsets.UTF_8));
                        return true;
                    }
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
    Assert.assertNotNull(obj);
    Assert.assertEquals(2, obj.size());
    Assert.assertEquals(value0, obj.get(key0));
    Assert.assertEquals(value1, obj.get(key2));
}
Also used : InputStreamReader(java.io.InputStreamReader) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) GZIPInputStream(java.util.zip.GZIPInputStream) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) 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) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with Response

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

the class AsyncMiddleManServletTest method testLargeChunkedBufferedDownstreamTransformation.

private void testLargeChunkedBufferedDownstreamTransformation(final boolean gzipped) throws Exception {
    // Tests the race between a incomplete write performed from ProxyResponseListener.onSuccess()
    // and ProxyResponseListener.onComplete() being called before the write has completed.
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            OutputStream output = response.getOutputStream();
            if (gzipped) {
                output = new GZIPOutputStream(output);
                response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            }
            Random random = new Random();
            byte[] chunk = new byte[1024 * 1024];
            for (int i = 0; i < 16; ++i) {
                random.nextBytes(chunk);
                output.write(chunk);
                output.flush();
            }
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            ContentTransformer transformer = new BufferingContentTransformer();
            if (gzipped)
                transformer = new GZIPContentTransformer(transformer);
            return transformer;
        }
    });
    startClient();
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest("localhost", serverConnector.getLocalPort()).onResponseContent(new Response.ContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content) {
            // Slow down the reader so that the
            // write from the proxy gets congested.
            sleep(1);
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            Assert.assertEquals(200, result.getResponse().getStatus());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(15, TimeUnit.SECONDS));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Random(java.util.Random) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 5 with Response

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

the class AsyncMiddleManServletTest method testDiscardUpstreamAndDownstreamKnownContentLengthGzipped.

@Test
public void testDiscardUpstreamAndDownstreamKnownContentLengthGzipped() throws Exception {
    final byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // decode input stream thru gzip
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            IO.copy(new GZIPInputStream(request.getInputStream()), bos);
            // ensure decompressed is 0 length
            Assert.assertEquals(0, bos.toByteArray().length);
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            response.getOutputStream().write(gzip(bytes));
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new GZIPContentTransformer(new DiscardContentTransformer());
        }

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new GZIPContentTransformer(new DiscardContentTransformer());
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(new BytesContentProvider(gzip(bytes))).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(0, response.getContent().length);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPInputStream(java.util.zip.GZIPInputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)113 Test (org.junit.Test)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)89 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)84 IOException (java.io.IOException)77 HttpServletRequest (javax.servlet.http.HttpServletRequest)70 ServletException (javax.servlet.ServletException)68 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ByteBuffer (java.nio.ByteBuffer)34 ServletOutputStream (javax.servlet.ServletOutputStream)34 Request (org.eclipse.jetty.client.api.Request)31 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)29 Request (org.eclipse.jetty.server.Request)26 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22