Search in sources :

Example 16 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class HttpChannelAssociationTest method testAssociationFailedAbortsRequest.

@Test
public void testAssociationFailedAbortsRequest() throws Exception {
    startServer(new EmptyServerHandler());
    client = new HttpClient(newHttpClientTransport(transport, exchange -> false), sslContextFactory);
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    client.setExecutor(clientThreads);
    client.start();
    CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 17 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class HttpReceiverOverHTTP method releaseBuffer.

private void releaseBuffer() {
    if (buffer == null)
        throw new IllegalStateException();
    if (BufferUtil.hasContent(buffer))
        throw new IllegalStateException();
    HttpClient client = getHttpDestination().getHttpClient();
    ByteBufferPool bufferPool = client.getByteBufferPool();
    bufferPool.release(buffer);
    buffer = null;
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpClient(org.eclipse.jetty.client.HttpClient)

Example 18 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class HttpSenderOverHTTP method sendContent.

@Override
protected void sendContent(HttpExchange exchange, HttpContent content, Callback callback) {
    try {
        HttpClient client = getHttpChannel().getHttpDestination().getHttpClient();
        ByteBufferPool bufferPool = client.getByteBufferPool();
        ByteBuffer chunk = null;
        while (true) {
            ByteBuffer contentBuffer = content.getByteBuffer();
            boolean lastContent = content.isLast();
            HttpGenerator.Result result = generator.generateRequest(null, null, chunk, contentBuffer, lastContent);
            if (LOG.isDebugEnabled())
                LOG.debug("Generated content ({} bytes) - {}/{}", contentBuffer == null ? -1 : contentBuffer.remaining(), result, generator);
            switch(result) {
                case NEED_CHUNK:
                    {
                        chunk = bufferPool.acquire(HttpGenerator.CHUNK_SIZE, false);
                        break;
                    }
                case FLUSH:
                    {
                        EndPoint endPoint = getHttpChannel().getHttpConnection().getEndPoint();
                        if (chunk != null)
                            endPoint.write(new ByteBufferRecyclerCallback(callback, bufferPool, chunk), chunk, contentBuffer);
                        else
                            endPoint.write(callback, contentBuffer);
                        return;
                    }
                case SHUTDOWN_OUT:
                    {
                        shutdownOutput();
                        break;
                    }
                case CONTINUE:
                    {
                        if (lastContent)
                            break;
                        callback.succeeded();
                        return;
                    }
                case DONE:
                    {
                        callback.succeeded();
                        return;
                    }
                default:
                    {
                        throw new IllegalStateException(result.toString());
                    }
            }
        }
    } catch (Throwable x) {
        if (LOG.isDebugEnabled())
            LOG.debug(x);
        callback.failed(x);
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpClient(org.eclipse.jetty.client.HttpClient) EndPoint(org.eclipse.jetty.io.EndPoint) HttpGenerator(org.eclipse.jetty.http.HttpGenerator) ByteBuffer(java.nio.ByteBuffer)

Example 19 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class TestMemcachedSessions method testMemcached.

@Test
public void testMemcached() throws Exception {
    String contextPath = "/";
    Server server = new Server(0);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(System.getProperty("java.io.tmpdir"));
    server.setHandler(context);
    NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
    dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
    context.getSessionHandler().setSessionCache(dsc);
    // Add a test servlet
    ServletHolder h = new ServletHolder();
    h.setServlet(new TestServlet());
    context.addServlet(h, "/");
    try {
        server.start();
        int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            int value = 42;
            ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            String resp = response.getContentAsString();
            assertEquals(resp.trim(), String.valueOf(value));
            // Be sure the session value is still there
            Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals(String.valueOf(value), resp.trim());
            //Delete the session
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //Check that the session is gone
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals("No session", resp.trim());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpClient(org.eclipse.jetty.client.HttpClient) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 20 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class WebSocketClientInitTest method testInit_HttpClient_StartedOutside.

/**
     * This is the new Jetty 9.4 advanced usage mode of WebSocketClient,
     * that allows for more robust HTTP configurations (such as authentication,
     * cookies, and proxies)
     * 
     * @throws Exception
     *             on test failure
     */
@Test
public void testInit_HttpClient_StartedOutside() throws Exception {
    HttpClient http = new HttpClient();
    http.start();
    try {
        WebSocketClient ws = new WebSocketClient(http);
        ws.start();
        try {
            assertThat("HttpClient", ws.getHttpClient(), is(http));
            assertThat("WebSocketClient started", ws.isStarted(), is(true));
            assertThat("HttpClient started", http.isStarted(), is(true));
            HttpClient httpBean = ws.getBean(HttpClient.class);
            assertThat("HttpClient should not be found in WebSocketClient", httpBean, nullValue());
            assertThat("HttpClient bean is managed", ws.isManaged(httpBean), is(false));
            assertThat("WebSocketClient should not be found in HttpClient", http.getBean(WebSocketClient.class), nullValue());
        } finally {
            ws.stop();
        }
        assertThat("WebSocketClient stopped", ws.isStopped(), is(true));
        assertThat("HttpClient stopped", http.isStopped(), is(false));
    } finally {
        http.stop();
    }
    assertThat("HttpClient stopped", http.isStopped(), is(true));
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)135 Test (org.junit.Test)90 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)70 Request (org.eclipse.jetty.client.api.Request)44 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)23 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)18 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)8 URI (java.net.URI)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HttpProxy (org.eclipse.jetty.client.HttpProxy)7 Before (org.junit.Before)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)6 InputStream (java.io.InputStream)5 File (java.io.File)4 ContentExchange (org.eclipse.jetty.client.ContentExchange)4