Search in sources :

Example 1 with PersistentSession

use of io.undertow.servlet.api.SessionPersistenceManager.PersistentSession in project spring-boot by spring-projects.

the class FileSessionPersistenceTests method deleteFileOnClear.

@Test
public void deleteFileOnClear() throws Exception {
    File sessionFile = new File(this.dir, "test.session");
    Map<String, PersistentSession> sessionData = new LinkedHashMap<>();
    this.persistence.persistSessions("test", sessionData);
    assertThat(sessionFile.exists()).isTrue();
    this.persistence.clear("test");
    assertThat(sessionFile.exists()).isFalse();
}
Also used : PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with PersistentSession

use of io.undertow.servlet.api.SessionPersistenceManager.PersistentSession in project spring-boot by spring-projects.

the class FileSessionPersistenceTests method dontRestoreExpired.

@Test
public void dontRestoreExpired() throws Exception {
    Date expired = new Date(System.currentTimeMillis() - 1000);
    Map<String, PersistentSession> sessionData = new LinkedHashMap<>();
    Map<String, Object> data = new LinkedHashMap<>();
    data.put("spring", "boot");
    PersistentSession session = new PersistentSession(expired, data);
    sessionData.put("abc", session);
    this.persistence.persistSessions("test", sessionData);
    Map<String, PersistentSession> restored = this.persistence.loadSessionAttributes("test", this.classLoader);
    assertThat(restored).isNotNull();
    assertThat(restored.containsKey("abc")).isFalse();
}
Also used : PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with PersistentSession

use of io.undertow.servlet.api.SessionPersistenceManager.PersistentSession 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 4 with PersistentSession

use of io.undertow.servlet.api.SessionPersistenceManager.PersistentSession 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)

Example 5 with PersistentSession

use of io.undertow.servlet.api.SessionPersistenceManager.PersistentSession in project spring-boot by spring-projects.

the class FileSessionPersistenceTests method persistAndLoad.

@Test
public void persistAndLoad() throws Exception {
    Map<String, PersistentSession> sessionData = new LinkedHashMap<>();
    Map<String, Object> data = new LinkedHashMap<>();
    data.put("spring", "boot");
    PersistentSession session = new PersistentSession(this.expiration, data);
    sessionData.put("abc", session);
    this.persistence.persistSessions("test", sessionData);
    Map<String, PersistentSession> restored = this.persistence.loadSessionAttributes("test", this.classLoader);
    assertThat(restored).isNotNull();
    assertThat(restored.get("abc").getExpiration()).isEqualTo(this.expiration);
    assertThat(restored.get("abc").getSessionData().get("spring")).isEqualTo("boot");
}
Also used : PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

PersistentSession (io.undertow.servlet.api.SessionPersistenceManager.PersistentSession)5 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 HttpSessionActivationListener (javax.servlet.http.HttpSessionActivationListener)2 HttpSessionEvent (javax.servlet.http.HttpSessionEvent)2 Session (io.undertow.server.session.Session)1 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)1 File (java.io.File)1 Map (java.util.Map)1