Search in sources :

Example 16 with Response

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

the class RedirectProtocolHandler method onComplete.

@Override
public void onComplete(Result result) {
    Request request = result.getRequest();
    Response response = result.getResponse();
    if (result.isSucceeded())
        redirector.redirect(request, response, null);
    else
        redirector.fail(request, response, result.getFailure());
}
Also used : Response(org.eclipse.jetty.client.api.Response) Request(org.eclipse.jetty.client.api.Request)

Example 17 with Response

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

the class HttpChannelOverHTTP method exchangeTerminated.

@Override
public void exchangeTerminated(HttpExchange exchange, Result result) {
    super.exchangeTerminated(exchange, result);
    Response response = result.getResponse();
    HttpFields responseHeaders = response.getHeaders();
    String closeReason = null;
    if (result.isFailed())
        closeReason = "failure";
    else if (receiver.isShutdown())
        closeReason = "server close";
    else if (sender.isShutdown())
        closeReason = "client close";
    if (closeReason == null) {
        if (response.getVersion().compareTo(HttpVersion.HTTP_1_1) < 0) {
            // HTTP 1.0 must close the connection unless it has
            // an explicit keep alive or it's a CONNECT method.
            boolean keepAlive = responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString());
            boolean connect = HttpMethod.CONNECT.is(exchange.getRequest().getMethod());
            if (!keepAlive && !connect)
                closeReason = "http/1.0";
        } else {
            // HTTP 1.1 closes only if it has an explicit close.
            if (responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()))
                closeReason = "http/1.1";
        }
    }
    if (closeReason != null) {
        if (LOG.isDebugEnabled())
            LOG.debug("Closing, reason: {} - {}", closeReason, connection);
        connection.close();
    } else {
        if (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)
            connection.remove();
        else
            release();
    }
}
Also used : HttpResponse(org.eclipse.jetty.client.HttpResponse) Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields)

Example 18 with Response

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

the class HttpClientAsyncContentTest method testSmallAsyncContent.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            ServletOutputStream output = response.getOutputStream();
            output.write(65);
            output.flush();
            output.write(66);
        }
    });
    final AtomicInteger contentCount = new AtomicInteger();
    final AtomicReference<Callback> callbackRef = new AtomicReference<>();
    final AtomicReference<CountDownLatch> contentLatch = new AtomicReference<>(new CountDownLatch(1));
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseContentAsync(new Response.AsyncContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            contentCount.incrementAndGet();
            callbackRef.set(callback);
            contentLatch.get().countDown();
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            completeLatch.countDown();
        }
    });
    Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
    Callback callback = callbackRef.get();
    // Wait a while to be sure that the parsing does not proceed.
    TimeUnit.MILLISECONDS.sleep(1000);
    Assert.assertEquals(1, contentCount.get());
    // Succeed the content callback to proceed with parsing.
    callbackRef.set(null);
    contentLatch.set(new CountDownLatch(1));
    callback.succeeded();
    Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
    callback = callbackRef.get();
    // Wait a while to be sure that the parsing does not proceed.
    TimeUnit.MILLISECONDS.sleep(1000);
    Assert.assertEquals(2, contentCount.get());
    Assert.assertEquals(1, completeLatch.getCount());
    // Succeed the content callback to proceed with parsing.
    callbackRef.set(null);
    contentLatch.set(new CountDownLatch(1));
    callback.succeeded();
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(2, contentCount.get());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 19 with Response

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

the class HttpClientRedirectTest method testDontFollowRedirects.

@Test
public void testDontFollowRedirects() throws Exception {
    start(new RedirectHandler());
    Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).followRedirects(false).path("/303/localhost/done?close=true").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(303, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 20 with Response

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

the class HttpClientRedirectTest method test_301.

@Test
public void test_301() throws Exception {
    start(new RedirectHandler());
    Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.HEAD).path("/301/localhost/done").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)104 Test (org.junit.Test)89 HttpServletResponse (javax.servlet.http.HttpServletResponse)88 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)82 IOException (java.io.IOException)71 HttpServletRequest (javax.servlet.http.HttpServletRequest)69 ServletException (javax.servlet.ServletException)67 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)33 InputStream (java.io.InputStream)32 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 Request (org.eclipse.jetty.server.Request)26 Request (org.eclipse.jetty.client.api.Request)24 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22