Search in sources :

Example 11 with InputStreamResponseListener

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

the class HttpClientStreamTest method testDownloadWithCloseMiddleOfContent.

@Test(expected = AsynchronousCloseException.class)
public void testDownloadWithCloseMiddleOfContent() throws Exception {
    final byte[] data1 = new byte[1024];
    final byte[] data2 = new byte[1024];
    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.getOutputStream().write(data1);
            response.flushBuffer();
            try {
                Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                throw new InterruptedIOException();
            }
            response.getOutputStream().write(data2);
        }
    });
    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);
    for (byte datum1 : data1) Assert.assertEquals(datum1, input.read());
    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)

Example 12 with InputStreamResponseListener

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

the class HttpClientStreamTest method testInputStreamResponseListenerWithRedirect.

@Test
public void testInputStreamResponseListenerWithRedirect() 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.startsWith("/303"))
                response.sendRedirect("/200");
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).path("/303").followRedirects(true).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertTrue(result.isSucceeded());
}
Also used : 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) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 13 with InputStreamResponseListener

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

the class HttpClientStreamTest method testInputStreamResponseListenerBufferedRead.

@Test
public void testInputStreamResponseListenerBufferedRead() throws Exception {
    AtomicReference<AsyncContext> asyncContextRef = new AtomicReference<>();
    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);
            asyncContextRef.set(request.startAsync());
            latch.countDown();
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).timeout(5, TimeUnit.SECONDS).send(listener);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    AsyncContext asyncContext = asyncContextRef.get();
    Assert.assertNotNull(asyncContext);
    Random random = new Random();
    byte[] chunk = new byte[64];
    random.nextBytes(chunk);
    ServletOutputStream output = asyncContext.getResponse().getOutputStream();
    output.write(chunk);
    output.flush();
    // Use a buffer larger than the data
    // written to test that the read returns.
    byte[] buffer = new byte[2 * chunk.length];
    InputStream stream = listener.getInputStream();
    int totalRead = 0;
    while (totalRead < chunk.length) {
        int read = stream.read(buffer);
        Assert.assertTrue(read > 0);
        totalRead += read;
    }
    asyncContext.complete();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Request(org.eclipse.jetty.server.Request) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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) 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) Random(java.util.Random) Test(org.junit.Test)

Example 14 with InputStreamResponseListener

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

the class HttpClientStreamTest method testDownloadWithFailure.

@Test
public void testDownloadWithFailure() throws Exception {
    final byte[] data = new byte[64 * 1024];
    byte value = 1;
    Arrays.fill(data, value);
    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);
            // Say we want to send this much...
            response.setContentLength(2 * data.length);
            // ...but write only half...
            response.getOutputStream().write(data);
            // ...then shutdown output
            baseRequest.getHttpChannel().getEndPoint().shutdownOutput();
        }
    });
    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);
    int length = 0;
    try {
        length = 0;
        while (input.read() == value) {
            if (length % 100 == 0)
                Thread.sleep(1);
            ++length;
        }
        Assert.fail();
    } catch (IOException x) {
    // Expected.
    }
    Assert.assertThat(length, Matchers.lessThanOrEqualTo(data.length));
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isFailed());
}
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) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) 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) Test(org.junit.Test)

Example 15 with InputStreamResponseListener

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

the class HttpClientStreamTest method testDownloadOfUTF8Content.

@Test
public void testDownloadOfUTF8Content() throws Exception {
    // UTF-8 representation of &egrave;
    final byte[] data = new byte[] { (byte) 0xC3, (byte) 0xA8 };
    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.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);
    for (byte b : data) {
        int read = input.read();
        Assert.assertTrue(read >= 0);
        Assert.assertEquals(b & 0xFF, read);
    }
    Assert.assertEquals(-1, input.read());
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertNotNull(result);
    Assert.assertFalse(result.isFailed());
    Assert.assertSame(response, result.getResponse());
}
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) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) 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) 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