Search in sources :

Example 16 with InputStreamResponseListener

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

the class HttpClientStreamTest method testInputStreamResponseListenerFailedWhileWaiting.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            byte[] data = new byte[1024];
            response.setContentLength(data.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(data);
        }
    });
    CountDownLatch failedLatch = new CountDownLatch(1);
    CountDownLatch contentLatch = new CountDownLatch(1);
    InputStreamResponseListener listener = new InputStreamResponseListener() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            super.onContent(response, content, new Callback() {

                @Override
                public void failed(Throwable x) {
                    failedLatch.countDown();
                    callback.failed(x);
                }
            });
            contentLatch.countDown();
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    // Wait until we get some content.
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    // Abort the response.
    response.abort(new Exception());
    // Make sure that the callback has been invoked.
    Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServletException(javax.servlet.ServletException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) 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) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Example 17 with InputStreamResponseListener

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

the class HttpClientStreamTest method testInputStreamResponseListenerFailedBeforeResponse.

@Test
public void testInputStreamResponseListenerFailedBeforeResponse() throws Exception {
    start(new EmptyServerHandler());
    int port = connector.getLocalPort();
    server.stop();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    // Connect to the wrong port
    client.newRequest("localhost", port).scheme(getScheme()).send(listener);
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertNotNull(result);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 18 with InputStreamResponseListener

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

the class HttpClientStreamTest method testInputStreamResponseListenerClosedBeforeContent.

@Test(expected = AsynchronousCloseException.class)
public void testInputStreamResponseListenerClosedBeforeContent() throws Exception {
    AtomicReference<AsyncContext> contextRef = new AtomicReference<>();
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            contextRef.set(request.startAsync());
            response.flushBuffer();
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    InputStreamResponseListener listener = new InputStreamResponseListener() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            super.onContent(response, content, new Callback() {

                @Override
                public void failed(Throwable x) {
                    latch.countDown();
                    callback.failed(x);
                }
            });
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    InputStream input = listener.getInputStream();
    input.close();
    AsyncContext asyncContext = contextRef.get();
    asyncContext.getResponse().getOutputStream().write(new byte[1024]);
    asyncContext.complete();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    // Throws
    input.read();
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncContext(javax.servlet.AsyncContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) 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) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Example 19 with InputStreamResponseListener

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

the class Usage method testResponseInputStream.

@Test
public void testResponseInputStream() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    // Send asynchronously with the InputStreamResponseListener
    client.newRequest("localhost", 8080).send(listener);
    // Call to the listener's get() blocks until the headers arrived
    Response response = listener.get(5, TimeUnit.SECONDS);
    // Now check the response information that arrived to decide whether to read the content
    if (response.getStatus() == 200) {
        byte[] buffer = new byte[256];
        try (InputStream input = listener.getInputStream()) {
            while (true) {
                int read = input.read(buffer);
                if (read < 0)
                    break;
            // Do something with the bytes just read
            }
        }
    } else {
        response.abort(new Exception());
    }
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 20 with InputStreamResponseListener

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

the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingBigContent.

@Test
public void testProxyRequestFailureInTheMiddleOfProxyingBigContent() throws Exception {
    int outputBufferSize = 1024;
    final CountDownLatch chunk1Latch = new CountDownLatch(1);
    final byte[] chunk1 = new byte[outputBufferSize];
    new Random().nextBytes(chunk1);
    final int chunk2 = 'w';
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            response.flushBuffer();
            // Wait for the client to receive this chunk.
            await(chunk1Latch, 5000);
            // Send second chunk, must not be received by proxy.
            output.write(chunk2);
        }

        private boolean await(CountDownLatch latch, long ms) throws IOException {
            try {
                return latch.await(ms, TimeUnit.MILLISECONDS);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    final long proxyTimeout = 1000;
    Map<String, String> proxyParams = new HashMap<>();
    proxyParams.put("timeout", String.valueOf(proxyTimeout));
    proxyParams.put("outputBufferSize", String.valueOf(outputBufferSize));
    startProxy(proxyParams);
    startClient();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    int port = serverConnector.getLocalPort();
    client.newRequest("localhost", port).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    InputStream input = listener.getInputStream();
    for (int i = 0; i < chunk1.length; ++i) Assert.assertEquals(chunk1[i] & 0xFF, input.read());
    TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
    chunk1Latch.countDown();
    try {
        // Make sure the proxy does not receive chunk2.
        input.read();
        Assert.fail();
    } catch (EOFException x) {
    // Expected
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) ServletOutputStream(javax.servlet.ServletOutputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpServlet(javax.servlet.http.HttpServlet) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) EOFException(java.io.EOFException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Aggregations

InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)20 IOException (java.io.IOException)18 Response (org.eclipse.jetty.client.api.Response)18 Test (org.junit.Test)17 InputStream (java.io.InputStream)16 InterruptedIOException (java.io.InterruptedIOException)15 ServletException (javax.servlet.ServletException)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 HttpServletResponse (javax.servlet.http.HttpServletResponse)15 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)15 Request (org.eclipse.jetty.server.Request)13 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)13 ByteArrayInputStream (java.io.ByteArrayInputStream)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 Result (org.eclipse.jetty.client.api.Result)7 ServletOutputStream (javax.servlet.ServletOutputStream)6 Callback (org.eclipse.jetty.util.Callback)5 ByteBuffer (java.nio.ByteBuffer)4 ExecutionException (java.util.concurrent.ExecutionException)4 Random (java.util.Random)3