Skip to content

Commit

Permalink
Issue #4888 Ensure HttpSessionListener can access session via (#5220)
Browse files Browse the repository at this point in the history
Issue #4888 Ensure HttpSessionListener can call Request.getSession

Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Sep 2, 2020
1 parent 4e8bf37 commit ef0c752
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Expand Up @@ -767,7 +767,9 @@ public HttpSession newHttpSession(HttpServletRequest request)
try
{
_sessionCache.add(id, session);
Request.getBaseRequest(request).enterSession(session);
Request baseRequest = Request.getBaseRequest(request);
baseRequest.setSession(session);
baseRequest.enterSession(session);
_sessionsCreatedStats.increment();

if (request != null && request.isSecure())
Expand Down
Expand Up @@ -28,13 +28,17 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.servlet.ListenerHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.Source;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StacklessLogging;
Expand All @@ -56,7 +60,62 @@
*/
public class CreationTest
{
static ThreadLocal<HttpServletRequest> currentRequest = new ThreadLocal<>();

@Test
public void testRequestGetSessionInsideListener() throws Exception
{
String contextPath = "";
String servletMapping = "/server";

DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.EVICT_ON_SESSION_EXIT);
SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();

TestServer server1 = new TestServer(0, -1, -1, cacheFactory, storeFactory);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);

ListenerHolder h = contextHandler.getServletHandler().newListenerHolder(Source.EMBEDDED);
h.setListener(new MySessionListener());
contextHandler.getServletHandler().addListener(h);

TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
server1.getServerConnector().addBean(scopeListener);
contextHandler.addServlet(holder, servletMapping);
servlet.setStore(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore());
server1.start();
int port1 = server1.getPort();
try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session")))
{
HttpClient client = new HttpClient();
client.start();

//make a session
String url = "http://localhost:" + port1 + contextPath + servletMapping + "?action=create&check=false";

CountDownLatch synchronizer = new CountDownLatch(1);
scopeListener.setExitSynchronizer(synchronizer);

//make a request to set up a session on the server
ContentResponse response = client.GET(url);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());

String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);

//ensure request has finished being handled
synchronizer.await(5, TimeUnit.SECONDS);
}
finally
{
server1.stop();
}

}


/**
* Test creating a session when the cache is set to
* evict after the request exits.
Expand Down Expand Up @@ -387,6 +446,23 @@ public void testSessionCreateForwardAndInvalidate() throws Exception
}
}

public static class MySessionListener implements HttpSessionListener
{
@Override
public void sessionCreated(HttpSessionEvent se)
{
System.err.println("Session created");
currentRequest.get().getSession(true);
}

@Override
public void sessionDestroyed(HttpSessionEvent se)
{
System.err.println("Session destroyed");
}

}

public static class TestServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -436,6 +512,7 @@ else if (action != null && "test".equals(action))
}
else if (action != null && action.startsWith("create"))
{
currentRequest.set(request);
HttpSession session = request.getSession(true);
_id = session.getId();
session.setAttribute("value", new Integer(1));
Expand Down

0 comments on commit ef0c752

Please sign in to comment.