Search in sources :

Example 31 with FutureResponseListener

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

the class ValidatingConnectionPoolTest method testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnections.

private void testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConnections(Handler handler) throws Exception {
    start(handler);
    client.setMaxConnectionsPerDestination(1);
    final CountDownLatch latch = new CountDownLatch(1);
    Request request1 = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/one").onRequestBegin(r -> {
        try {
            latch.await();
        } catch (InterruptedException x) {
            r.abort(x);
        }
    });
    FutureResponseListener listener1 = new FutureResponseListener(request1);
    request1.send(listener1);
    Request request2 = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/two");
    FutureResponseListener listener2 = new FutureResponseListener(request2);
    request2.send(listener2);
    // Now we have one request about to be sent, and one queued.
    latch.countDown();
    ContentResponse response1 = listener1.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response1.getStatus());
    ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response2.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 32 with FutureResponseListener

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

the class ForwardProxyTLSServerTest method testTwoConcurrentExchanges.

@Test
public void testTwoConcurrentExchanges() throws Exception {
    startTLSServer(new ServerHandler());
    startProxy();
    final HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        final AtomicReference<Connection> connection = new AtomicReference<>();
        final CountDownLatch connectionLatch = new CountDownLatch(1);
        String content1 = "BODY";
        ContentResponse response1 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(content1, "UTF-8")).onRequestCommit(request -> {
            Destination destination = httpClient.getDestination(HttpScheme.HTTPS.asString(), "localhost", serverConnector.getLocalPort());
            destination.newConnection(new Promise.Adapter<Connection>() {

                @Override
                public void succeeded(Connection result) {
                    connection.set(result);
                    connectionLatch.countDown();
                }
            });
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response1.getStatus());
        String content = response1.getContentAsString();
        Assert.assertEquals(content1, content);
        Assert.assertTrue(connectionLatch.await(5, TimeUnit.SECONDS));
        String body2 = "body=" + content1;
        org.eclipse.jetty.client.api.Request request2 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.POST).path("/echo").header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()).header(HttpHeader.CONTENT_LENGTH, String.valueOf(body2.length())).content(new StringContentProvider(body2));
        // Make sure the second connection can send the exchange via the tunnel
        FutureResponseListener listener2 = new FutureResponseListener(request2);
        connection.get().send(request2, listener2);
        ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
        Assert.assertEquals(HttpStatus.OK_200, response2.getStatus());
        String content2 = response2.getContentAsString();
        Assert.assertEquals(content1, content2);
    } finally {
        httpClient.stop();
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Connection(org.eclipse.jetty.client.api.Connection) HttpConnection(org.eclipse.jetty.server.HttpConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(org.eclipse.jetty.util.Promise) HttpClient(org.eclipse.jetty.client.HttpClient) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 33 with FutureResponseListener

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

the class HttpRequest method send.

@Override
public ContentResponse send() throws InterruptedException, TimeoutException, ExecutionException {
    FutureResponseListener listener = new FutureResponseListener(this);
    send(this, listener);
    try {
        long timeout = getTimeout();
        if (timeout <= 0)
            return listener.get();
        return listener.get(timeout, TimeUnit.MILLISECONDS);
    } catch (Throwable x) {
        // Differently from the Future, the semantic of this method is that if
        // the send() is interrupted or times out, we abort the request.
        abort(x);
        throw x;
    }
}
Also used : FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Aggregations

FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)33 Test (org.junit.Test)24 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)22 Request (org.eclipse.jetty.client.api.Request)20 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 IOException (java.io.IOException)10 ServletException (javax.servlet.ServletException)9 OutputStream (java.io.OutputStream)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HttpExchange (org.eclipse.jetty.client.HttpExchange)7 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)7 Random (java.util.Random)6 Connection (org.eclipse.jetty.client.api.Connection)6 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)6 InputStream (java.io.InputStream)5 Destination (org.eclipse.jetty.client.api.Destination)5 Response (org.eclipse.jetty.client.api.Response)5 FuturePromise (org.eclipse.jetty.util.FuturePromise)5 ServerSocket (java.net.ServerSocket)4