Search in sources :

Example 16 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project tomee by apache.

the class HttpSessionImpl method getSessionContext.

@Override
public HttpSessionContext getSessionContext() {
    touch();
    final SessionManager component = SystemInstance.get().getComponent(SessionManager.class);
    return new HttpSessionContext() {

        @Override
        public javax.servlet.http.HttpSession getSession(final String sessionId) {
            final HttpSessionEvent event = component.findSession(sessionId);
            return event == null ? null : event.getSession();
        }

        @Override
        public Enumeration<String> getIds() {
            return Collections.enumeration(component.findSessionIds());
        }
    };
}
Also used : SessionManager(org.apache.openejb.server.httpd.session.SessionManager) HttpSessionContext(javax.servlet.http.HttpSessionContext) HttpSessionEvent(javax.servlet.http.HttpSessionEvent)

Example 17 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project tomee by apache.

the class HttpSessionImplTest method run.

@Test
public void run() throws URISyntaxException {
    final HttpRequest req = new HttpRequestImpl(new URI("http://localhost:1234/foo"));
    final javax.servlet.http.HttpSession session = req.getSession();
    Reflections.set(session, "listeners", Collections.<Object>singletonList(new HttpSessionListener() {

        private int count = 0;

        @Override
        public void sessionCreated(final HttpSessionEvent se) {
        // no-op
        }

        @Override
        public void sessionDestroyed(final HttpSessionEvent se) {
            se.getSession().setAttribute("seen", ++count);
        }
    }));
    session.invalidate();
    final long c1 = Integer.class.cast(session.getAttribute("seen"));
    session.invalidate();
    final long c2 = Integer.class.cast(session.getAttribute("seen"));
    assertEquals(c1, c2);
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) URI(java.net.URI) Test(org.junit.Test)

Example 18 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project jetty.project by eclipse.

the class Session method willPassivate.

/* ------------------------------------------------------------- */
/**
     * Call the passivation listeners. This must be called holding the
     * lock
     */
public void willPassivate() {
    HttpSessionEvent event = new HttpSessionEvent(this);
    for (Iterator<String> iter = _sessionData.getKeys().iterator(); iter.hasNext(); ) {
        Object value = _sessionData.getAttribute(iter.next());
        if (value instanceof HttpSessionActivationListener) {
            HttpSessionActivationListener listener = (HttpSessionActivationListener) value;
            listener.sessionWillPassivate(event);
        }
    }
}
Also used : HttpSessionEvent(javax.servlet.http.HttpSessionEvent) HttpSessionActivationListener(javax.servlet.http.HttpSessionActivationListener)

Example 19 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project jetty.project by eclipse.

the class SessionHandler method newHttpSession.

/* ------------------------------------------------------------ */
/**
     * Creates a new <code>HttpSession</code>.
     *
     * @param request the HttpServletRequest containing the requested session id
     * @return the new <code>HttpSession</code>
     */
public HttpSession newHttpSession(HttpServletRequest request) {
    long created = System.currentTimeMillis();
    String id = _sessionIdManager.newSessionId(request, created);
    Session session = _sessionCache.newSession(request, id, created, (_dftMaxIdleSecs > 0 ? _dftMaxIdleSecs * 1000L : -1));
    session.setExtendedId(_sessionIdManager.getExtendedId(id, request));
    session.getSessionData().setLastNode(_sessionIdManager.getWorkerName());
    try {
        _sessionCache.put(id, session);
        _sessionsCreatedStats.increment();
        if (request.isSecure())
            session.setAttribute(Session.SESSION_CREATED_SECURE, Boolean.TRUE);
        if (_sessionListeners != null) {
            HttpSessionEvent event = new HttpSessionEvent(session);
            for (HttpSessionListener listener : _sessionListeners) listener.sessionCreated(event);
        }
        return session;
    } catch (Exception e) {
        LOG.warn(e);
        return null;
    }
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpSession(javax.servlet.http.HttpSession)

Example 20 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project tomcat by apache.

the class StandardSessionContext method activate.

/**
     * Perform internal processing required to activate this
     * session.
     */
public void activate() {
    // Initialize access count
    if (ACTIVITY_CHECK) {
        accessCount = new AtomicInteger();
    }
    // Notify interested session event listeners
    fireSessionEvent(Session.SESSION_ACTIVATED_EVENT, null);
    // Notify ActivationListeners
    HttpSessionEvent event = null;
    String[] keys = keys();
    for (int i = 0; i < keys.length; i++) {
        Object attribute = attributes.get(keys[i]);
        if (attribute instanceof HttpSessionActivationListener) {
            if (event == null)
                event = new HttpSessionEvent(getSession());
            try {
                ((HttpSessionActivationListener) attribute).sessionDidActivate(event);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                manager.getContext().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) HttpSessionActivationListener(javax.servlet.http.HttpSessionActivationListener)

Aggregations

HttpSessionEvent (javax.servlet.http.HttpSessionEvent)25 HttpSessionActivationListener (javax.servlet.http.HttpSessionActivationListener)8 HttpSessionListener (javax.servlet.http.HttpSessionListener)7 Test (org.junit.Test)6 IOException (java.io.IOException)4 HttpSession (javax.servlet.http.HttpSession)4 MockHttpSession (org.springframework.mock.web.MockHttpSession)4 MockServletContext (org.springframework.mock.web.MockServletContext)4 HttpSessionEventPublisher (org.springframework.security.web.session.HttpSessionEventPublisher)4 ServletContext (javax.servlet.ServletContext)3 SessionManager (org.apache.openejb.server.httpd.session.SessionManager)3 PersistentSession (io.undertow.servlet.api.SessionPersistenceManager.PersistentSession)2 NotSerializableException (java.io.NotSerializableException)2 WriteAbortedException (java.io.WriteAbortedException)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServletException (javax.servlet.ServletException)2 Context (org.apache.catalina.Context)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 ImmutableHttpSessionAdapter (org.wildfly.clustering.web.session.ImmutableHttpSessionAdapter)2