Search in sources :

Example 21 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project camel by apache.

the class WebsocketComponent method createStaticResourcesServer.

protected Server createStaticResourcesServer(Server server, ServletContextHandler context, String home) throws Exception {
    context.setContextPath("/");
    SessionHandler sh = new SessionHandler();
    context.setSessionHandler(sh);
    if (home != null) {
        String[] resources = home.split(":");
        if (LOG.isDebugEnabled()) {
            LOG.debug(">>> Protocol found: " + resources[0] + ", and resource: " + resources[1]);
        }
        if (resources[0].equals("classpath")) {
            context.setBaseResource(new JettyClassPathResource(getCamelContext().getClassResolver(), resources[1]));
        } else if (resources[0].equals("file")) {
            context.setBaseResource(Resource.newResource(resources[1]));
        }
        DefaultServlet defaultServlet = new DefaultServlet();
        ServletHolder holder = new ServletHolder(defaultServlet);
        // avoid file locking on windows
        // http://stackoverflow.com/questions/184312/how-to-make-jetty-dynamically-load-static-pages
        holder.setInitParameter("useFileMappedBuffer", "false");
        context.addServlet(holder, "/");
    }
    server.setHandler(context);
    return server;
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet)

Example 22 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project dropwizard by dropwizard.

the class ServletEnvironmentTest method setsSessionHandlers.

@Test
public void setsSessionHandlers() throws Exception {
    final SessionHandler sessionHandler = mock(SessionHandler.class);
    environment.setSessionHandler(sessionHandler);
    verify(handler).setSessionHandler(sessionHandler);
    verify(handler).setSessionsEnabled(true);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Test(org.junit.Test)

Example 23 with SessionHandler

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

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

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

the class ServerConnectorTimeoutTest method testIdleTimeoutAfterSuspend.

@Test(timeout = 60000)
public void testIdleTimeoutAfterSuspend() throws Exception {
    SuspendHandler _handler = new SuspendHandler();
    _server.stop();
    SessionHandler session = new SessionHandler();
    session.setHandler(_handler);
    _server.setHandler(session);
    _server.start();
    _handler.setSuspendFor(100);
    _handler.setResumeAfter(25);
    assertTrue(process(null).toUpperCase(Locale.ENGLISH).contains("RESUMED"));
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Test(org.junit.Test)

Aggregations

SessionHandler (org.eclipse.jetty.server.session.SessionHandler)43 Server (org.eclipse.jetty.server.Server)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)10 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)10 Test (org.junit.Test)10 IOException (java.io.IOException)8 ServletException (javax.servlet.ServletException)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 HashSessionManager (org.eclipse.jetty.server.session.HashSessionManager)5 File (java.io.File)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpSession (javax.servlet.http.HttpSession)4 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)4 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)4 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 BeforeClass (org.junit.BeforeClass)4 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)3 LocalConnector (org.eclipse.jetty.server.LocalConnector)3 ServerConnector (org.eclipse.jetty.server.ServerConnector)3 SessionManager (org.eclipse.jetty.server.SessionManager)3