Search in sources :

Example 1 with DefaultSessionCache

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

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

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

Example 4 with DefaultSessionCache

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

the class TestServer method main.

public static void main(String[] args) throws Exception {
    ((StdErrLog) Log.getLog()).setSource(false);
    String jetty_root = "../../..";
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMaxThreads(100);
    // Setup server
    Server server = new Server(threadPool);
    server.manage(threadPool);
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);
    server.addBean(Log.getLog());
    // Common HTTP configuration
    HttpConfiguration config = new HttpConfiguration();
    config.setSecurePort(8443);
    config.addCustomizer(new ForwardedRequestCustomizer());
    config.addCustomizer(new SecureRequestCustomizer());
    config.setSendDateHeader(true);
    config.setSendServerVersion(true);
    // Http Connector
    HttpConnectionFactory http = new HttpConnectionFactory(config);
    ServerConnector httpConnector = new ServerConnector(server, http);
    httpConnector.setPort(8080);
    httpConnector.setIdleTimeout(30000);
    server.addConnector(httpConnector);
    // Handlers
    HandlerCollection handlers = new HandlerCollection();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
    // Add restart handler to test the ability to save sessions and restart
    RestartHandler restart = new RestartHandler();
    restart.setHandler(handlers);
    server.setHandler(restart);
    // Setup context
    HashLoginService login = new HashLoginService();
    login.setName("Test Realm");
    login.setConfig(jetty_root + "/tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/etc/realm.properties");
    server.addBean(login);
    File log = File.createTempFile("jetty-yyyy_mm_dd", "log");
    NCSARequestLog requestLog = new NCSARequestLog(log.toString());
    requestLog.setExtended(false);
    requestLogHandler.setRequestLog(requestLog);
    server.setStopAtShutdown(true);
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/test");
    webapp.setParentLoaderPriority(true);
    webapp.setResourceBase("./src/main/webapp");
    webapp.setAttribute("testAttribute", "testValue");
    File sessiondir = File.createTempFile("sessions", null);
    if (sessiondir.exists())
        sessiondir.delete();
    sessiondir.mkdir();
    sessiondir.deleteOnExit();
    DefaultSessionCache ss = new DefaultSessionCache(webapp.getSessionHandler());
    FileSessionDataStore sds = new FileSessionDataStore();
    ss.setSessionDataStore(sds);
    sds.setStoreDir(sessiondir);
    webapp.getSessionHandler().setSessionCache(ss);
    contexts.addHandler(webapp);
    ContextHandler srcroot = new ContextHandler();
    srcroot.setResourceBase(".");
    srcroot.setHandler(new ResourceHandler());
    srcroot.setContextPath("/src");
    contexts.addHandler(srcroot);
    server.start();
    server.join();
}
Also used : DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) StdErrLog(org.eclipse.jetty.util.log.StdErrLog) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HashLoginService(org.eclipse.jetty.security.HashLoginService) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) FileSessionDataStore(org.eclipse.jetty.server.session.FileSessionDataStore) File(java.io.File)

Example 5 with DefaultSessionCache

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

the class GCloudSessionTester method main.

public static void main(String[] args) throws Exception {
    if (args.length < 4)
        System.err.println("Usage: GCloudSessionTester projectid p12file password serviceaccount");
    System.setProperty("org.eclipse.jetty.server.session.LEVEL", "DEBUG");
    Server server = new Server(8080);
    HashLoginService loginService = new HashLoginService();
    loginService.setName("Test Realm");
    loginService.setConfig("../../jetty-distribution/target/distribution/demo-base/resources/realm.properties");
    server.addBean(loginService);
    DefaultSessionIdManager idmgr = new DefaultSessionIdManager(server);
    idmgr.setWorkerName("w1");
    server.setSessionIdManager(idmgr);
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar("../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
    webapp.addAliasCheck(new AllowSymLinkAliasChecker());
    GCloudSessionDataStore ds = new GCloudSessionDataStore();
    DefaultSessionCache ss = new DefaultSessionCache(webapp.getSessionHandler());
    webapp.getSessionHandler().setSessionCache(ss);
    ss.setSessionDataStore(ds);
    webapp.getSessionHandler().setSessionIdManager(idmgr);
    // A WebAppContext is a ContextHandler as well so it needs to be set to
    // the server so it is aware of where to send the appropriate requests.
    server.setHandler(webapp);
    // Start things up! 
    server.start();
    server.join();
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) HashLoginService(org.eclipse.jetty.security.HashLoginService) DefaultSessionIdManager(org.eclipse.jetty.server.session.DefaultSessionIdManager) AllowSymLinkAliasChecker(org.eclipse.jetty.server.handler.AllowSymLinkAliasChecker) Server(org.eclipse.jetty.server.Server)

Aggregations

DefaultSessionCache (org.eclipse.jetty.server.session.DefaultSessionCache)8 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)5 Server (org.eclipse.jetty.server.Server)4 DefaultSessionIdManager (org.eclipse.jetty.server.session.DefaultSessionIdManager)4 NullSessionDataStore (org.eclipse.jetty.server.session.NullSessionDataStore)3 File (java.io.File)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HashLoginService (org.eclipse.jetty.security.HashLoginService)2 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)2 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)2 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)2 ServerConnector (org.eclipse.jetty.server.ServerConnector)2 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)2 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)2 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)2 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)2 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)2 FileSessionDataStore (org.eclipse.jetty.server.session.FileSessionDataStore)2 SessionCache (org.eclipse.jetty.server.session.SessionCache)2