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