Search in sources :

Example 6 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class ProxyServletFailureTest method prepareProxy.

private void prepareProxy(Map<String, String> initParams) throws Exception {
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName("proxy");
    proxy = new Server(executor);
    proxyConnector = new ServerConnector(proxy);
    proxy.addConnector(proxyConnector);
    proxyConnector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().setDelayDispatchUntilContent(false);
    ServletContextHandler proxyCtx = new ServletContextHandler(proxy, "/", true, false);
    ServletHolder proxyServletHolder = new ServletHolder(proxyServlet);
    proxyServletHolder.setInitParameters(initParams);
    proxyCtx.addServlet(proxyServletHolder, "/*");
    proxy.start();
    client = prepareClient();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 7 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class ProxyServletFailureTest method prepareServer.

private void prepareServer(HttpServlet servlet) throws Exception {
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName("server");
    server = new Server(executor);
    serverConnector = new ServerConnector(server);
    server.addConnector(serverConnector);
    ServletContextHandler appCtx = new ServletContextHandler(server, "/", true, false);
    ServletHolder appServletHolder = new ServletHolder(servlet);
    appCtx.addServlet(appServletHolder, "/*");
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 8 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class AbstractModifyMaxInactiveIntervalTest method testSessionExpiryAfterModifiedMaxInactiveInterval.

@Test
public void testSessionExpiryAfterModifiedMaxInactiveInterval() throws Exception {
    int oldMaxInactive = 4;
    int newMaxInactive = 20;
    int sleep = oldMaxInactive + (int) (oldMaxInactive * 0.8);
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, oldMaxInactive, __scavenge, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?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=");
            //do another request to change the maxinactive interval
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //wait for longer than the old inactive interval
            Thread.currentThread().sleep(sleep * 1000L);
            //do another request using the cookie to ensure the session is still there
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } 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 9 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction.

@Test
public void testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction() throws Exception {
    int oldMaxInactive = 10;
    int newMaxInactive = 2;
    int evict = 4;
    int sleep = evict;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(evict);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?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=");
            //do another request to reduce the maxinactive interval
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //do another request using the cookie to ensure the session is still there
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } 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 10 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project jetty.project by eclipse.

the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithImmortalSessionAndEviction.

@Test
public void testSetMaxInactiveIntervalWithImmortalSessionAndEviction() throws Exception {
    int oldMaxInactive = -1;
    //2min
    int newMaxInactive = 120;
    int evict = 2;
    int sleep = evict;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(evict);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?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=");
            //do another request to reduce the maxinactive interval
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //do another request using the cookie to ensure the session is still there
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } 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)

Aggregations

ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)385 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)249 Server (org.eclipse.jetty.server.Server)216 ServerConnector (org.eclipse.jetty.server.ServerConnector)98 Test (org.junit.Test)70 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)55 IOException (java.io.IOException)42 HttpClient (org.eclipse.jetty.client.HttpClient)39 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)39 HttpServletRequest (javax.servlet.http.HttpServletRequest)38 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)37 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)37 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)32 Connector (org.eclipse.jetty.server.Connector)29 Request (org.eclipse.jetty.client.api.Request)27 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)25 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)24 ServletException (javax.servlet.ServletException)22 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)22 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)22