Search in sources :

Example 1 with CachingSessionDataStore

use of org.eclipse.jetty.server.session.CachingSessionDataStore 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 2 with CachingSessionDataStore

use of org.eclipse.jetty.server.session.CachingSessionDataStore in project jetty.project by eclipse.

the class CachingSessionDataStoreTest method testSessionCRUD.

@Test
public void testSessionCRUD() throws Exception {
    String servletMapping = "/server";
    int scavengePeriod = -1;
    int maxInactivePeriod = -1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    SessionDataStoreFactory storeFactory = MemcachedTestHelper.newSessionDataStoreFactory();
    //Make sure sessions are evicted on request exit so they will need to be reloaded via cache/persistent store
    TestServer server = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext("/");
    context.addServlet(TestServlet.class, servletMapping);
    String contextPath = "";
    try {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            //
            //Create a session
            //
            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=");
            String id = TestServer.extractSessionId(sessionCookie);
            //check that the memcache contains the session, and the session data store contains the session
            CachingSessionDataStore ds = (CachingSessionDataStore) context.getSessionHandler().getSessionCache().getSessionDataStore();
            assertNotNull(ds);
            SessionDataStore persistentStore = ds.getSessionStore();
            SessionDataMap dataMap = ds.getSessionDataMap();
            //the backing persistent store contains the session
            assertNotNull(persistentStore.load(id));
            //the memcache cache contains the session
            assertNotNull(dataMap.load(id));
            //
            //Update a session and check that is is NOT loaded via the persistent store
            //
            ((MockDataStore) persistentStore).zeroLoadCount();
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=update");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            assertEquals(0, ((MockDataStore) persistentStore).getLoadCount());
            //check it was updated in the persistent store
            SessionData sd = persistentStore.load(id);
            assertNotNull(sd);
            assertEquals("bar", sd.getAttribute("foo"));
            //check it was updated in the cache
            sd = dataMap.load(id);
            assertNotNull(sd);
            assertEquals("bar", sd.getAttribute("foo"));
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) SessionDataStoreFactory(org.eclipse.jetty.server.session.SessionDataStoreFactory) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionData(org.eclipse.jetty.server.session.SessionData) TestServer(org.eclipse.jetty.server.session.TestServer) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) SessionDataStore(org.eclipse.jetty.server.session.SessionDataStore) SessionDataMap(org.eclipse.jetty.server.session.SessionDataMap) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) MockDataStore(org.eclipse.jetty.memcached.sessions.MemcachedTestHelper.MockDataStore) Test(org.junit.Test)

Aggregations

HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpClient (org.eclipse.jetty.client.HttpClient)2 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)2 Request (org.eclipse.jetty.client.api.Request)2 CachingSessionDataStore (org.eclipse.jetty.server.session.CachingSessionDataStore)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 Test (org.junit.Test)2 MockDataStore (org.eclipse.jetty.memcached.sessions.MemcachedTestHelper.MockDataStore)1 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)1 Server (org.eclipse.jetty.server.Server)1 DefaultSessionCacheFactory (org.eclipse.jetty.server.session.DefaultSessionCacheFactory)1 NullSessionDataStore (org.eclipse.jetty.server.session.NullSessionDataStore)1 SessionData (org.eclipse.jetty.server.session.SessionData)1 SessionDataMap (org.eclipse.jetty.server.session.SessionDataMap)1 SessionDataStore (org.eclipse.jetty.server.session.SessionDataStore)1 SessionDataStoreFactory (org.eclipse.jetty.server.session.SessionDataStoreFactory)1 TestServer (org.eclipse.jetty.server.session.TestServer)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1