Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4888 Ensure HttpSessionListener can access session via #5220

Merged
merged 1 commit into from Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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