Search in sources :

Example 21 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder 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 22 with ServletHolder

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

the class CreationTest method testSessionCreateForward.

/**
     * Create a session in a context, forward to another context and create a 
     * session in it too. Check that both sessions exist after the response
     * completes.
     * @throws Exception
     */
@Test
public void testSessionCreateForward() throws Exception {
    String contextPath = "";
    String contextB = "/contextB";
    String servletMapping = "/server";
    int inactivePeriod = 20;
    int scavengePeriod = 3;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    _server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletHolder holder = new ServletHolder(_servlet);
    ServletContextHandler contextHandler = _server1.addContext(contextPath);
    contextHandler.addServlet(holder, servletMapping);
    ServletContextHandler ctxB = _server1.addContext(contextB);
    ctxB.addServlet(TestServletB.class, servletMapping);
    _server1.start();
    int port1 = _server1.getPort();
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        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 response = client.GET(url + "?action=forward");
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        //ensure work has finished on the server side
        _synchronizer.await();
        //check that the sessions exist persisted
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(_servlet._id));
        assertTrue(ctxB.getSessionHandler().getSessionCache().getSessionDataStore().exists(_servlet._id));
    } finally {
        _server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 23 with ServletHolder

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

the class CreationTest method testSessionCreateWithEviction.

/**
     * Test creating a session when the cache is set to
     * evict after the request exits.
     * @throws Exception
     */
@Test
public void testSessionCreateWithEviction() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.EVICT_ON_SESSION_EXIT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    _server1 = new TestServer(0, -1, -1, cacheFactory, storeFactory);
    ServletHolder holder = new ServletHolder(_servlet);
    ServletContextHandler contextHandler = _server1.addContext(contextPath);
    contextHandler.addServlet(holder, servletMapping);
    _servlet.setStore(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore());
    _server1.start();
    int port1 = _server1.getPort();
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping + "?action=create&check=false";
        //make a request to set up a session on the server
        ContentResponse response = client.GET(url);
        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=");
        //session should now be evicted from the cache
        String id = TestServer.extractSessionId(sessionCookie);
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //make another request for the same session
        Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=test");
        request.header("Cookie", sessionCookie);
        response = request.send();
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        //session should now be evicted from the cache again
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(TestServer.extractSessionId(sessionCookie)));
    } finally {
        _server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 24 with ServletHolder

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

the class DeleteUnloadableSessionTest method testDeleteUnloadableSession.

/**
     * Test that session data that can't be loaded results in a null Session object
     * @throws Exception
     */
@Test
public void testDeleteUnloadableSession() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = -1;
    int scavengePeriod = 100;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    cacheFactory.setRemoveUnloadableSessions(true);
    SessionDataStoreFactory storeFactory = new DelSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext(contextPath);
    TestServlet servlet = new TestServlet();
    ServletHolder holder = new ServletHolder(servlet);
    context.addServlet(holder, servletMapping);
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String sessionCookie = "JSESSIONID=w0rm3zxpa6h1zg1mevtv76b3te00.w0;$Path=/";
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
            request.header("Cookie", sessionCookie);
            ContentResponse response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            assertFalse(context.getSessionHandler().getSessionCache().getSessionDataStore().exists(TestServer.extractSessionId(sessionCookie)));
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : 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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 25 with ServletHolder

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

the class IdleSessionTest method testSessionIdle.

/**
     * @throws Exception
     */
@Test
public void testSessionIdle() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 20;
    int scavengePeriod = 3;
    int evictionSec = 5;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(evictionSec);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    _server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletHolder holder = new ServletHolder(_servlet);
    ServletContextHandler contextHandler = _server1.addContext(contextPath);
    contextHandler.addServlet(holder, servletMapping);
    _server1.start();
    int port1 = _server1.getPort();
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        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 response = client.GET(url + "?action=init");
        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=");
        //and wait until the session should be idled out
        pause(evictionSec * 3);
        //check that the session has been idled
        String id = TestServer.extractSessionId(sessionCookie);
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //make another request to de-idle the session
        Request request = client.newRequest(url + "?action=test");
        request.getHeaders().add("Cookie", sessionCookie);
        ContentResponse response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //check session de-idled
        assertTrue(contextHandler.getSessionHandler().getSessionCache().contains(id));
        //wait again for the session to be idled
        pause(evictionSec * 3);
        //check that it is
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //While idle, take some action to ensure that a deidle won't work, like
        //deleting the sessions in the store
        ((TestSessionDataStore) contextHandler.getSessionHandler().getSessionCache().getSessionDataStore())._map.clear();
        //make a request
        request = client.newRequest(url + "?action=testfail");
        request.getHeaders().add("Cookie", sessionCookie);
        response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //Test trying to de-idle an expired session (ie before the scavenger can get to it)
        //make a request to set up a session on the server
        response = client.GET(url + "?action=init");
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        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=");
        id = TestServer.extractSessionId(sessionCookie);
        //and wait until the session should be idled out
        pause(evictionSec * 3);
        //stop the scavenger
        if (_server1.getHouseKeeper() != null)
            _server1.getHouseKeeper().stop();
        //check that the session is idle
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //wait until the session should be expired
        pause(inactivePeriod + (3 * scavengePeriod));
        //make another request to de-idle the session
        request = client.newRequest(url + "?action=testfail");
        request.getHeaders().add("Cookie", sessionCookie);
        response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertFalse(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
    } finally {
        _server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Aggregations

ServletHolder (org.eclipse.jetty.servlet.ServletHolder)473 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)255 Server (org.eclipse.jetty.server.Server)177 Test (org.junit.Test)81 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)74 ServerConnector (org.eclipse.jetty.server.ServerConnector)67 HttpServletRequest (javax.servlet.http.HttpServletRequest)47 IOException (java.io.IOException)44 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)36 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)33 File (java.io.File)32 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)32 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)30 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)27 HttpServletResponse (javax.servlet.http.HttpServletResponse)26 HttpClient (org.eclipse.jetty.client.HttpClient)24 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 ServletException (javax.servlet.ServletException)23 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)23