Search in sources :

Example 96 with Session

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

the class TestSSOnonLoginAndBasicAuthenticator method doImminentSessionTimeout.

/*
     * Force faster timeout for an active Container than can
     * be defined in web.xml. By getting to the active Session we
     * can choose seconds instead of minutes.
     * Note: shamelessly cloned from ManagerBase - beware of synch issues
     *       on the underlying sessions.
     */
private void doImminentSessionTimeout(Context activeContext) {
    ManagerBase manager = (ManagerBase) activeContext.getManager();
    Session[] sessions = manager.findSessions();
    for (Session session : sessions) {
        if (session != null && session.isValid()) {
            session.setMaxInactiveInterval(EXTRA_DELAY_SECS);
        // leave it to be expired by the manager
        }
    }
    try {
        Thread.sleep(EXTRA_DELAY_SECS * 1000);
    } catch (InterruptedException ie) {
    // ignored
    }
    // Paranoid verification that active sessions have now gone
    int count = 0;
    sessions = manager.findSessions();
    while (sessions.length != 0 && count < TIMEOUT_WAIT_SECS) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        // Ignore
        }
        sessions = manager.findSessions();
        count++;
    }
    sessions = manager.findSessions();
    Assert.assertTrue(sessions.length == 0);
}
Also used : ManagerBase(org.apache.catalina.session.ManagerBase) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) Session(org.apache.catalina.Session)

Example 97 with Session

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

the class HTMLManagerServlet method getSessionsForName.

protected List<Session> getSessionsForName(ContextName cn, StringManager smClient) {
    if ((cn == null) || !(cn.getPath().startsWith("/") || cn.getPath().equals(""))) {
        String path = null;
        if (cn != null) {
            path = cn.getPath();
        }
        throw new IllegalArgumentException(smClient.getString("managerServlet.invalidPath", Escape.htmlElementContent(path)));
    }
    Context ctxt = (Context) host.findChild(cn.getName());
    if (null == ctxt) {
        throw new IllegalArgumentException(smClient.getString("managerServlet.noContext", Escape.htmlElementContent(cn.getDisplayName())));
    }
    Manager manager = ctxt.getManager();
    List<Session> sessions = new ArrayList<>(Arrays.asList(manager.findSessions()));
    if (manager instanceof DistributedManager && showProxySessions) {
        // Add dummy proxy sessions
        Set<String> sessionIds = ((DistributedManager) manager).getSessionIdsFull();
        // Remove active (primary and backup) session IDs from full list
        for (Session session : sessions) {
            sessionIds.remove(session.getId());
        }
        // Left with just proxy sessions - add them
        for (String sessionId : sessionIds) {
            sessions.add(new DummyProxySession(sessionId));
        }
    }
    return sessions;
}
Also used : Context(org.apache.catalina.Context) ArrayList(java.util.ArrayList) DistributedManager(org.apache.catalina.DistributedManager) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) HttpSession(jakarta.servlet.http.HttpSession) Session(org.apache.catalina.Session) DistributedManager(org.apache.catalina.DistributedManager)

Example 98 with Session

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

the class HTMLManagerServlet method displaySessionDetailPage.

/**
 * Display session details.
 *
 * @param req The Servlet request
 * @param resp The Servlet response
 * @param cn Name of the application for which the sessions will be listed
 * @param sessionId the session id
 * @param smClient StringManager for the client's locale
 * @throws ServletException Propagated Servlet error
 * @throws IOException An IO error occurred
 */
protected void displaySessionDetailPage(HttpServletRequest req, HttpServletResponse resp, ContextName cn, String sessionId, StringManager smClient) throws ServletException, IOException {
    Session session = getSessionForNameAndId(cn, sessionId, smClient);
    // strong>NOTE</strong> - This header will be overridden
    // automatically if a <code>RequestDispatcher.forward()</code> call is
    // ultimately invoked.
    // HTTP 1.0
    resp.setHeader("Pragma", "No-cache");
    // HTTP 1.1
    resp.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
    // 0 means now
    resp.setDateHeader("Expires", 0);
    req.setAttribute("currentSession", session);
    getServletContext().getRequestDispatcher(resp.encodeURL(sessionDetailJspPath)).include(req, resp);
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) Session(org.apache.catalina.Session)

Example 99 with Session

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

the class DeltaManager method expireAllLocalSessions.

/**
 * Expire all find sessions.
 */
public void expireAllLocalSessions() {
    long timeNow = System.currentTimeMillis();
    Session[] sessions = findSessions();
    int expireDirect = 0;
    int expireIndirect = 0;
    if (log.isDebugEnabled()) {
        log.debug("Start expire all sessions " + getName() + " at " + timeNow + " sessioncount " + sessions.length);
    }
    for (Session value : sessions) {
        if (value instanceof DeltaSession) {
            DeltaSession session = (DeltaSession) value;
            if (session.isPrimarySession()) {
                if (session.isValid()) {
                    session.expire();
                    expireDirect++;
                } else {
                    expireIndirect++;
                }
            // end if
            }
        // end if
        }
    // end if
    }
    // for
    long timeEnd = System.currentTimeMillis();
    if (log.isDebugEnabled()) {
        log.debug("End expire sessions " + getName() + " expire processingTime " + (timeEnd - timeNow) + " expired direct sessions: " + expireDirect + " expired direct sessions: " + expireIndirect);
    }
}
Also used : Session(org.apache.catalina.Session)

Example 100 with Session

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

the class DeltaManager method stopInternal.

/**
 * Stop this component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void stopInternal() throws LifecycleException {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("deltaManager.stopped", getName()));
    }
    setState(LifecycleState.STOPPING);
    // Expire all active sessions
    if (log.isInfoEnabled()) {
        log.info(sm.getString("deltaManager.expireSessions", getName()));
    }
    Session[] sessions = findSessions();
    for (Session value : sessions) {
        DeltaSession session = (DeltaSession) value;
        if (!session.isValid()) {
            continue;
        }
        try {
            session.expire(true, isExpireSessionsOnShutdown());
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
        }
    }
    // Require a new random number generator if we are restarted
    super.stopInternal();
}
Also used : Session(org.apache.catalina.Session)

Aggregations

Session (org.apache.catalina.Session)106 IOException (java.io.IOException)24 Manager (org.apache.catalina.Manager)22 Context (org.apache.catalina.Context)16 HttpSession (javax.servlet.http.HttpSession)13 StringManager (org.apache.tomcat.util.res.StringManager)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 HttpSession (jakarta.servlet.http.HttpSession)7 GenericPrincipal (org.apache.catalina.realm.GenericPrincipal)7 Principal (java.security.Principal)6 Realm (org.apache.catalina.Realm)6 StandardContext (org.apache.catalina.core.StandardContext)6 ClusterSession (org.apache.catalina.ha.ClusterSession)6 DeltaSession (org.apache.catalina.ha.session.DeltaSession)6 Container (org.apache.catalina.Container)5 ArrayList (java.util.ArrayList)4 StandardSession (org.apache.catalina.session.StandardSession)4 BufferedOutputStream (java.io.BufferedOutputStream)3 File (java.io.File)3 ObjectOutputStream (java.io.ObjectOutputStream)3