Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.undertow.servlet.handlers;
  
 
 import java.util.Date;
 import java.util.Map;
 
 import static io.undertow.servlet.api.SessionPersistenceManager.PersistentSession;

A handler that restores persistent HTTP session state for requests in development mode.

This handler should not be used in production environments.

Author(s):
Stuart Douglas
 
 public class SessionRestoringHandler implements HttpHandlerLifecycle {
 
     private final String deploymentName;
     private final SessionManager sessionManager;
     private final ServletContextImpl servletContext;
     private final HttpHandler next;
     private volatile boolean started = false;
 
     public SessionRestoringHandler(String deploymentNameSessionManager sessionManagerServletContextImpl servletContextHttpHandler nextSessionPersistenceManager sessionPersistenceManager) {
         this. = deploymentName;
         this. = sessionManager;
         this. = servletContext;
         this. = next;
         this. = sessionPersistenceManager;
     }
 
     public void start() {
         ClassLoader old = getTccl();
         try {
             setTccl(.getClassLoader());
 
             try {
                 if (sessionData != null) {
                     this..putAll(sessionData);
                 }
             } catch (Exception e) {
             }
             this. = true;
         } finally {
             setTccl(old);
         }
     }
 
     public void stop() {
         ClassLoader old = getTccl();
         try {
             setTccl(.getClassLoader());
             this. = false;
             for (String sessionId : .getTransientSessions()) {
                 Session session = .getSession(sessionId);
                 if (session != null) {
                     final HttpSessionEvent event = new HttpSessionEvent(SecurityActions.forSession(sessionfalse));
                     final Map<StringObjectsessionData = new HashMap<StringObject>();
                     for (String attr : session.getAttributeNames()) {
                         final Object attribute = session.getAttribute(attr);
                         sessionData.put(attrattribute);
                         if (attribute instanceof HttpSessionActivationListener) {
                             ((HttpSessionActivationListenerattribute).sessionWillPassivate(event);
                         }
                     }
                     objectData.put(sessionIdnew PersistentSession(new Date(session.getLastAccessedTime() + (session.getMaxInactiveInterval() * 1000)), sessionData));
                 }
             }
             .persistSessions(objectData);
             this..clear();
         } finally {
             setTccl(old);
         }
     }
 
     @Override
     public void handleRequest(HttpServerExchange exchangethrows Exception {
         final String incomingSessionId = .getSessionConfig().findSessionId(exchange);
        if (incomingSessionId == null || !.containsKey(incomingSessionId)) {
            .handleRequest(exchange);
            return;
        }
        //we have some old data
        PersistentSession result = .remove(incomingSessionId);
        if (result != null) {
            long time = System.currentTimeMillis();
            if (time < result.getExpiration().getTime()) {
                final HttpSessionImpl session = .getSession(exchangetrue);
                final HttpSessionEvent event = new HttpSessionEvent(session);
                for (Map.Entry<StringObjectentry : result.getSessionData().entrySet()) {
                    if (entry.getValue() instanceof HttpSessionActivationListener) {
                        ((HttpSessionActivationListenerentry.getValue()).sessionWillPassivate(event);
                    }
                    session.setAttribute(entry.getKey(), entry.getValue());
                }
            }
        }
        .handleRequest(exchange);
    }
    @Override
    public boolean isStarted() {
        return ;
    }
    private ClassLoader getTccl() {
        if (System.getSecurityManager() == null) {
            return Thread.currentThread().getContextClassLoader();
        } else {
            return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                @Override
                public ClassLoader run() {
                    return Thread.currentThread().getContextClassLoader();
                }
            });
        }
    }
    private void setTccl(final ClassLoader classLoader) {
        if (System.getSecurityManager() == null) {
            Thread.currentThread().setContextClassLoader(classLoader);
        } else {
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
                @Override
                public Void run() {
                    Thread.currentThread().setContextClassLoader(classLoader);
                    return null;
                }
            });
        }
    }
New to GrepCode? Check out our FAQ X