public static void main(String args[]) throws Exception { Server server = new Server(8080); // Specify the Session ID Manager HashSessionIdManager idmanager = new HashSessionIdManager(); server.setSessionIdManager(idmanager); // Sessions are bound to a context. ContextHandler context = new ContextHandler("/"); server.setHandler(context); // Create the SessionHandler (wrapper) to handle the sessions HashSessionManager manager = new HashSessionManager(); SessionHandler sessions = new SessionHandler(manager); context.setHandler(sessions); // ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); // ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); // context.setContextPath("/"); // server.setHandler(h); // server.setSessionIdManager(sessionIdManager); /* context.addServlet(new ServletHolder(new HelloServlet()),"/*"); context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*"); context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*"); */ Canvas c = new Canvas(800, 900); context.setAttribute("myCanvas", c); sessions.setHandler(new HttpHandler()); server.start(); server.join(); }
public EmbeddedJettyServer(int port, boolean useRestSession) throws Exception { server = new Server(); connector = new SelectChannelConnector(); connector.setPort(port); server.addConnector(connector); root = new WebAppContext(); root.setContextPath("/"); root.setResourceBase("."); if (useRestSession) { RestSessionIdManager idManager = new RestSessionIdManager(); RestSessionManager sessionManager = new RestSessionManager(); server.setSessionIdManager(idManager); sessionManager.setSessionIdManager(idManager); SessionHandler sessionHandler = new SessionHandler(); sessionHandler.setSessionManager(sessionManager); root.setSessionHandler(sessionHandler); root.setClassLoader(getContextClassLoader()); } server.setHandler(root); server.start(); while (!server.isStarted()) { Thread.sleep(100); } }
/** @param sessionHandler The sessionHandler to set. */ public void setSessionHandler(SessionHandler sessionHandler) { if (isStarted()) throw new IllegalStateException("STARTED"); Handler next = null; if (_sessionHandler != null) { next = _sessionHandler.getHandler(); _sessionHandler.setHandler(null); replaceHandler(_sessionHandler, sessionHandler); } _sessionHandler = sessionHandler; if (next != null && _sessionHandler.getHandler() == null) _sessionHandler.setHandler(next); relinkHandlers(); }
private void persistSession(WebAppContext webApp) { String storeDir = getStoreDir(); SessionManager sm = webApp.getSessionHandler().getSessionManager(); if (sm instanceof HashSessionManager) { ((HashSessionManager) sm).setStoreDirectory(new File(storeDir)); return; } HashSessionManager hsm = new HashSessionManager(); hsm.setStoreDirectory(new File(storeDir)); SessionHandler sh = new SessionHandler(); sh.setSessionManager(hsm); webApp.setSessionHandler(sh); }
@Before public void setupSecurity() { _security = new ConstraintSecurityHandler(); _session.setHandler(_security); RequestHandler _handler = new RequestHandler(); _security.setHandler(_handler); /* <security-constraint> <web-resource-collection> <web-resource-name>precluded methods</web-resource-name> <url-pattern>/*</url-pattern> <url-pattern>/acme/wholesale/*</url-pattern> <url-pattern>/acme/retail/*</url-pattern> <http-method-exception>GET</http-method-exception> <http-method-exception>POST</http-method-exception> </web-resource-collection> <auth-constraint/> </security-constraint> */ Constraint constraint0 = new Constraint(); constraint0.setAuthenticate(true); constraint0.setName("precluded methods"); ConstraintMapping mapping0 = new ConstraintMapping(); mapping0.setPathSpec("/*"); mapping0.setConstraint(constraint0); mapping0.setMethodOmissions(new String[] {"GET", "POST"}); ConstraintMapping mapping1 = new ConstraintMapping(); mapping1.setPathSpec("/acme/wholesale/*"); mapping1.setConstraint(constraint0); mapping1.setMethodOmissions(new String[] {"GET", "POST"}); ConstraintMapping mapping2 = new ConstraintMapping(); mapping2.setPathSpec("/acme/retail/*"); mapping2.setConstraint(constraint0); mapping2.setMethodOmissions(new String[] {"GET", "POST"}); /* <security-constraint> <web-resource-collection> <web-resource-name>wholesale</web-resource-name> <url-pattern>/acme/wholesale/*</url-pattern> <http-method>GET</http-method> <http-method>PUT</http-method> </web-resource-collection> <auth-constraint> <role-name>SALESCLERK</role-name> </auth-constraint> </security-constraint> */ Constraint constraint1 = new Constraint(); constraint1.setAuthenticate(true); constraint1.setName("wholesale"); constraint1.setRoles(new String[] {"SALESCLERK"}); ConstraintMapping mapping3 = new ConstraintMapping(); mapping3.setPathSpec("/acme/wholesale/*"); mapping3.setConstraint(constraint1); mapping3.setMethod("GET"); ConstraintMapping mapping4 = new ConstraintMapping(); mapping4.setPathSpec("/acme/wholesale/*"); mapping4.setConstraint(constraint1); mapping4.setMethod("PUT"); /* <security-constraint> <web-resource-collection> <web-resource-name>wholesale 2</web-resource-name> <url-pattern>/acme/wholesale/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>CONTRACTOR</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> */ Constraint constraint2 = new Constraint(); constraint2.setAuthenticate(true); constraint2.setName("wholesale 2"); constraint2.setRoles(new String[] {"CONTRACTOR"}); constraint2.setDataConstraint(Constraint.DC_CONFIDENTIAL); ConstraintMapping mapping5 = new ConstraintMapping(); mapping5.setPathSpec("/acme/wholesale/*"); mapping5.setMethod("GET"); mapping5.setConstraint(constraint2); ConstraintMapping mapping6 = new ConstraintMapping(); mapping6.setPathSpec("/acme/wholesale/*"); mapping6.setMethod("POST"); mapping6.setConstraint(constraint2); /* <security-constraint> <web-resource-collection> <web-resource-name>retail</web-resource-name> <url-pattern>/acme/retail/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>CONTRACTOR</role-name> <role-name>HOMEOWNER</role-name> </auth-constraint> </security-constraint> */ Constraint constraint4 = new Constraint(); constraint4.setName("retail"); constraint4.setAuthenticate(true); constraint4.setRoles(new String[] {"CONTRACTOR", "HOMEOWNER"}); ConstraintMapping mapping7 = new ConstraintMapping(); mapping7.setPathSpec("/acme/retail/*"); mapping7.setMethod("GET"); mapping7.setConstraint(constraint4); ConstraintMapping mapping8 = new ConstraintMapping(); mapping8.setPathSpec("/acme/retail/*"); mapping8.setMethod("POST"); mapping8.setConstraint(constraint4); Set<String> knownRoles = new HashSet<String>(); knownRoles.add("CONTRACTOR"); knownRoles.add("HOMEOWNER"); knownRoles.add("SALESCLERK"); _security.setConstraintMappings( Arrays.asList( new ConstraintMapping[] { mapping0, mapping1, mapping2, mapping3, mapping4, mapping5, mapping6, mapping7, mapping8 }), knownRoles); }