Search in sources :

Example 1 with HttpSessionEvent

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

the class HttpSessionEventPublisherTests method sessionIdChangeNullApplicationContext.

@Test
public void sessionIdChangeNullApplicationContext() {
    HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
    MockServletContext servletContext = new MockServletContext();
    MockHttpSession session = new MockHttpSession(servletContext);
    HttpSessionEvent event = new HttpSessionEvent(session);
    assertThatIllegalStateException().isThrownBy(() -> publisher.sessionIdChanged(event, "oldSessionId"));
}
Also used : HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 2 with HttpSessionEvent

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

the class HttpSessionEventPublisherTests method sessionCreatedNullApplicationContext.

// SEC-2599
@Test
public void sessionCreatedNullApplicationContext() {
    HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
    MockServletContext servletContext = new MockServletContext();
    MockHttpSession session = new MockHttpSession(servletContext);
    HttpSessionEvent event = new HttpSessionEvent(session);
    assertThatIllegalStateException().isThrownBy(() -> publisher.sessionCreated(event));
}
Also used : HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with HttpSessionEvent

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

the class StandardSession method activate.

/**
 * Perform internal processing required to activate this
 * session.
 */
public void activate() {
    // Initialize access count
    if (activityCheck) {
        accessCount = new AtomicInteger();
    }
    // Notify interested session event listeners
    fireSessionEvent(Session.SESSION_ACTIVATED_EVENT, null);
    // Notify ActivationListeners
    HttpSessionEvent event = null;
    String[] keys = keys();
    for (String key : keys) {
        Object attribute = attributes.get(key);
        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(jakarta.servlet.http.HttpSessionEvent) HttpSessionActivationListener(jakarta.servlet.http.HttpSessionActivationListener)

Example 4 with HttpSessionEvent

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

the class StandardSession method passivate.

/**
 * Perform the internal processing required to passivate
 * this session.
 */
public void passivate() {
    // Notify interested session event listeners
    fireSessionEvent(Session.SESSION_PASSIVATED_EVENT, null);
    // Notify ActivationListeners
    HttpSessionEvent event = null;
    String[] keys = keys();
    for (String key : keys) {
        Object attribute = attributes.get(key);
        if (attribute instanceof HttpSessionActivationListener) {
            if (event == null) {
                event = new HttpSessionEvent(getSession());
            }
            try {
                ((HttpSessionActivationListener) attribute).sessionWillPassivate(event);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                manager.getContext().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
            }
        }
    }
}
Also used : HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) HttpSessionActivationListener(jakarta.servlet.http.HttpSessionActivationListener)

Example 5 with HttpSessionEvent

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

the class StandardSession 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 (Object o : listeners) {
            if (!(o instanceof HttpSessionListener)) {
                continue;
            }
            HttpSessionListener listener = (HttpSessionListener) o;
            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(jakarta.servlet.ServletContext) HttpSessionListener(jakarta.servlet.http.HttpSessionListener) HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) ObjectStreamException(java.io.ObjectStreamException) WriteAbortedException(java.io.WriteAbortedException)

Aggregations

HttpSessionEvent (jakarta.servlet.http.HttpSessionEvent)9 Test (org.junit.jupiter.api.Test)5 MockHttpSession (org.springframework.mock.web.MockHttpSession)5 MockServletContext (org.springframework.mock.web.MockServletContext)5 ServletContext (jakarta.servlet.ServletContext)2 HttpSessionActivationListener (jakarta.servlet.http.HttpSessionActivationListener)2 HttpSessionListener (jakarta.servlet.http.HttpSessionListener)2 IOException (java.io.IOException)2 NotSerializableException (java.io.NotSerializableException)2 ObjectStreamException (java.io.ObjectStreamException)2 WriteAbortedException (java.io.WriteAbortedException)2 Context (org.apache.catalina.Context)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 TomcatPrincipal (org.apache.catalina.TomcatPrincipal)1