Search in sources :

Example 16 with ServletContextHandler

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

the class SessionExpiryTest method testChangeNewSessionTimeout.

@Test
public void testChangeNewSessionTimeout() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 10;
    int scavengePeriod = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ImmediateChangeTimeoutServlet servlet = new ImmediateChangeTimeoutServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //change from the sessionmanager configured default
        inactivePeriod = 5;
        //make a request to set up a session on the server and change its inactive setting straight away
        ContentResponse response1 = client.GET(url + "?action=init&val=" + inactivePeriod);
        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=");
        String sessionId = TestServer.extractSessionId(sessionCookie);
        DBCollection sessions = MongoTestHelper.getCollection();
        verifySessionCreated(listener, sessionId);
        //verify that the session timeout is the new value and not the default
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
    } finally {
        server1.stop();
    }
}
Also used : DBCollection(com.mongodb.DBCollection) DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) TestServer(org.eclipse.jetty.server.session.TestServer) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 17 with ServletContextHandler

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

the class SessionExpiryTest method testBigSessionExpiry.

@Test
public void testBigSessionExpiry() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    //integer overflow
    int inactivePeriod = Integer.MAX_VALUE * 60;
    int scavengePeriod = 10;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the 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=");
        String sessionId = TestServer.extractSessionId(sessionCookie);
        DBCollection sessions = MongoTestHelper.getCollection();
        verifySessionCreated(listener, sessionId);
        //verify that the session timeout is set in mongo
        //SessionManager sets -1 if maxInactive < 0
        verifySessionTimeout(sessions, sessionId, -1);
        //get the session expiry time from mongo
        long expiry = getSessionExpiry(sessions, sessionId);
        assertEquals(0, expiry);
    } finally {
        server1.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) TestServer(org.eclipse.jetty.server.session.TestServer) DBCollection(com.mongodb.DBCollection) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 18 with ServletContextHandler

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

the class SessionExpiryTest method changeSessionTimeout.

@Test
public void changeSessionTimeout() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 10;
    int scavengePeriod = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the 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=");
        String sessionId = TestServer.extractSessionId(sessionCookie);
        DBCollection sessions = MongoTestHelper.getCollection();
        verifySessionCreated(listener, sessionId);
        //verify that the session timeout is set in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        //get the session expiry time from mongo
        long expiry = getSessionExpiry(sessions, sessionId);
        //make another request to change the session timeout to a smaller value
        inactivePeriod = 5;
        Request request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
        request.getHeaders().add("Cookie", sessionCookie);
        ContentResponse response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //check the timeout in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        //check the session expiry time has decreased from previous value
        assertTrue(getSessionExpiry(sessions, sessionId) < expiry);
        expiry = getSessionExpiry(sessions, sessionId);
        //increase the session timeout
        inactivePeriod = 20;
        request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
        request.getHeaders().add("Cookie", sessionCookie);
        response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //verify that the session timeout is set in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        long latestExpiry = getSessionExpiry(sessions, sessionId);
        assertTrue(latestExpiry > expiry);
        assertTrue(getSessionAccessed(sessions, sessionId) + (1000L * inactivePeriod) <= getSessionExpiry(sessions, sessionId));
        //old inactive expired in 5, new inactive expired in 20
        assertTrue(latestExpiry >= 15);
    } finally {
        server1.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) 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) TestServer(org.eclipse.jetty.server.session.TestServer) DBCollection(com.mongodb.DBCollection) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 19 with ServletContextHandler

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

the class AbstractClusteredSessionScavengingTest method testNoScavenging.

@Test
public void testNoScavenging() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 3;
    int scavengePeriod = 0;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context1 = server1.addContext(contextPath);
    context1.addServlet(TestServlet.class, servletMapping);
    SessionListener listener = new SessionListener();
    context1.getSessionHandler().addEventListener(listener);
    try {
        server1.start();
        int port1 = server1.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String url = "http://localhost:" + port1 + contextPath + servletMapping;
            // Create the session
            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=");
            SessionHandler m1 = context1.getSessionHandler();
            assertEquals(1, m1.getSessionsCreated());
            // Wait a while to ensure that the session should have expired, if the
            //scavenger was running
            pause(2 * inactivePeriod);
            assertEquals(1, m1.getSessionsCreated());
            if (m1 instanceof TestSessionHandler) {
                ((TestSessionHandler) m1).assertCandidatesForExpiry(0);
            }
            //check a session removed listener did not get called
            assertEquals(1, listener.count.getCurrent());
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 20 with ServletContextHandler

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

the class AbstractModifyMaxInactiveIntervalTest method testNoExpireSessionInUse.

@Test
public void testNoExpireSessionInUse() throws Exception {
    int maxInactive = 3;
    int sleep = maxInactive + (int) (maxInactive * 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, maxInactive, 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 that will sleep long enough for the session expiry time to have passed
            //before trying to access the session and ensure it is still there
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=sleep&val=" + sleep);
            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