Search in sources :

Example 16 with BufferingResponseListener

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

the class HttpRedirector method redirect.

/**
     * Redirects the given {@code response}, blocking until the redirect is complete.
     *
     * @param request the original request that triggered the redirect
     * @param response the response to the original request
     * @return a {@link Result} object containing the request to the redirected location and its response
     * @throws InterruptedException if the thread is interrupted while waiting for the redirect to complete
     * @throws ExecutionException if the redirect failed
     * @see #redirect(Request, Response, Response.CompleteListener)
     */
public Result redirect(Request request, Response response) throws InterruptedException, ExecutionException {
    final AtomicReference<Result> resultRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    Request redirect = redirect(request, response, new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            resultRef.set(new Result(result.getRequest(), result.getRequestFailure(), new HttpContentResponse(result.getResponse(), getContent(), getMediaType(), getEncoding()), result.getResponseFailure()));
            latch.countDown();
        }
    });
    try {
        latch.await();
        Result result = resultRef.get();
        if (result.isFailed())
            throw new ExecutionException(result.getFailure());
        return result;
    } catch (InterruptedException x) {
        // If the application interrupts, we need to abort the redirect
        redirect.abort(x);
        throw x;
    }
}
Also used : Request(org.eclipse.jetty.client.api.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Result(org.eclipse.jetty.client.api.Result) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener)

Example 17 with BufferingResponseListener

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

the class ProxyServletTest method testExpect100ContinueRespond100ContinueDelayedRequestContent.

@Test
public void testExpect100ContinueRespond100ContinueDelayedRequestContent() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // Send the 100 Continue.
            ServletInputStream input = request.getInputStream();
            // Echo the content.
            IO.copy(input, response.getOutputStream());
        }
    });
    startProxy();
    startClient();
    byte[] content = new byte[1024];
    new Random().nextBytes(content);
    int chunk1 = content.length / 2;
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    contentProvider.offer(ByteBuffer.wrap(content, 0, chunk1));
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(contentProvider).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                if (result.getResponse().getStatus() == HttpStatus.OK_200) {
                    if (Arrays.equals(content, getContent()))
                        clientLatch.countDown();
                }
            }
        }
    });
    // Wait a while and then offer more content.
    Thread.sleep(1000);
    contentProvider.offer(ByteBuffer.wrap(content, chunk1, content.length - chunk1));
    contentProvider.close();
    Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) 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) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 18 with BufferingResponseListener

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

the class HttpClientTest method setOnCompleteCallbackWithBlockingSend.

@Test
public void setOnCompleteCallbackWithBlockingSend() throws Exception {
    final byte[] content = new byte[512];
    new Random().nextBytes(content);
    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(content);
        }
    });
    final Exchanger<Response> ex = new Exchanger<>();
    BufferingResponseListener listener = new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            try {
                ex.exchange(result.getResponse());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(listener);
    Response response = ex.exchange(null);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(content, listener.getContent());
}
Also used : Exchanger(java.util.concurrent.Exchanger) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) Random(java.util.Random) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 19 with BufferingResponseListener

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

the class HttpClientStreamTest method testUploadWithOutputStream.

@Test
public void testUploadWithOutputStream() 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());
        }
    });
    final byte[] data = new byte[512];
    final CountDownLatch latch = new CountDownLatch(1);
    OutputStreamContentProvider content = new OutputStreamContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded() && result.getResponse().getStatus() == 200 && Arrays.equals(data, getContent()))
                latch.countDown();
        }
    });
    // Make sure we provide the content *after* the request has been "sent".
    Thread.sleep(1000);
    try (OutputStream output = content.getOutputStream()) {
        output.write(data);
    }
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) 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) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 20 with BufferingResponseListener

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

the class HttpClientContinueTest method test_Redirect_WithExpect100Continue_WithContent.

@Test
public void test_Redirect_WithExpect100Continue_WithContent() throws Exception {
    // A request with Expect: 100-Continue cannot receive non-final responses like 3xx
    final String data = "success";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (request.getRequestURI().endsWith("/done")) {
                // Send 100-Continue and consume the content
                IO.copy(request.getInputStream(), new ByteArrayOutputStream());
                response.getOutputStream().print(data);
            } else {
                // Send a redirect
                response.sendRedirect("/done");
            }
        }
    });
    byte[] content = new byte[10240];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).method(HttpMethod.POST).path("/redirect").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.assertNull(result.getResponseFailure());
            Assert.assertEquals(302, result.getResponse().getStatus());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : 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) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Aggregations

Result (org.eclipse.jetty.client.api.Result)28 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 HttpServletRequest (javax.servlet.http.HttpServletRequest)25 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)24 Test (org.junit.Test)24 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)17 Request (org.eclipse.jetty.server.Request)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)12 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)12 InterruptedIOException (java.io.InterruptedIOException)11 Response (org.eclipse.jetty.client.api.Response)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Random (java.util.Random)7 ServletInputStream (javax.servlet.ServletInputStream)7 ServletOutputStream (javax.servlet.ServletOutputStream)7 HttpServlet (javax.servlet.http.HttpServlet)7 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)7