Search in sources :

Example 86 with Response

use of org.eclipse.jetty.client.api.Response 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 87 with Response

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

the class HttpClientTest method testClientManyWritesSlowServer.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            long sleep = 1024;
            long total = 0;
            ServletInputStream input = request.getInputStream();
            byte[] buffer = new byte[1024];
            while (true) {
                int read = input.read(buffer);
                if (read < 0)
                    break;
                total += read;
                if (total >= sleep) {
                    sleep(250);
                    sleep += 256;
                }
            }
            response.getOutputStream().print(total);
        }
    });
    int chunks = 256;
    int chunkSize = 16;
    byte[][] bytes = IntStream.range(0, chunks).mapToObj(x -> new byte[chunkSize]).toArray(byte[][]::new);
    BytesContentProvider contentProvider = new BytesContentProvider("application/octet-stream", bytes);
    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).content(contentProvider).timeout(15, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(chunks * chunkSize, Integer.parseInt(response.getContentAsString()));
}
Also used : IntStream(java.util.stream.IntStream) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Assume(org.junit.Assume) HttpStatus(org.eclipse.jetty.http.HttpStatus) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) IO(org.eclipse.jetty.util.IO) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpMethod(org.eclipse.jetty.http.HttpMethod) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) 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) ServletInputStream(javax.servlet.ServletInputStream) Test(org.junit.Test)

Example 88 with Response

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

the class HttpClientTest method testAsyncResponseContentBackPressure.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Large write to generate multiple DATA frames.
            response.getOutputStream().write(new byte[256 * 1024]);
        }
    });
    CountDownLatch completeLatch = new CountDownLatch(1);
    AtomicInteger counter = new AtomicInteger();
    AtomicReference<Callback> callbackRef = new AtomicReference<>();
    AtomicReference<CountDownLatch> latchRef = new AtomicReference<>(new CountDownLatch(1));
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).onResponseContentAsync((response, content, callback) -> {
        if (counter.incrementAndGet() == 1) {
            callbackRef.set(callback);
            latchRef.get().countDown();
        } else {
            callback.succeeded();
        }
    }).send(result -> completeLatch.countDown());
    Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
    // Wait some time to verify that back pressure is applied correctly.
    Thread.sleep(1000);
    Assert.assertEquals(1, counter.get());
    callbackRef.get().succeeded();
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
Also used : IntStream(java.util.stream.IntStream) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Assume(org.junit.Assume) HttpStatus(org.eclipse.jetty.http.HttpStatus) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) IO(org.eclipse.jetty.util.IO) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpMethod(org.eclipse.jetty.http.HttpMethod) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) 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) 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) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 89 with Response

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_During100Continue.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and consume the content
            IO.copy(request.getInputStream(), new ByteArrayOutputStream());
        }
    });
    client.getProtocolHandlers().clear();
    client.getProtocolHandlers().put(new ContinueProtocolHandler() {

        @Override
        public Response.Listener getResponseListener() {
            final Response.Listener listener = super.getResponseListener();
            return new Response.Listener.Adapter() {

                @Override
                public void onBegin(Response response) {
                    response.abort(new Exception());
                }

                @Override
                public void onFailure(Response response, Throwable failure) {
                    listener.onFailure(response, failure);
                }
            };
        }
    });
    byte[] content = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNotNull(result.getRequestFailure());
            Assert.assertNotNull(result.getResponseFailure());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ContinueProtocolHandler(org.eclipse.jetty.client.ContinueProtocolHandler) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) 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) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 90 with Response

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

the class AsyncQueryForwardingServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(request.getContentType()) || APPLICATION_SMILE.equals(request.getContentType());
    final ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper;
    request.setAttribute(OBJECTMAPPER_ATTRIBUTE, objectMapper);
    final String defaultHost = hostFinder.getDefaultHost();
    request.setAttribute(HOST_ATTRIBUTE, defaultHost);
    final boolean isQueryEndpoint = request.getRequestURI().startsWith("/druid/v2");
    if (isQueryEndpoint && HttpMethod.DELETE.is(request.getMethod())) {
        // query cancellation request
        for (final String host : hostFinder.getAllHosts()) {
            // to keep the code simple, the proxy servlet will also send a request to one of the default brokers
            if (!host.equals(defaultHost)) {
                // issue async requests
                broadcastClient.newRequest(rewriteURI(request, host)).method(HttpMethod.DELETE).timeout(CANCELLATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS).send(new Response.CompleteListener() {

                    @Override
                    public void onComplete(Result result) {
                        if (result.isFailed()) {
                            log.warn(result.getFailure(), "Failed to forward cancellation request to [%s]", host);
                        }
                    }
                });
            }
            interruptedQueryCount.incrementAndGet();
        }
    } else if (isQueryEndpoint && HttpMethod.POST.is(request.getMethod())) {
        // query request
        try {
            Query inputQuery = objectMapper.readValue(request.getInputStream(), Query.class);
            if (inputQuery != null) {
                request.setAttribute(HOST_ATTRIBUTE, hostFinder.getHost(inputQuery));
                if (inputQuery.getId() == null) {
                    inputQuery = inputQuery.withId(UUID.randomUUID().toString());
                }
            }
            request.setAttribute(QUERY_ATTRIBUTE, inputQuery);
        } catch (IOException e) {
            log.warn(e, "Exception parsing query");
            final String errorMessage = e.getMessage() == null ? "no error message" : e.getMessage();
            requestLogger.log(new RequestLogLine(new DateTime(), request.getRemoteAddr(), null, new QueryStats(ImmutableMap.<String, Object>of("success", false, "exception", errorMessage))));
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.setContentType(MediaType.APPLICATION_JSON);
            objectMapper.writeValue(response.getOutputStream(), ImmutableMap.of("error", errorMessage));
            return;
        } catch (Exception e) {
            handleException(response, objectMapper, e);
            return;
        }
    }
    super.service(request, response);
}
Also used : Query(io.druid.query.Query) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Result(org.eclipse.jetty.client.api.Result) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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