* Issue #1986 Support session listeners in ServletContextHandler.addEventListener method. Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
parent
d4061fcfeb
commit
b1c80ba231
|
@ -46,6 +46,11 @@ import javax.servlet.SessionTrackingMode;
|
|||
import javax.servlet.descriptor.JspConfigDescriptor;
|
||||
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
|
||||
import javax.servlet.descriptor.TaglibDescriptor;
|
||||
import javax.servlet.http.HttpSessionActivationListener;
|
||||
import javax.servlet.http.HttpSessionAttributeListener;
|
||||
import javax.servlet.http.HttpSessionBindingListener;
|
||||
import javax.servlet.http.HttpSessionIdListener;
|
||||
import javax.servlet.http.HttpSessionListener;
|
||||
|
||||
import org.eclipse.jetty.security.ConstraintAware;
|
||||
import org.eclipse.jetty.security.ConstraintMapping;
|
||||
|
@ -174,6 +179,27 @@ public class ServletContextHandler extends ContextHandler
|
|||
setErrorHandler(errorHandler);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Add EventListener
|
||||
* Adds an EventListener to the list. @see org.eclipse.jetty.server.handler.ContextHandler#addEventListener().
|
||||
* Also adds any listeners that are session related to the SessionHandler.
|
||||
* @param listener the listener to add
|
||||
*/
|
||||
@Override
|
||||
public void addEventListener(EventListener listener)
|
||||
{
|
||||
super.addEventListener(listener);
|
||||
if ((listener instanceof HttpSessionActivationListener)
|
||||
|| (listener instanceof HttpSessionAttributeListener)
|
||||
|| (listener instanceof HttpSessionBindingListener)
|
||||
|| (listener instanceof HttpSessionListener)
|
||||
|| (listener instanceof HttpSessionIdListener))
|
||||
{
|
||||
if (_sessionHandler!=null)
|
||||
_sessionHandler.addEventListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHandler(Handler handler)
|
||||
{
|
||||
|
|
|
@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.containsString;
|
|||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
@ -38,6 +39,10 @@ import javax.servlet.ServletException;
|
|||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSessionAttributeListener;
|
||||
import javax.servlet.http.HttpSessionBindingEvent;
|
||||
import javax.servlet.http.HttpSessionEvent;
|
||||
import javax.servlet.http.HttpSessionListener;
|
||||
|
||||
import org.eclipse.jetty.security.ConstraintSecurityHandler;
|
||||
import org.eclipse.jetty.security.SecurityHandler;
|
||||
|
@ -68,6 +73,57 @@ public class ServletContextHandlerTest
|
|||
|
||||
private static final AtomicInteger __testServlets = new AtomicInteger();
|
||||
|
||||
public static class MySessionHandler extends SessionHandler
|
||||
{
|
||||
public void checkSessionListeners (int size)
|
||||
{
|
||||
assertNotNull(_sessionListeners);
|
||||
assertEquals(size, _sessionListeners.size());
|
||||
}
|
||||
|
||||
public void checkSessionAttributeListeners(int size)
|
||||
{
|
||||
assertNotNull(_sessionAttributeListeners);
|
||||
assertEquals(size, _sessionAttributeListeners.size());
|
||||
}
|
||||
|
||||
public void checkSessionIdListeners(int size)
|
||||
{
|
||||
assertNotNull(_sessionIdListeners);
|
||||
assertEquals(size, _sessionIdListeners.size());
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyTestSessionListener implements HttpSessionAttributeListener, HttpSessionListener
|
||||
{
|
||||
|
||||
@Override
|
||||
public void sessionCreated(HttpSessionEvent se)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sessionDestroyed(HttpSessionEvent se)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributeAdded(HttpSessionBindingEvent event)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributeRemoved(HttpSessionBindingEvent event)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributeReplaced(HttpSessionBindingEvent event)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createServer()
|
||||
{
|
||||
|
@ -84,6 +140,24 @@ public class ServletContextHandlerTest
|
|||
_server.stop();
|
||||
_server.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddSessionListener() throws Exception
|
||||
{
|
||||
ContextHandlerCollection contexts = new ContextHandlerCollection();
|
||||
_server.setHandler(contexts);
|
||||
|
||||
ServletContextHandler root = new ServletContextHandler(contexts,"/",ServletContextHandler.SESSIONS);
|
||||
|
||||
MySessionHandler sessions = new MySessionHandler();
|
||||
root.setSessionHandler(sessions);
|
||||
assertNotNull(sessions);
|
||||
|
||||
root.addEventListener(new MyTestSessionListener());
|
||||
sessions.checkSessionAttributeListeners(1);
|
||||
sessions.checkSessionIdListeners(0);
|
||||
sessions.checkSessionListeners(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindContainer() throws Exception
|
||||
|
|
|
@ -1191,25 +1191,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
|
|||
super.setEventListeners(eventListeners);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Add EventListener
|
||||
* Convenience method that calls {@link #setEventListeners(EventListener[])}
|
||||
* @param listener the listener to add
|
||||
*/
|
||||
@Override
|
||||
public void addEventListener(EventListener listener)
|
||||
{
|
||||
super.addEventListener(listener);
|
||||
if ((listener instanceof HttpSessionActivationListener)
|
||||
|| (listener instanceof HttpSessionAttributeListener)
|
||||
|| (listener instanceof HttpSessionBindingListener)
|
||||
|| (listener instanceof HttpSessionListener)
|
||||
|| (listener instanceof HttpSessionIdListener))
|
||||
{
|
||||
if (_sessionHandler!=null)
|
||||
_sessionHandler.addEventListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removeEventListener(EventListener listener)
|
||||
|
|
|
@ -28,6 +28,7 @@ import java.io.IOException;
|
|||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -38,6 +39,8 @@ import javax.servlet.ServletContextListener;
|
|||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpSessionEvent;
|
||||
import javax.servlet.http.HttpSessionListener;
|
||||
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
|
@ -57,6 +60,44 @@ import org.junit.Test;
|
|||
|
||||
public class WebAppContextTest
|
||||
{
|
||||
public class MySessionListener implements HttpSessionListener
|
||||
{
|
||||
|
||||
@Override
|
||||
public void sessionCreated(HttpSessionEvent se)
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sessionDestroyed(HttpSessionEvent se)
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSessionListeners ()
|
||||
throws Exception
|
||||
{
|
||||
Server server = new Server();
|
||||
|
||||
WebAppContext wac = new WebAppContext();
|
||||
|
||||
wac.setServer(server);
|
||||
server.setHandler(wac);
|
||||
wac.addEventListener(new MySessionListener());
|
||||
|
||||
Collection<MySessionListener> listeners = wac.getSessionHandler().getBeans(org.eclipse.jetty.webapp.WebAppContextTest.MySessionListener.class);
|
||||
assertNotNull(listeners);
|
||||
assertEquals(1, listeners.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testConfigurationClassesFromDefault ()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue