use of javax.servlet.http.HttpSessionListener in project tomcat by apache.
the class StandardSessionContext method tellNew.
/**
* Inform the listeners about the new session.
*
*/
public void tellNew() {
// Notify interested session event listeners
fireSessionEvent(Session.SESSION_CREATED_EVENT, null);
// Notify interested application event listeners
Context context = manager.getContext();
Object[] listeners = context.getApplicationLifecycleListeners();
if (listeners != null && listeners.length > 0) {
HttpSessionEvent event = new HttpSessionEvent(getSession());
for (int i = 0; i < listeners.length; i++) {
if (!(listeners[i] instanceof HttpSessionListener))
continue;
HttpSessionListener listener = (HttpSessionListener) listeners[i];
try {
context.fireContainerEvent("beforeSessionCreated", listener);
listener.sessionCreated(event);
context.fireContainerEvent("afterSessionCreated", listener);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
try {
context.fireContainerEvent("afterSessionCreated", listener);
} catch (Exception e) {
// Ignore
}
manager.getContext().getLogger().error(sm.getString("standardSession.sessionEvent"), t);
}
}
}
}
use of javax.servlet.http.HttpSessionListener in project wildfly by wildfly.
the class UndertowContext method addSessionListener.
@Override
public void addSessionListener(HttpSessionListener listener) {
ManagedListener ml = new ManagedListener(new ListenerInfo(HttpSessionListener.class, new ImmediateInstanceFactory<>(listener)), true);
try {
ml.start();
} catch (ServletException e) {
throw new RuntimeException(e);
}
this.deployment.getApplicationListeners().addListener(ml);
}
use of javax.servlet.http.HttpSessionListener in project felix by apache.
the class ProxyServletContextListener method contextInitialized.
// ---------- ServletContextListener
@Override
public void contextInitialized(final ServletContextEvent sce) {
this.servletContext = sce.getServletContext();
// add all required listeners
this.servletContext.addListener(new HttpSessionListener() {
private HttpSessionListener getHttpSessionListener() {
final EventDispatcherTracker tracker = eventDispatcherTracker;
if (tracker != null) {
return tracker.getHttpSessionListener();
}
return null;
}
@Override
public void sessionCreated(final HttpSessionEvent se) {
final HttpSessionListener sessionDispatcher = getHttpSessionListener();
if (sessionDispatcher != null) {
sessionDispatcher.sessionCreated(se);
}
}
@Override
public void sessionDestroyed(final HttpSessionEvent se) {
final HttpSessionListener sessionDispatcher = getHttpSessionListener();
if (sessionDispatcher != null) {
sessionDispatcher.sessionDestroyed(se);
}
}
});
this.servletContext.addListener(new HttpSessionIdListener() {
private HttpSessionIdListener getHttpSessionIdListener() {
final EventDispatcherTracker tracker = eventDispatcherTracker;
if (tracker != null) {
return tracker.getHttpSessionIdListener();
}
return null;
}
@Override
public void sessionIdChanged(final HttpSessionEvent event, final String oldSessionId) {
final HttpSessionIdListener sessionIdDispatcher = getHttpSessionIdListener();
if (sessionIdDispatcher != null) {
sessionIdDispatcher.sessionIdChanged(event, oldSessionId);
}
}
});
this.servletContext.addListener(new HttpSessionAttributeListener() {
private HttpSessionAttributeListener getHttpSessionAttributeListener() {
final EventDispatcherTracker tracker = eventDispatcherTracker;
if (tracker != null) {
return tracker.getHttpSessionAttributeListener();
}
return null;
}
@Override
public void attributeAdded(final HttpSessionBindingEvent se) {
final HttpSessionAttributeListener attributeDispatcher = getHttpSessionAttributeListener();
if (attributeDispatcher != null) {
attributeDispatcher.attributeAdded(se);
}
}
@Override
public void attributeRemoved(final HttpSessionBindingEvent se) {
final HttpSessionAttributeListener attributeDispatcher = getHttpSessionAttributeListener();
if (attributeDispatcher != null) {
attributeDispatcher.attributeRemoved(se);
}
}
@Override
public void attributeReplaced(final HttpSessionBindingEvent se) {
final HttpSessionAttributeListener attributeDispatcher = getHttpSessionAttributeListener();
if (attributeDispatcher != null) {
attributeDispatcher.attributeReplaced(se);
}
}
});
this.servletContext.addListener(new ServletContextAttributeListener() {
@Override
public void attributeAdded(final ServletContextAttributeEvent event) {
if (event.getName().equals(BundleContext.class.getName())) {
startTracker(event.getValue());
}
}
@Override
public void attributeRemoved(final ServletContextAttributeEvent event) {
if (event.getName().equals(BundleContext.class.getName())) {
stopTracker();
}
}
@Override
public void attributeReplaced(final ServletContextAttributeEvent event) {
if (event.getName().equals(BundleContext.class.getName())) {
stopTracker();
startTracker(event.getServletContext().getAttribute(event.getName()));
}
}
});
}
use of javax.servlet.http.HttpSessionListener in project felix by apache.
the class EventListenerTest method testHttpSessionListenerOk.
/**
* Tests that {@link HttpSessionListener}s are called whenever a session is created or destroyed.
*/
@Test
public void testHttpSessionListenerOk() throws Exception {
final CountDownLatch createdLatch = new CountDownLatch(1);
final CountDownLatch destroyedLatch = new CountDownLatch(1);
HttpSessionListener listener = new HttpSessionListener() {
@Override
public void sessionDestroyed(HttpSessionEvent se) {
destroyedLatch.countDown();
}
@Override
public void sessionCreated(HttpSessionEvent se) {
createdLatch.countDown();
}
};
ServiceRegistration<HttpSessionListener> reg = m_context.registerService(HttpSessionListener.class, listener, getListenerProps());
ServiceRegistration<Servlet> regS = m_context.registerService(Servlet.class, new TestServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setMaxInactiveInterval(2);
resp.setStatus(SC_OK);
resp.flushBuffer();
}
}, getServletProps("/session"));
try {
assertContent(SC_OK, null, createURL("/session"));
// Session should been created...
assertTrue(createdLatch.await(50, TimeUnit.SECONDS));
assertContent(SC_OK, null, createURL("/session"));
// Session should timeout automatically...
assertTrue(destroyedLatch.await(50, TimeUnit.SECONDS));
} finally {
reg.unregister();
regS.unregister();
}
}
use of javax.servlet.http.HttpSessionListener in project felix by apache.
the class EventListenerTest method testHttpSessionListenerOldWhiteboardOk.
/**
* Tests that {@link HttpSessionListener}s are called whenever a session is created or destroyed.
*/
@Test
public void testHttpSessionListenerOldWhiteboardOk() throws Exception {
final CountDownLatch createdLatch = new CountDownLatch(1);
final CountDownLatch destroyedLatch = new CountDownLatch(1);
HttpSessionListener listener = new HttpSessionListener() {
@Override
public void sessionDestroyed(HttpSessionEvent se) {
destroyedLatch.countDown();
}
@Override
public void sessionCreated(HttpSessionEvent se) {
createdLatch.countDown();
}
};
ServiceRegistration<HttpSessionListener> reg = m_context.registerService(HttpSessionListener.class, listener, null);
register("/session", new TestServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setMaxInactiveInterval(2);
resp.setStatus(SC_OK);
resp.flushBuffer();
}
});
try {
assertContent(SC_OK, null, createURL("/session"));
// Session should been created...
assertTrue(createdLatch.await(50, TimeUnit.SECONDS));
assertContent(SC_OK, null, createURL("/session"));
// Session should timeout automatically...
assertTrue(destroyedLatch.await(50, TimeUnit.SECONDS));
} finally {
reg.unregister();
}
}
Aggregations