use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class AbstractSessionExpiryTest method testSessionExpiry.
/**
* Check that a session that expires whilst the server is stopped will not be
* able to be used when the server restarts
* @throws Exception
*/
@Test
public void testSessionExpiry() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 4;
int scavengePeriod = 1;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler context = server1.addContext(contextPath);
context.addServlet(holder, servletMapping);
TestHttpSessionListener listener = new TestHttpSessionListener();
context.getSessionHandler().addEventListener(listener);
server1.start();
int port1 = server1.getPort();
try {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.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 sessionId = TestServer.extractSessionId(sessionCookie);
verifySessionCreated(listener, sessionId);
//now stop the server
server1.stop();
//and wait until the session should have expired
pause(inactivePeriod);
//restart the server
server1.start();
//and wait until the scavenger has run
pause(inactivePeriod + (scavengePeriod * 2));
port1 = server1.getPort();
url = "http://localhost:" + port1 + contextPath + servletMapping;
//make another request, the session should have expired
Request request = client.newRequest(url + "?action=test");
request.getHeaders().add("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
String cookie2 = response2.getHeaders().get("Set-Cookie");
assertTrue(!cookie2.equals(sessionCookie));
verifySessionDestroyed(listener, sessionId);
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class AbstractSessionExpiryTest method testSessionExpiresWithListener.
@Test
public void testSessionExpiresWithListener() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 3;
int scavengePeriod = 1;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler context = server1.addContext(contextPath);
context.addServlet(holder, servletMapping);
TestHttpSessionListener listener = new TestHttpSessionListener(true);
context.getSessionHandler().addEventListener(listener);
server1.start();
int port1 = server1.getPort();
try {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.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 sessionId = TestServer.extractSessionId(sessionCookie);
verifySessionCreated(listener, sessionId);
//and wait until the session should have expired
pause(inactivePeriod + (scavengePeriod * 2));
verifySessionDestroyed(listener, sessionId);
assertNull(listener.ex);
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class AbstractSessionInvalidateCreateScavengeTest method testSessionScavenge.
@Test
public void testSessionScavenge() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 6;
int scavengePeriod = 3;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
TestServer server = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ServletContextHandler context = server.addContext(contextPath);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
context.addServlet(holder, servletMapping);
MySessionListener listener = new MySessionListener();
context.getSessionHandler().addEventListener(listener);
try {
server.start();
int port1 = server.getPort();
HttpClient client = new HttpClient();
client.start();
try {
String url = "http://localhost:" + port1 + contextPath + servletMapping;
// Create the session
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
// Make a request which will invalidate the existing session and create a new one
Request request2 = client.newRequest(url + "?action=test");
request2.header("Cookie", sessionCookie);
ContentResponse response2 = request2.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
// Wait for the scavenger to run
pause(inactivePeriod + (2 * scavengePeriod));
//test that the session created in the last test is scavenged:
//the HttpSessionListener should have been called when session1 was invalidated and session2 was scavenged
assertTrue(listener.destroys.size() == 2);
//ensure 2 different objects
assertTrue(listener.destroys.get(0) != listener.destroys.get(1));
//session2's HttpSessionBindingListener should have been called when it was scavenged
assertTrue(servlet.listener.unbound);
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class SessionExpiryTest method testChangeNewSessionTimeout.
@Test
public void testChangeNewSessionTimeout() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 10;
int scavengePeriod = 1;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
storeFactory.setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ImmediateChangeTimeoutServlet servlet = new ImmediateChangeTimeoutServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler context = server1.addContext(contextPath);
context.addServlet(holder, servletMapping);
TestHttpSessionListener listener = new TestHttpSessionListener();
context.getSessionHandler().addEventListener(listener);
server1.start();
int port1 = server1.getPort();
try {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//change from the sessionmanager configured default
inactivePeriod = 5;
//make a request to set up a session on the server and change its inactive setting straight away
ContentResponse response1 = client.GET(url + "?action=init&val=" + inactivePeriod);
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.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 sessionId = TestServer.extractSessionId(sessionCookie);
DBCollection sessions = MongoTestHelper.getCollection();
verifySessionCreated(listener, sessionId);
//verify that the session timeout is the new value and not the default
verifySessionTimeout(sessions, sessionId, inactivePeriod);
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class SessionExpiryTest method testBigSessionExpiry.
@Test
public void testBigSessionExpiry() throws Exception {
String contextPath = "";
String servletMapping = "/server";
//integer overflow
int inactivePeriod = Integer.MAX_VALUE * 60;
int scavengePeriod = 10;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
storeFactory.setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler context = server1.addContext(contextPath);
context.addServlet(holder, servletMapping);
TestHttpSessionListener listener = new TestHttpSessionListener();
context.getSessionHandler().addEventListener(listener);
server1.start();
int port1 = server1.getPort();
try {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.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 sessionId = TestServer.extractSessionId(sessionCookie);
DBCollection sessions = MongoTestHelper.getCollection();
verifySessionCreated(listener, sessionId);
//verify that the session timeout is set in mongo
//SessionManager sets -1 if maxInactive < 0
verifySessionTimeout(sessions, sessionId, -1);
//get the session expiry time from mongo
long expiry = getSessionExpiry(sessions, sessionId);
assertEquals(0, expiry);
} finally {
server1.stop();
}
}
Aggregations