Search in sources :

Example 1 with HttpSessionListener

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);
            }
        }
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) WriteAbortedException(java.io.WriteAbortedException)

Example 2 with HttpSessionListener

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);
}
Also used : ServletException(javax.servlet.ServletException) ListenerInfo(io.undertow.servlet.api.ListenerInfo) HttpSessionListener(javax.servlet.http.HttpSessionListener) ManagedListener(io.undertow.servlet.core.ManagedListener) ImmediateInstanceFactory(io.undertow.servlet.util.ImmediateInstanceFactory)

Example 3 with HttpSessionListener

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()));
            }
        }
    });
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) ServletContextAttributeEvent(javax.servlet.ServletContextAttributeEvent) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener)

Example 4 with HttpSessionListener

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();
    }
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 5 with HttpSessionListener

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();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

HttpSessionListener (javax.servlet.http.HttpSessionListener)22 HttpSessionEvent (javax.servlet.http.HttpSessionEvent)15 HttpSession (javax.servlet.http.HttpSession)9 IOException (java.io.IOException)7 ServletException (javax.servlet.ServletException)5 NotSerializableException (java.io.NotSerializableException)4 WriteAbortedException (java.io.WriteAbortedException)4 ServletContext (javax.servlet.ServletContext)4 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)4 ServletContextListener (javax.servlet.ServletContextListener)4 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)4 Context (org.apache.catalina.Context)4 SessionAuthentication (org.eclipse.jetty.security.authentication.SessionAuthentication)4 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)4 ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)3 ServletRequestListener (javax.servlet.ServletRequestListener)3 HttpSessionIdListener (javax.servlet.http.HttpSessionIdListener)3 Test (org.junit.Test)3 ListenerInfo (io.undertow.servlet.api.ListenerInfo)2 ManagedListener (io.undertow.servlet.core.ManagedListener)2