Search in sources :

Example 6 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project spring-security by spring-projects.

the class HttpSessionEventPublisherTests method sessionCreatedNullApplicationContext.

// SEC-2599
@Test(expected = IllegalStateException.class)
public void sessionCreatedNullApplicationContext() {
    HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
    MockServletContext servletContext = new MockServletContext();
    MockHttpSession session = new MockHttpSession(servletContext);
    HttpSessionEvent event = new HttpSessionEvent(session);
    publisher.sessionCreated(event);
}
Also used : HttpSessionEventPublisher(org.springframework.security.web.session.HttpSessionEventPublisher) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 7 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project wildfly by wildfly.

the class UndertowContextTestCase method addSessionListener.

@Test
public void addSessionListener() throws ServletException {
    HttpSessionListener listener = mock(HttpSessionListener.class);
    ServletContext context = mock(ServletContext.class);
    HttpSession session = mock(HttpSession.class);
    ApplicationListeners listeners = new ApplicationListeners(Collections.<ManagedListener>emptyList(), context);
    ArgumentCaptor<HttpSessionEvent> event = ArgumentCaptor.forClass(HttpSessionEvent.class);
    when(this.deployment.getApplicationListeners()).thenReturn(listeners);
    this.context.addSessionListener(listener);
    listeners.start();
    listeners.sessionCreated(session);
    verify(listener).sessionCreated(event.capture());
    assertSame(session, event.getValue().getSession());
    event = ArgumentCaptor.forClass(HttpSessionEvent.class);
    listeners.sessionDestroyed(session);
    verify(listener).sessionDestroyed(event.capture());
    assertSame(session, event.getValue().getSession());
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSession(javax.servlet.http.HttpSession) ApplicationListeners(io.undertow.servlet.core.ApplicationListeners) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 8 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project undertow by undertow-io.

the class ApplicationListeners method sessionDestroyed.

public void sessionDestroyed(final HttpSession session) {
    if (!started) {
        return;
    }
    final HttpSessionEvent sre = new HttpSessionEvent(session);
    for (int i = httpSessionListeners.length - 1; i >= 0; --i) {
        ManagedListener listener = httpSessionListeners[i];
        this.<HttpSessionListener>get(listener).sessionDestroyed(sre);
    }
}
Also used : HttpSessionEvent(javax.servlet.http.HttpSessionEvent)

Example 9 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project undertow by undertow-io.

the class SessionRestoringHandler method stop.

public void stop() {
    ClassLoader old = getTccl();
    try {
        setTccl(servletContext.getClassLoader());
        this.started = false;
        final Map<String, SessionPersistenceManager.PersistentSession> objectData = new HashMap<>();
        for (String sessionId : sessionManager.getTransientSessions()) {
            Session session = sessionManager.getSession(sessionId);
            if (session != null) {
                final HttpSessionEvent event = new HttpSessionEvent(SecurityActions.forSession(session, servletContext, false));
                final Map<String, Object> sessionData = new HashMap<>();
                for (String attr : session.getAttributeNames()) {
                    final Object attribute = session.getAttribute(attr);
                    sessionData.put(attr, attribute);
                    if (attribute instanceof HttpSessionActivationListener) {
                        ((HttpSessionActivationListener) attribute).sessionWillPassivate(event);
                    }
                }
                objectData.put(sessionId, new PersistentSession(new Date(session.getLastAccessedTime() + (session.getMaxInactiveInterval() * 1000)), sessionData));
            }
        }
        sessionPersistenceManager.persistSessions(deploymentName, objectData);
        this.data.clear();
    } finally {
        setTccl(old);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession) HttpSessionActivationListener(javax.servlet.http.HttpSessionActivationListener) Date(java.util.Date) Session(io.undertow.server.session.Session) PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession)

Example 10 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project undertow by undertow-io.

the class SessionRestoringHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    final String incomingSessionId = servletContext.getSessionConfig().findSessionId(exchange);
    if (incomingSessionId == null || !data.containsKey(incomingSessionId)) {
        next.handleRequest(exchange);
        return;
    }
    //we have some old data
    PersistentSession result = data.remove(incomingSessionId);
    if (result != null) {
        long time = System.currentTimeMillis();
        if (time < result.getExpiration().getTime()) {
            final HttpSessionImpl session = servletContext.getSession(exchange, true);
            final HttpSessionEvent event = new HttpSessionEvent(session);
            for (Map.Entry<String, Object> entry : result.getSessionData().entrySet()) {
                if (entry.getValue() instanceof HttpSessionActivationListener) {
                    ((HttpSessionActivationListener) entry.getValue()).sessionDidActivate(event);
                }
                if (entry.getKey().startsWith(HttpSessionImpl.IO_UNDERTOW)) {
                    session.getSession().setAttribute(entry.getKey(), entry.getValue());
                } else {
                    session.setAttribute(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    next.handleRequest(exchange);
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) 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