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(""));
}
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();
}
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);
}
}
}
}
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();
}
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();
}
Aggregations