Search in sources :

Example 1 with NullSessionDataStore

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

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

the class ResponseTest method testEncodeRedirect.

@Test
public void testEncodeRedirect() throws Exception {
    Response response = getResponse();
    Request request = response.getHttpChannel().getRequest();
    request.setAuthority("myhost", 8888);
    request.setContextPath("/path");
    assertEquals("http://myhost:8888/path/info;param?query=0&more=1#target", response.encodeURL("http://myhost:8888/path/info;param?query=0&more=1#target"));
    request.setRequestedSessionId("12345");
    request.setRequestedSessionIdFromCookie(false);
    SessionHandler handler = new SessionHandler();
    DefaultSessionCache ss = new DefaultSessionCache(handler);
    NullSessionDataStore ds = new NullSessionDataStore();
    ss.setSessionDataStore(ds);
    DefaultSessionIdManager idMgr = new DefaultSessionIdManager(_server);
    idMgr.setWorkerName(null);
    handler.setSessionIdManager(idMgr);
    request.setSessionHandler(handler);
    TestSession tsession = new TestSession(handler, "12345");
    tsession.setExtendedId(handler.getSessionIdManager().getExtendedId("12345", null));
    request.setSession(tsession);
    handler.setCheckingRemoteSessionIdEncoding(false);
    assertEquals("http://myhost:8888/path/info;param;jsessionid=12345?query=0&more=1#target", response.encodeURL("http://myhost:8888/path/info;param?query=0&more=1#target"));
    assertEquals("http://other:8888/path/info;param;jsessionid=12345?query=0&more=1#target", response.encodeURL("http://other:8888/path/info;param?query=0&more=1#target"));
    assertEquals("http://myhost/path/info;param;jsessionid=12345?query=0&more=1#target", response.encodeURL("http://myhost/path/info;param?query=0&more=1#target"));
    assertEquals("http://myhost:8888/other/info;param;jsessionid=12345?query=0&more=1#target", response.encodeURL("http://myhost:8888/other/info;param?query=0&more=1#target"));
    handler.setCheckingRemoteSessionIdEncoding(true);
    assertEquals("http://myhost:8888/path/info;param;jsessionid=12345?query=0&more=1#target", response.encodeURL("http://myhost:8888/path/info;param?query=0&more=1#target"));
    assertEquals("http://other:8888/path/info;param?query=0&more=1#target", response.encodeURL("http://other:8888/path/info;param?query=0&more=1#target"));
    assertEquals("http://myhost/path/info;param?query=0&more=1#target", response.encodeURL("http://myhost/path/info;param?query=0&more=1#target"));
    assertEquals("http://myhost:8888/other/info;param?query=0&more=1#target", response.encodeURL("http://myhost:8888/other/info;param?query=0&more=1#target"));
    request.setContextPath("");
    assertEquals("http://myhost:8888/;jsessionid=12345", response.encodeURL("http://myhost:8888"));
    assertEquals("https://myhost:8888/;jsessionid=12345", response.encodeURL("https://myhost:8888"));
    assertEquals("mailto:/foo", response.encodeURL("mailto:/foo"));
    assertEquals("http://myhost:8888/;jsessionid=12345", response.encodeURL("http://myhost:8888/"));
    assertEquals("http://myhost:8888/;jsessionid=12345", response.encodeURL("http://myhost:8888/;jsessionid=7777"));
    assertEquals("http://myhost:8888/;param;jsessionid=12345?query=0&more=1#target", response.encodeURL("http://myhost:8888/;param?query=0&more=1#target"));
    assertEquals("http://other:8888/path/info;param?query=0&more=1#target", response.encodeURL("http://other:8888/path/info;param?query=0&more=1#target"));
    handler.setCheckingRemoteSessionIdEncoding(false);
    assertEquals("/foo;jsessionid=12345", response.encodeURL("/foo"));
    assertEquals("/;jsessionid=12345", response.encodeURL("/"));
    assertEquals("/foo.html;jsessionid=12345#target", response.encodeURL("/foo.html#target"));
    assertEquals(";jsessionid=12345", response.encodeURL(""));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) DefaultSessionIdManager(org.eclipse.jetty.server.session.DefaultSessionIdManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) Test(org.junit.Test)

Example 3 with NullSessionDataStore

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

the class OneServletContextWithSession method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    // Create a ServletContext, with a session handler enabled.
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(System.getProperty("java.io.tmpdir"));
    server.setHandler(context);
    // Access the SessionHandler from the context.
    SessionHandler sessions = context.getSessionHandler();
    // Explicitly set Session Cache and null Datastore.
    // This is normally done by default,
    // but is done explicitly here for demonstration.
    // If more than one context is to be deployed, it is
    // simpler to use SessionCacheFactory and/or
    // SessionDataStoreFactory instances set as beans on 
    // the server.
    SessionCache cache = new DefaultSessionCache(sessions);
    cache.setSessionDataStore(new NullSessionDataStore());
    sessions.setSessionCache(cache);
    // Servlet to read/set the greeting stored in the session.
    // Can be accessed using http://localhost:8080/hello
    context.addServlet(HelloSessionServlet.class, "/");
    server.start();
    server.join();
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) Server(org.eclipse.jetty.server.Server) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) SessionCache(org.eclipse.jetty.server.session.SessionCache)

Example 4 with NullSessionDataStore

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

the class ResponseTest method testSendRedirect.

@Test
public void testSendRedirect() throws Exception {
    String[][] tests = { // No cookie
    { "http://myhost:8888/other/location;jsessionid=12345?name=value", "http://myhost:8888/other/location;jsessionid=12345?name=value" }, { "/other/location;jsessionid=12345?name=value", "http://@HOST@@PORT@/other/location;jsessionid=12345?name=value" }, { "./location;jsessionid=12345?name=value", "http://@HOST@@PORT@/path/location;jsessionid=12345?name=value" }, // From cookie
    { "/other/location", "http://@HOST@@PORT@/other/location" }, { "/other/l%20cation", "http://@HOST@@PORT@/other/l%20cation" }, { "location", "http://@HOST@@PORT@/path/location" }, { "./location", "http://@HOST@@PORT@/path/location" }, { "../location", "http://@HOST@@PORT@/location" }, { "/other/l%20cation", "http://@HOST@@PORT@/other/l%20cation" }, { "l%20cation", "http://@HOST@@PORT@/path/l%20cation" }, { "./l%20cation", "http://@HOST@@PORT@/path/l%20cation" }, { "../l%20cation", "http://@HOST@@PORT@/l%20cation" }, { "../locati%C3%abn", "http://@HOST@@PORT@/locati%C3%abn" }, { "../other%2fplace", "http://@HOST@@PORT@/other%2fplace" }, { "http://somehost.com/other/location", "http://somehost.com/other/location" } };
    int[] ports = new int[] { 8080, 80 };
    String[] hosts = new String[] { null, "myhost", "192.168.0.1", "0::1" };
    for (int port : ports) {
        for (String host : hosts) {
            for (int i = 0; i < tests.length; i++) {
                // System.err.printf("%s %d %s%n",host,port,tests[i][0]);
                Response response = getResponse();
                Request request = response.getHttpChannel().getRequest();
                request.setScheme("http");
                if (host != null)
                    request.setAuthority(host, port);
                request.setURIPathQuery("/path/info;param;jsessionid=12345?query=0&more=1#target");
                request.setContextPath("/path");
                request.setRequestedSessionId("12345");
                request.setRequestedSessionIdFromCookie(i > 2);
                SessionHandler handler = new SessionHandler();
                NullSessionDataStore ds = new NullSessionDataStore();
                DefaultSessionCache ss = new DefaultSessionCache(handler);
                handler.setSessionCache(ss);
                ss.setSessionDataStore(ds);
                DefaultSessionIdManager idMgr = new DefaultSessionIdManager(_server);
                idMgr.setWorkerName(null);
                handler.setSessionIdManager(idMgr);
                request.setSessionHandler(handler);
                request.setSession(new TestSession(handler, "12345"));
                handler.setCheckingRemoteSessionIdEncoding(false);
                response.sendRedirect(tests[i][0]);
                String location = response.getHeader("Location");
                String expected = tests[i][1].replace("@HOST@", host == null ? request.getLocalAddr() : (host.contains(":") ? ("[" + host + "]") : host)).replace("@PORT@", host == null ? ":8888" : (port == 80 ? "" : (":" + port)));
                assertEquals("test-" + i + " " + host + ":" + port, expected, location);
            }
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) DefaultSessionIdManager(org.eclipse.jetty.server.session.DefaultSessionIdManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) AbstractEndPoint(org.eclipse.jetty.io.AbstractEndPoint) Test(org.junit.Test)

Aggregations

NullSessionDataStore (org.eclipse.jetty.server.session.NullSessionDataStore)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 DefaultSessionCache (org.eclipse.jetty.server.session.DefaultSessionCache)3 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)3 Test (org.junit.Test)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Server (org.eclipse.jetty.server.Server)2 DefaultSessionIdManager (org.eclipse.jetty.server.session.DefaultSessionIdManager)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 HttpClient (org.eclipse.jetty.client.HttpClient)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Request (org.eclipse.jetty.client.api.Request)1 AbstractEndPoint (org.eclipse.jetty.io.AbstractEndPoint)1 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)1 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)1 CachingSessionDataStore (org.eclipse.jetty.server.session.CachingSessionDataStore)1 SessionCache (org.eclipse.jetty.server.session.SessionCache)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1