Search in sources :

Example 26 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnQueued.

@Test
public void testAbortOnQueued() throws Exception {
    start(new EmptyServerHandler());
    final Throwable cause = new Exception();
    final AtomicBoolean aborted = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean begin = new AtomicBoolean();
    try {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).listener(new Request.Listener.Adapter() {

            @Override
            public void onQueued(Request request) {
                aborted.set(request.abort(cause));
                latch.countDown();
            }

            @Override
            public void onBegin(Request request) {
                begin.set(true);
            }
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        if (aborted.get())
            Assert.assertSame(cause, x.getCause());
        Assert.assertFalse(begin.get());
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 27 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnBegin.

@Test
public void testAbortOnBegin() throws Exception {
    start(new EmptyServerHandler());
    final Throwable cause = new Exception();
    final AtomicBoolean aborted = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch committed = new CountDownLatch(1);
    try {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).listener(new Request.Listener.Adapter() {

            @Override
            public void onBegin(Request request) {
                aborted.set(request.abort(cause));
                latch.countDown();
            }

            @Override
            public void onCommit(Request request) {
                committed.countDown();
            }
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        if (aborted.get())
            Assert.assertSame(cause, x.getCause());
        Assert.assertFalse(committed.await(1, TimeUnit.SECONDS));
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 28 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnCommitWithContent.

@Test
public void testAbortOnCommitWithContent() throws Exception {
    final AtomicReference<IOException> failure = new AtomicReference<>();
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                if (request.getDispatcherType() != DispatcherType.ERROR)
                    IO.copy(request.getInputStream(), response.getOutputStream());
            } catch (IOException x) {
                failure.set(x);
                throw x;
            }
        }
    });
    final Throwable cause = new Exception();
    final AtomicBoolean aborted = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestCommit(request -> {
            aborted.set(request.abort(cause));
            latch.countDown();
        }).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {

            @Override
            public long getLength() {
                return -1;
            }
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        if (aborted.get())
            Assert.assertSame(cause, x.getCause());
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Result(org.eclipse.jetty.client.api.Result) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Test(org.junit.Test) Request(org.eclipse.jetty.client.api.Request) IO(org.eclipse.jetty.util.IO) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) DispatcherType(javax.servlet.DispatcherType) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 29 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortLongPoll.

@Test
public void testAbortLongPoll() throws Exception {
    final long delay = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                if (request.getDispatcherType() != DispatcherType.ERROR)
                    TimeUnit.MILLISECONDS.sleep(2 * delay);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    final Request request = client.newRequest("localhost", connector.getLocalPort()).timeout(3 * delay, TimeUnit.MILLISECONDS).scheme(scheme);
    final Throwable cause = new Exception();
    final AtomicBoolean aborted = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread(() -> {
        try {
            TimeUnit.MILLISECONDS.sleep(delay);
            aborted.set(request.abort(cause));
            latch.countDown();
        } catch (InterruptedException x) {
            throw new RuntimeException(x);
        }
    }).start();
    try {
        request.send();
    } catch (ExecutionException x) {
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        if (aborted.get())
            Assert.assertSame(cause, x.getCause());
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 30 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingBigContent.

@Test
public void testProxyRequestFailureInTheMiddleOfProxyingBigContent() throws Exception {
    int outputBufferSize = 1024;
    final CountDownLatch chunk1Latch = new CountDownLatch(1);
    final byte[] chunk1 = new byte[outputBufferSize];
    new Random().nextBytes(chunk1);
    final int chunk2 = 'w';
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            response.flushBuffer();
            // Wait for the client to receive this chunk.
            await(chunk1Latch, 5000);
            // Send second chunk, must not be received by proxy.
            output.write(chunk2);
        }

        private boolean await(CountDownLatch latch, long ms) throws IOException {
            try {
                return latch.await(ms, TimeUnit.MILLISECONDS);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    final long proxyTimeout = 1000;
    Map<String, String> proxyParams = new HashMap<>();
    proxyParams.put("timeout", String.valueOf(proxyTimeout));
    proxyParams.put("outputBufferSize", String.valueOf(outputBufferSize));
    startProxy(proxyParams);
    startClient();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    int port = serverConnector.getLocalPort();
    client.newRequest("localhost", port).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    InputStream input = listener.getInputStream();
    for (int i = 0; i < chunk1.length; ++i) Assert.assertEquals(chunk1[i] & 0xFF, input.read());
    TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
    chunk1Latch.countDown();
    try {
        // Make sure the proxy does not receive chunk2.
        input.read();
        Assert.fail();
    } catch (EOFException x) {
    // Expected
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) ServletOutputStream(javax.servlet.ServletOutputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpServlet(javax.servlet.http.HttpServlet) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Random(java.util.Random) EOFException(java.io.EOFException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Aggregations

HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)30 Test (org.junit.Test)28 CountDownLatch (java.util.concurrent.CountDownLatch)23 HttpServletRequest (javax.servlet.http.HttpServletRequest)22 IOException (java.io.IOException)21 ServletException (javax.servlet.ServletException)21 HttpServletResponse (javax.servlet.http.HttpServletResponse)19 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 Request (org.eclipse.jetty.client.api.Request)17 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)14 Connection (org.eclipse.jetty.client.api.Connection)13 Result (org.eclipse.jetty.client.api.Result)12 ByteBuffer (java.nio.ByteBuffer)11 TimeUnit (java.util.concurrent.TimeUnit)11 Assert (org.junit.Assert)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)10 Response (org.eclipse.jetty.client.api.Response)9 ExecutionException (java.util.concurrent.ExecutionException)8 HttpConnectionOverHTTP (org.eclipse.jetty.client.http.HttpConnectionOverHTTP)8