Search in sources :

Example 26 with Session

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

the class ApplicationHttpRequest method getSession.

/**
     * Return the session associated with this Request, creating one
     * if necessary and requested.
     *
     * @param create Create a new session if one does not exist
     */
@Override
public HttpSession getSession(boolean create) {
    if (crossContext) {
        // There cannot be a session if no context has been assigned yet
        if (context == null)
            return (null);
        // Return the current session if it exists and is valid
        if (session != null && session.isValid()) {
            return (session.getSession());
        }
        HttpSession other = super.getSession(false);
        if (create && (other == null)) {
            // First create a session in the first context: the problem is
            // that the top level request is the only one which can
            // create the cookie safely
            other = super.getSession(true);
        }
        if (other != null) {
            Session localSession = null;
            try {
                localSession = context.getManager().findSession(other.getId());
                if (localSession != null && !localSession.isValid()) {
                    localSession = null;
                }
            } catch (IOException e) {
            // Ignore
            }
            if (localSession == null && create) {
                localSession = context.getManager().createSession(other.getId());
            }
            if (localSession != null) {
                localSession.access();
                session = localSession;
                return session.getSession();
            }
        }
        return null;
    } else {
        return super.getSession(create);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) IOException(java.io.IOException) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.catalina.Session)

Example 27 with Session

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

the class JvmRouteBinderValve method handleJvmRoute.

/**
     * Handle jvmRoute stickiness after tomcat instance failed. After this
     * correction a new Cookie send to client with new jvmRoute and the
     * SessionID change propagate to the other cluster nodes.
     *
     * @param request current request
     * @param sessionId
     *            request SessionID from Cookie
     * @param localJvmRoute
     *            local jvmRoute
     */
protected void handleJvmRoute(Request request, String sessionId, String localJvmRoute) {
    // get requested jvmRoute.
    String requestJvmRoute = null;
    int index = sessionId.indexOf('.');
    if (index > 0) {
        requestJvmRoute = sessionId.substring(index + 1, sessionId.length());
    }
    if (requestJvmRoute != null && !requestJvmRoute.equals(localJvmRoute)) {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("jvmRoute.failover", requestJvmRoute, localJvmRoute, sessionId));
        }
        Session catalinaSession = null;
        try {
            catalinaSession = getManager(request).findSession(sessionId);
        } catch (IOException e) {
        // Hups!
        }
        String id = sessionId.substring(0, index);
        String newSessionID = id + "." + localJvmRoute;
        // OK - turnover the session and inform other cluster nodes
        if (catalinaSession != null) {
            changeSessionID(request, sessionId, newSessionID, catalinaSession);
            numberOfSessions++;
        } else {
            try {
                catalinaSession = getManager(request).findSession(newSessionID);
            } catch (IOException e) {
            // Hups!
            }
            if (catalinaSession != null) {
                // session is rewrite at other request, rewrite this also
                changeRequestSessionID(request, sessionId, newSessionID);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("jvmRoute.cannotFindSession", sessionId));
                }
            }
        }
    }
}
Also used : IOException(java.io.IOException) Session(org.apache.catalina.Session)

Example 28 with Session

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

the class ReplicationValve method sendCrossContextSession.

/**
     * Send all changed cross context sessions to backups
     */
protected void sendCrossContextSession() {
    List<DeltaSession> sessions = crossContextSessions.get();
    if (sessions != null && sessions.size() > 0) {
        for (Iterator<DeltaSession> iter = sessions.iterator(); iter.hasNext(); ) {
            Session session = iter.next();
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("ReplicationValve.crossContext.sendDelta", session.getManager().getContext().getName()));
            }
            sendMessage(session, (ClusterManager) session.getManager());
            if (doStatistics()) {
                nrOfCrossContextSendRequests++;
            }
        }
    }
}
Also used : DeltaSession(org.apache.catalina.ha.session.DeltaSession) ClusterSession(org.apache.catalina.ha.ClusterSession) Session(org.apache.catalina.Session) DeltaSession(org.apache.catalina.ha.session.DeltaSession)

Example 29 with Session

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

the class ReplicationValve method createPrimaryIndicator.

/**
     * Mark Request that processed at primary node with attribute
     * primaryIndicatorName
     *
     * @param request The Servlet request
     * @throws IOException IO error finding session
     */
protected void createPrimaryIndicator(Request request) throws IOException {
    String id = request.getRequestedSessionId();
    if ((id != null) && (id.length() > 0)) {
        Manager manager = request.getContext().getManager();
        Session session = manager.findSession(id);
        if (session instanceof ClusterSession) {
            ClusterSession cses = (ClusterSession) session;
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("ReplicationValve.session.indicator", request.getContext().getName(), id, primaryIndicatorName, Boolean.valueOf(cses.isPrimarySession())));
            }
            request.setAttribute(primaryIndicatorName, cses.isPrimarySession() ? Boolean.TRUE : Boolean.FALSE);
        } else {
            if (log.isDebugEnabled()) {
                if (session != null) {
                    log.debug(sm.getString("ReplicationValve.session.found", request.getContext().getName(), id));
                } else {
                    log.debug(sm.getString("ReplicationValve.session.invalid", request.getContext().getName(), id));
                }
            }
        }
    }
}
Also used : ClusterSession(org.apache.catalina.ha.ClusterSession) DeltaManager(org.apache.catalina.ha.session.DeltaManager) ClusterManager(org.apache.catalina.ha.ClusterManager) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) ClusterSession(org.apache.catalina.ha.ClusterSession) Session(org.apache.catalina.Session) DeltaSession(org.apache.catalina.ha.session.DeltaSession)

Example 30 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 (int i = 0; i < sessions.length; i++) {
        if (sessions[i] != null && sessions[i].isValid()) {
            sessions[i].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();
    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)

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