Search in sources :

Example 6 with InputStreamResponseListener

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

the class HttpClientStreamTest method testInputStreamResponseListenerClosedBeforeReading.

@Test(expected = AsynchronousCloseException.class)
public void testInputStreamResponseListenerClosedBeforeReading() throws Exception {
    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);
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    InputStream stream = listener.getInputStream();
    // Close the stream immediately.
    stream.close();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(new BytesContentProvider(new byte[] { 0, 1, 2, 3 })).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    // Throws
    stream.read();
}
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) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) 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 7 with InputStreamResponseListener

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

the class HttpClientStreamTest method testDownloadWithCloseBeforeContent.

@Test(expected = AsynchronousCloseException.class)
public void testDownloadWithCloseBeforeContent() throws Exception {
    final byte[] data = new byte[128 * 1024];
    byte value = 3;
    Arrays.fill(data, value);
    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.flushBuffer();
            try {
                Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                throw new InterruptedIOException();
            }
            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);
    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 8 with InputStreamResponseListener

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

the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingSmallContent.

@Test
public void testProxyRequestFailureInTheMiddleOfProxyingSmallContent() throws Exception {
    final CountDownLatch chunk1Latch = new CountDownLatch(1);
    final int chunk1 = 'q';
    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));
    startProxy(proxyParams);
    startClient();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    int port = serverConnector.getLocalPort();
    client.newRequest("localhost", port).send(listener);
    // Make the proxy request fail; given the small content, the
    // proxy-to-client response is not committed yet so it will be reset.
    TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(504, response.getStatus());
    // Make sure there is no content, as the proxy-to-client response has been reset.
    InputStream input = listener.getInputStream();
    Assert.assertEquals(-1, input.read());
    chunk1Latch.countDown();
    // Result succeeds because a 504 is a valid HTTP response.
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertTrue(result.isSucceeded());
    // Make sure the proxy does not receive chunk2.
    Assert.assertEquals(-1, input.read());
    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) 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) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 9 with InputStreamResponseListener

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

the class ZeppelinhubRestApiHandler method getInstances.

/**
   * Fetch zeppelin instances for a given user.
   * @param ticket
   * @return
   * @throws IOException
   */
public List<Instance> getInstances(String ticket) throws IOException {
    InputStreamResponseListener listener = new InputStreamResponseListener();
    Response response;
    String url = zepelinhubUrl + "instances";
    String data;
    Request request = client.newRequest(url).header(USER_SESSION_HEADER, ticket);
    request.send(listener);
    try {
        response = listener.get(30, TimeUnit.SECONDS);
    } catch (InterruptedException | TimeoutException | ExecutionException e) {
        LOG.error("Cannot perform request to ZeppelinHub", e);
        throw new IOException("Cannot perform  GET request to ZeppelinHub", e);
    }
    int code = response.getStatus();
    if (code == 200) {
        try (InputStream responseContent = listener.getInputStream()) {
            data = IOUtils.toString(responseContent, "UTF-8");
        }
    } else {
        LOG.error("ZeppelinHub GET {} returned with status {} ", url, code);
        throw new IOException("Cannot perform  GET request to ZeppelinHub");
    }
    Type listType = new TypeToken<ArrayList<Instance>>() {
    }.getType();
    return new Gson().fromJson(data, listType);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) Response(org.eclipse.jetty.client.api.Response) Type(java.lang.reflect.Type) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with InputStreamResponseListener

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

the class ZeppelinhubRestApiHandler method sendToZeppelinHub.

private String sendToZeppelinHub(final Request request) throws IOException {
    InputStreamResponseListener listener = new InputStreamResponseListener();
    Response response;
    String data;
    request.send(listener);
    try {
        response = listener.get(30, TimeUnit.SECONDS);
    } catch (InterruptedException | TimeoutException | ExecutionException e) {
        String method = request.getMethod();
        LOG.error("Cannot perform {} request to ZeppelinHub", method, e);
        throw new IOException("Cannot perform " + method + " request to ZeppelinHub", e);
    }
    int code = response.getStatus();
    if (code == 200) {
        try (InputStream responseContent = listener.getInputStream()) {
            data = IOUtils.toString(responseContent, "UTF-8");
        }
    } else {
        String method = response.getRequest().getMethod();
        String url = response.getRequest().getURI().toString();
        LOG.error("ZeppelinHub {} {} returned with status {} ", method, url, code);
        throw new IOException("Cannot perform " + method + " request to ZeppelinHub");
    }
    return data;
}
Also used : Response(org.eclipse.jetty.client.api.Response) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) InputStream(java.io.InputStream) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

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