Search in sources :

Example 11 with Session

use of org.apache.catalina.Session in project tomcat by apache.

the class ManagerBase method expireSession.

public void expireSession(String sessionId) {
    Session s = sessions.get(sessionId);
    if (s == null) {
        if (log.isInfoEnabled())
            log.info("Session not found " + sessionId);
        return;
    }
    s.expire();
}
Also used : Session(org.apache.catalina.Session)

Example 12 with Session

use of org.apache.catalina.Session in project tomcat by apache.

the class PersistentManagerBase method swapIn.

// ------------------------------------------------------ Protected Methods
/**
     * Look for a session in the Store and, if found, restore
     * it in the Manager's list of active sessions if appropriate.
     * The session will be removed from the Store after swapping
     * in, but will not be added to the active session list if it
     * is invalid or past its expiration.
     *
     * @param id The id of the session that should be swapped in
     * @return restored session, or {@code null}, if none is found
     * @throws IOException an IO error occurred
     */
protected Session swapIn(String id) throws IOException {
    if (store == null)
        return null;
    Object swapInLock = null;
    /*
         * The purpose of this sync and these locks is to make sure that a
         * session is only loaded once. It doesn't matter if the lock is removed
         * and then another thread enters this method and tries to load the same
         * session. That thread will re-create a swapIn lock for that session,
         * quickly find that the session is already in sessions, use it and
         * carry on.
         */
    synchronized (this) {
        swapInLock = sessionSwapInLocks.get(id);
        if (swapInLock == null) {
            swapInLock = new Object();
            sessionSwapInLocks.put(id, swapInLock);
        }
    }
    Session session = null;
    synchronized (swapInLock) {
        // First check to see if another thread has loaded the session into
        // the manager
        session = sessions.get(id);
        if (session == null) {
            try {
                if (SecurityUtil.isPackageProtectionEnabled()) {
                    try {
                        session = AccessController.doPrivileged(new PrivilegedStoreLoad(id));
                    } catch (PrivilegedActionException ex) {
                        Exception e = ex.getException();
                        log.error(sm.getString("persistentManager.swapInException", id), e);
                        if (e instanceof IOException) {
                            throw (IOException) e;
                        } else if (e instanceof ClassNotFoundException) {
                            throw (ClassNotFoundException) e;
                        }
                    }
                } else {
                    session = store.load(id);
                }
            } catch (ClassNotFoundException e) {
                String msg = sm.getString("persistentManager.deserializeError", id);
                log.error(msg, e);
                throw new IllegalStateException(msg, e);
            }
            if (session != null && !session.isValid()) {
                log.error(sm.getString("persistentManager.swapInInvalid", id));
                session.expire();
                removeSession(id);
                session = null;
            }
            if (session != null) {
                if (log.isDebugEnabled())
                    log.debug(sm.getString("persistentManager.swapIn", id));
                session.setManager(this);
                // make sure the listeners know about it.
                ((StandardSession) session).tellNew();
                add(session);
                ((StandardSession) session).activate();
                // endAccess() to ensure timeouts happen correctly.
                // access() to keep access count correct or it will end up
                // negative
                session.access();
                session.endAccess();
            }
        }
    }
    // Make sure the lock is removed
    synchronized (this) {
        sessionSwapInLocks.remove(id);
    }
    return session;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) PrivilegedActionException(java.security.PrivilegedActionException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) Session(org.apache.catalina.Session)

Example 13 with Session

use of org.apache.catalina.Session in project tomcat by apache.

the class StandardManager method doUnload.

/**
     * Save any currently active sessions in the appropriate persistence
     * mechanism, if any.  If persistence is not supported, this method
     * returns without doing anything.
     *
     * @exception IOException if an input/output error occurs
     */
protected void doUnload() throws IOException {
    if (log.isDebugEnabled())
        log.debug(sm.getString("standardManager.unloading.debug"));
    if (sessions.isEmpty()) {
        log.debug(sm.getString("standardManager.unloading.nosessions"));
        // nothing to do
        return;
    }
    // Open an output stream to the specified pathname, if any
    File file = file();
    if (file == null) {
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("standardManager.unloading", pathname));
    }
    // Keep a note of sessions that are expired
    ArrayList<StandardSession> list = new ArrayList<>();
    try (FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
        synchronized (sessions) {
            if (log.isDebugEnabled()) {
                log.debug("Unloading " + sessions.size() + " sessions");
            }
            // Write the number of active sessions, followed by the details
            oos.writeObject(Integer.valueOf(sessions.size()));
            Iterator<Session> elements = sessions.values().iterator();
            while (elements.hasNext()) {
                StandardSession session = (StandardSession) elements.next();
                list.add(session);
                session.passivate();
                session.writeObjectData(oos);
            }
        }
    }
    // Expire all the sessions we just wrote
    if (log.isDebugEnabled()) {
        log.debug("Expiring " + list.size() + " persisted sessions");
    }
    Iterator<StandardSession> expires = list.iterator();
    while (expires.hasNext()) {
        StandardSession session = expires.next();
        try {
            session.expire(false);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
        } finally {
            session.recycle();
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Unloading complete");
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Session(org.apache.catalina.Session)

Example 14 with Session

use of org.apache.catalina.Session in project tomcat by apache.

the class PersistentValve method invoke.

/**
     * Select the appropriate child Context to process this request,
     * based on the specified request URI.  If no matching Context can
     * be found, return an appropriate HTTP error.
     *
     * @param request Request to be processed
     * @param response Response to be produced
     *
     * @exception IOException if an input/output error occurred
     * @exception ServletException if a servlet error occurred
     */
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
    // Select the Context to be used for this Request
    Context context = request.getContext();
    if (context == null) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, sm.getString("standardHost.noContext"));
        return;
    }
    // Update the session last access time for our session (if any)
    String sessionId = request.getRequestedSessionId();
    Manager manager = context.getManager();
    if (sessionId != null && manager instanceof StoreManager) {
        Store store = ((StoreManager) manager).getStore();
        if (store != null) {
            Session session = null;
            try {
                session = store.load(sessionId);
            } catch (Exception e) {
                container.getLogger().error("deserializeError");
            }
            if (session != null) {
                if (!session.isValid() || isSessionStale(session, System.currentTimeMillis())) {
                    if (container.getLogger().isDebugEnabled()) {
                        container.getLogger().debug("session swapped in is invalid or expired");
                    }
                    session.expire();
                    store.remove(sessionId);
                } else {
                    session.setManager(manager);
                    // session.setId(sessionId); Only if new ???
                    manager.add(session);
                    // ((StandardSession)session).activate();
                    session.access();
                    session.endAccess();
                }
            }
        }
    }
    if (container.getLogger().isDebugEnabled()) {
        container.getLogger().debug("sessionId: " + sessionId);
    }
    // Ask the next valve to process the request.
    getNext().invoke(request, response);
    // If still processing async, don't try to store the session
    if (!request.isAsync()) {
        // Read the sessionid after the response.
        // HttpSession hsess = hreq.getSession(false);
        Session hsess;
        try {
            hsess = request.getSessionInternal(false);
        } catch (Exception ex) {
            hsess = null;
        }
        String newsessionId = null;
        if (hsess != null) {
            newsessionId = hsess.getIdInternal();
        }
        if (container.getLogger().isDebugEnabled()) {
            container.getLogger().debug("newsessionId: " + newsessionId);
        }
        if (newsessionId != null) {
            try {
                bind(context);
                /* store the session and remove it from the manager */
                if (manager instanceof StoreManager) {
                    Session session = manager.findSession(newsessionId);
                    Store store = ((StoreManager) manager).getStore();
                    if (store != null && session != null && session.isValid() && !isSessionStale(session, System.currentTimeMillis())) {
                        store.save(session);
                        ((StoreManager) manager).removeSuper(session);
                        session.recycle();
                    } else {
                        if (container.getLogger().isDebugEnabled()) {
                            container.getLogger().debug("newsessionId store: " + store + " session: " + session + " valid: " + (session == null ? "N/A" : Boolean.toString(session.isValid())) + " stale: " + isSessionStale(session, System.currentTimeMillis()));
                        }
                    }
                } else {
                    if (container.getLogger().isDebugEnabled()) {
                        container.getLogger().debug("newsessionId Manager: " + manager);
                    }
                }
            } finally {
                unbind(context);
            }
        }
    }
}
Also used : Context(org.apache.catalina.Context) Store(org.apache.catalina.Store) StoreManager(org.apache.catalina.StoreManager) Manager(org.apache.catalina.Manager) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) StoreManager(org.apache.catalina.StoreManager) Session(org.apache.catalina.Session)

Example 15 with Session

use of org.apache.catalina.Session in project redisson by redisson.

the class RedissonSessionManager method findSession.

@Override
public Session findSession(String id) throws IOException {
    Session result = super.findSession(id);
    if (result == null && id != null) {
        RedissonSession session = (RedissonSession) createEmptySession();
        session.setId(id);
        session.load();
        return session;
    }
    return result;
}
Also used : Session(org.apache.catalina.Session)

Aggregations

Session (org.apache.catalina.Session)58 HttpSession (javax.servlet.http.HttpSession)17 Manager (org.apache.catalina.Manager)16 IOException (java.io.IOException)13 StandardSession (org.apache.catalina.session.StandardSession)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 Context (org.apache.catalina.Context)7 StringManager (org.apache.tomcat.util.res.StringManager)7 StandardContext (org.apache.catalina.core.StandardContext)5 Principal (java.security.Principal)3 Container (org.apache.catalina.Container)3 LifecycleException (org.apache.catalina.LifecycleException)3 Realm (org.apache.catalina.Realm)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ServletContext (javax.servlet.ServletContext)2 ServletRequest (javax.servlet.ServletRequest)2 Cookie (javax.servlet.http.Cookie)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Loader (org.apache.catalina.Loader)2