Search in sources :

Example 51 with Result

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

the class HttpSender method abort.

public boolean abort(HttpExchange exchange, Throwable failure) {
    // Update the state to avoid more request processing.
    boolean terminate;
    out: while (true) {
        RequestState current = requestState.get();
        switch(current) {
            case FAILURE:
                {
                    return false;
                }
            default:
                {
                    if (updateRequestState(current, RequestState.FAILURE)) {
                        terminate = current != RequestState.TRANSIENT;
                        break out;
                    }
                    break;
                }
        }
    }
    this.failure = failure;
    dispose();
    Request request = exchange.getRequest();
    if (LOG.isDebugEnabled())
        LOG.debug("Request failure {} {} on {}: {}", request, exchange, getHttpChannel(), failure);
    HttpDestination destination = getHttpChannel().getHttpDestination();
    destination.getRequestNotifier().notifyFailure(request, failure);
    if (terminate) {
        // Mark atomically the request as terminated, with
        // respect to concurrency between request and response.
        Result result = exchange.terminateRequest();
        terminateRequest(exchange, failure, result);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("Concurrent failure: request termination skipped, performed by helpers");
    }
    return true;
}
Also used : Request(org.eclipse.jetty.client.api.Request) Result(org.eclipse.jetty.client.api.Result)

Example 52 with Result

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

the class ResponseNotifier method forwardFailureComplete.

public void forwardFailureComplete(List<Response.ResponseListener> listeners, Request request, Throwable requestFailure, Response response, Throwable responseFailure) {
    forwardFailure(listeners, response, responseFailure);
    notifyComplete(listeners, new Result(request, requestFailure, response, responseFailure));
}
Also used : Result(org.eclipse.jetty.client.api.Result)

Example 53 with Result

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

the class ResponseNotifier method forwardSuccessComplete.

public void forwardSuccessComplete(List<Response.ResponseListener> listeners, Request request, Response response) {
    forwardSuccess(listeners, response);
    notifyComplete(listeners, new Result(request, response));
}
Also used : Result(org.eclipse.jetty.client.api.Result)

Example 54 with Result

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

the class PushedResourcesTest method testPushedResourceRedirect.

@Test
public void testPushedResourceRedirect() throws Exception {
    Random random = new Random();
    byte[] pushBytes = new byte[512];
    random.nextBytes(pushBytes);
    String oldPath = "/old";
    String newPath = "/new";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals(oldPath))
                response.sendRedirect(newPath);
            else if (target.equals(newPath))
                response.getOutputStream().write(pushBytes);
            else
                baseRequest.getPushBuilder().path(oldPath).push();
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            Assert.assertEquals(oldPath, pushedRequest.getPath());
            Assert.assertEquals(newPath, result.getRequest().getPath());
            Assert.assertArrayEquals(pushBytes, getContent());
            latch.countDown();
        }
    }).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) Random(java.util.Random) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 55 with Result

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

the class PushedResourcesTest method testPushedResources.

@Test
public void testPushedResources() throws Exception {
    Random random = new Random();
    byte[] bytes = new byte[512];
    random.nextBytes(bytes);
    byte[] pushBytes1 = new byte[1024];
    random.nextBytes(pushBytes1);
    byte[] pushBytes2 = new byte[2048];
    random.nextBytes(pushBytes2);
    String path1 = "/secondary1";
    String path2 = "/secondary2";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals(path1)) {
                response.getOutputStream().write(pushBytes1);
            } else if (target.equals(path2)) {
                response.getOutputStream().write(pushBytes2);
            } else {
                baseRequest.getPushBuilder().path(path1).push();
                baseRequest.getPushBuilder().path(path2).push();
                response.getOutputStream().write(bytes);
            }
        }
    });
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            if (pushedRequest.getPath().equals(path1)) {
                Assert.assertArrayEquals(pushBytes1, getContent());
                latch1.countDown();
            } else if (pushedRequest.getPath().equals(path2)) {
                Assert.assertArrayEquals(pushBytes2, getContent());
                latch2.countDown();
            }
        }
    }).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
    Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) Random(java.util.Random) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Aggregations

Result (org.eclipse.jetty.client.api.Result)100 Test (org.junit.Test)80 CountDownLatch (java.util.concurrent.CountDownLatch)79 HttpServletResponse (javax.servlet.http.HttpServletResponse)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)66 Response (org.eclipse.jetty.client.api.Response)62 IOException (java.io.IOException)60 ServletException (javax.servlet.ServletException)56 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)44 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)41 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)39 Request (org.eclipse.jetty.client.api.Request)31 ByteBuffer (java.nio.ByteBuffer)29 Request (org.eclipse.jetty.server.Request)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)24 InterruptedIOException (java.io.InterruptedIOException)21 ServletOutputStream (javax.servlet.ServletOutputStream)20 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)16