Search in sources :

Example 56 with HttpClient

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

the class RemoveSessionTest method testRemoveSession.

@Test
public void testRemoveSession() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    TestServer server = new TestServer(0, -1, -1, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext(contextPath);
    context.addServlet(TestServlet.class, servletMapping);
    TestEventListener testListener = new TestEventListener();
    context.getSessionHandler().addEventListener(testListener);
    SessionHandler m = context.getSessionHandler();
    try {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
            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=");
            //ensure sessionCreated listener is called
            assertTrue(testListener.isCreated());
            assertEquals(1, m.getSessionsCreated());
            assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsMax());
            assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsTotal());
            //now delete the session
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=delete");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //ensure sessionDestroyed listener is called
            assertTrue(testListener.isDestroyed());
            assertEquals(0, ((DefaultSessionCache) m.getSessionCache()).getSessionsCurrent());
            assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsMax());
            assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsTotal());
            //check the session is not persisted any more
            assertFalse(m.getSessionCache().getSessionDataStore().exists(TestServer.extractSessionId(sessionCookie)));
            // The session is not there anymore, even if we present an old cookie
            request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=check");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            assertEquals(0, ((DefaultSessionCache) m.getSessionCache()).getSessionsCurrent());
            assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsMax());
            assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsTotal());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 57 with HttpClient

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

the class SameNodeLoadTest method testLoad.

@Test
@Slow
public void testLoad() throws Exception {
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    String contextPath = "";
    String servletMapping = "/server";
    TestServer server1 = new TestServer(0, -1, 4, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String url = "http://localhost:" + port1 + contextPath + servletMapping;
            //create session via first server
            ContentResponse response1 = client.GET(url + "?action=init");
            assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
            String sessionCookie = response1.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //simulate 10 clients making 100 requests each
            ExecutorService executor = Executors.newCachedThreadPool();
            int clientsCount = 10;
            CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
            int requestsCount = 100;
            Worker[] workers = new Worker[clientsCount];
            for (int i = 0; i < clientsCount; ++i) {
                workers[i] = new Worker(barrier, client, requestsCount, sessionCookie, url);
                executor.execute(workers[i]);
            }
            // Wait for all workers to be ready
            barrier.await();
            long start = System.nanoTime();
            // Wait for all workers to be done
            barrier.await();
            long end = System.nanoTime();
            long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
            System.err.println("Elapsed ms:" + elapsed);
            executor.shutdownNow();
            // Perform one request to get the result
            Request request = client.newRequest(url + "?action=result");
            request.header("Cookie", sessionCookie);
            ContentResponse response2 = request.send();
            assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            String response = response2.getContentAsString();
            assertEquals(response.trim(), String.valueOf(clientsCount * requestsCount));
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CyclicBarrier(java.util.concurrent.CyclicBarrier) HttpClient(org.eclipse.jetty.client.HttpClient) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 58 with HttpClient

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

the class HttpConnectionOverFCGI method acquireBuffer.

private ByteBuffer acquireBuffer() {
    HttpClient client = destination.getHttpClient();
    ByteBufferPool bufferPool = client.getByteBufferPool();
    return bufferPool.acquire(client.getResponseBufferSize(), true);
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpClient(org.eclipse.jetty.client.HttpClient)

Example 59 with HttpClient

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

the class AbstractHttpClientServerTest method start.

public void start(Handler handler) throws Exception {
    server = new Server();
    ServerFCGIConnectionFactory fcgiConnectionFactory = new ServerFCGIConnectionFactory(new HttpConfiguration());
    serverBufferPool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
    connector = new ServerConnector(server, null, null, serverBufferPool, 1, Math.max(1, Runtime.getRuntime().availableProcessors() / 2), fcgiConnectionFactory);
    //        connector.setPort(9000);
    server.addConnector(connector);
    server.setHandler(handler);
    server.start();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    client = new HttpClient(new HttpClientTransportOverFCGI(1, false, "") {

        @Override
        public HttpDestination newHttpDestination(Origin origin) {
            return new HttpDestinationOverFCGI(client, origin) {

                @Override
                protected DuplexConnectionPool newConnectionPool(HttpClient client) {
                    return new LeakTrackingConnectionPool(this, client.getMaxConnectionsPerDestination(), this) {

                        @Override
                        protected void leaked(LeakDetector.LeakInfo leakInfo) {
                            connectionLeaks.incrementAndGet();
                        }
                    };
                }
            };
        }
    }, null);
    client.setExecutor(executor);
    clientBufferPool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
    client.setByteBufferPool(clientBufferPool);
    client.start();
}
Also used : Origin(org.eclipse.jetty.client.Origin) HttpDestinationOverFCGI(org.eclipse.jetty.fcgi.client.http.HttpDestinationOverFCGI) Server(org.eclipse.jetty.server.Server) LeakTrackingByteBufferPool(org.eclipse.jetty.io.LeakTrackingByteBufferPool) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) LeakTrackingConnectionPool(org.eclipse.jetty.client.LeakTrackingConnectionPool) HttpClientTransportOverFCGI(org.eclipse.jetty.fcgi.client.http.HttpClientTransportOverFCGI)

Example 60 with HttpClient

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

the class FastCGIProxyServletTest method prepare.

public void prepare(HttpServlet servlet) throws Exception {
    QueuedThreadPool serverThreads = new QueuedThreadPool();
    serverThreads.setName("server");
    server = new Server(serverThreads);
    httpConnector = new ServerConnector(server);
    server.addConnector(httpConnector);
    fcgiConnector = new ServerConnector(server, new ServerFCGIConnectionFactory(new HttpConfiguration(), sendStatus200));
    server.addConnector(fcgiConnector);
    final String contextPath = "/";
    context = new ServletContextHandler(server, contextPath);
    final String servletPath = "/script";
    FastCGIProxyServlet fcgiServlet = new FastCGIProxyServlet() {

        @Override
        protected String rewriteTarget(HttpServletRequest request) {
            return "http://localhost:" + fcgiConnector.getLocalPort() + servletPath + request.getServletPath();
        }
    };
    ServletHolder fcgiServletHolder = new ServletHolder(fcgiServlet);
    fcgiServletHolder.setName("fcgi");
    fcgiServletHolder.setInitParameter(FastCGIProxyServlet.SCRIPT_ROOT_INIT_PARAM, "/scriptRoot");
    fcgiServletHolder.setInitParameter("proxyTo", "http://localhost");
    fcgiServletHolder.setInitParameter(FastCGIProxyServlet.SCRIPT_PATTERN_INIT_PARAM, "(.+?\\.php)");
    context.addServlet(fcgiServletHolder, "*.php");
    context.addServlet(new ServletHolder(servlet), servletPath + "/*");
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    client = new HttpClient();
    client.setExecutor(clientThreads);
    server.addBean(client);
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerFCGIConnectionFactory(org.eclipse.jetty.fcgi.server.ServerFCGIConnectionFactory) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)197 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)102 Test (org.junit.Test)94 Request (org.eclipse.jetty.client.api.Request)53 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 Test (org.testng.annotations.Test)34 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)24 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)19 CloudStore (com.yahoo.athenz.zts.store.CloudStore)17 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)16 HttpCertSigner (com.yahoo.athenz.zts.cert.impl.HttpCertSigner)14 HttpCertSignerFactory (com.yahoo.athenz.zts.cert.impl.HttpCertSignerFactory)14 URI (java.net.URI)11 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)11 ExecutionException (java.util.concurrent.ExecutionException)8 HttpProxy (org.eclipse.jetty.client.HttpProxy)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7