Skip to content

Commit

Permalink
Issue #4752 Call HttpSessionListener.sessionCreated in add order.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Apr 7, 2020
1 parent a189d13 commit ffe5a28
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ public boolean addEventListener(EventListener listener)
}

/**
* Call the session lifecycle listeners
* Call the session lifecycle listeners in
* the reverse order they were added.
*
* @param session the session on which to call the lifecycle listeners
*/
Expand Down Expand Up @@ -310,7 +311,8 @@ public void run()
}

/**
* Call the session lifecycle listeners
* Call the session lifecycle listeners in the order
* they were added.
*
* @param session the session on which to call the lifecycle listeners
*/
Expand All @@ -322,9 +324,9 @@ protected void callSessionCreatedListeners(Session session)
if (_sessionListeners != null)
{
HttpSessionEvent event = new HttpSessionEvent(session);
for (int i = _sessionListeners.size() - 1; i >= 0; i--)
for (HttpSessionListener l : _sessionListeners)
{
_sessionListeners.get(i).sessionCreated(event);
l.sessionCreated(event);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
import java.util.Collections;
import java.util.HashSet;
import javax.servlet.SessionTrackingMode;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.eclipse.jetty.server.Server;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class SessionHandlerTest
Expand All @@ -35,7 +40,64 @@ public void testSessionTrackingMode()
SessionHandler sessionHandler = new SessionHandler();
sessionHandler.setSessionTrackingModes(new HashSet<>(Arrays.asList(SessionTrackingMode.COOKIE, SessionTrackingMode.URL)));
sessionHandler.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.SSL));
assertThrows(IllegalArgumentException.class,() ->
sessionHandler.setSessionTrackingModes(new HashSet<>(Arrays.asList(SessionTrackingMode.SSL, SessionTrackingMode.URL))));
assertThrows(IllegalArgumentException.class, () -> sessionHandler.setSessionTrackingModes(new HashSet<>(Arrays.asList(SessionTrackingMode.SSL, SessionTrackingMode.URL))));
}

@Test
public void testSessionListenerOrdering()
throws Exception
{
final StringBuffer result = new StringBuffer();

class Listener1 implements HttpSessionListener
{

@Override
public void sessionCreated(HttpSessionEvent se)
{
result.append("Listener1 create;");
}

@Override
public void sessionDestroyed(HttpSessionEvent se)
{
result.append("Listener1 destroy;");
}
}

class Listener2 implements HttpSessionListener
{

@Override
public void sessionCreated(HttpSessionEvent se)
{
result.append("Listener2 create;");
}

@Override
public void sessionDestroyed(HttpSessionEvent se)
{
result.append("Listener2 destroy;");
}

}

Server server = new Server();
SessionHandler sessionHandler = new SessionHandler();
try
{
sessionHandler.addEventListener(new Listener1());
sessionHandler.addEventListener(new Listener2());
sessionHandler.setServer(server);
sessionHandler.start();
Session session = new Session(sessionHandler, new SessionData("aa", "_", "0.0", 0, 0, 0, 0));
sessionHandler.callSessionCreatedListeners(session);
sessionHandler.callSessionDestroyedListeners(session);
assertEquals("Listener1 create;Listener2 create;Listener2 destroy;Listener1 destroy;", result.toString());
}
finally
{
sessionHandler.stop();
}
}
}

0 comments on commit ffe5a28

Please sign in to comment.