Search in sources :

Example 61 with Session

use of org.apache.catalina.Session in project tomcat70 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", RequestUtil.filter(path)));
    }
    Context ctxt = (Context) host.findChild(cn.getName());
    if (null == ctxt) {
        throw new IllegalArgumentException(smClient.getString("managerServlet.noContext", RequestUtil.filter(cn.getDisplayName())));
    }
    Manager manager = ctxt.getManager();
    List<Session> sessions = new ArrayList<Session>();
    sessions.addAll(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(javax.servlet.http.HttpSession) Session(org.apache.catalina.Session) DistributedManager(org.apache.catalina.DistributedManager)

Example 62 with Session

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

the class ManagerServlet method sessions.

/**
 * Session information for the web application at the specified context path.
 * Displays a profile of session thisAccessedTime listing number
 * of sessions for each 10 minute interval up to 10 hours.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to list session information for
 * @param idle Expire all sessions with idle time &gt; idle for this context
 */
protected void sessions(PrintWriter writer, ContextName cn, int idle, StringManager smClient) {
    if (debug >= 1) {
        log("sessions: Session information for web application '" + cn + "'");
        if (idle >= 0)
            log("sessions: Session expiration for " + idle + " minutes '" + cn + "'");
    }
    if (!validateContextName(cn, writer, smClient)) {
        return;
    }
    String displayPath = cn.getDisplayName();
    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext", RequestUtil.filter(displayPath)));
            return;
        }
        Manager manager = context.getManager();
        if (manager == null) {
            writer.println(smClient.getString("managerServlet.noManager", RequestUtil.filter(displayPath)));
            return;
        }
        int maxCount = 60;
        int histoInterval = 1;
        int maxInactiveInterval = context.getSessionTimeout();
        if (maxInactiveInterval > 0) {
            histoInterval = maxInactiveInterval / maxCount;
            if (histoInterval * maxCount < maxInactiveInterval)
                histoInterval++;
            if (0 == histoInterval)
                histoInterval = 1;
            maxCount = maxInactiveInterval / histoInterval;
            if (histoInterval * maxCount < maxInactiveInterval)
                maxCount++;
        }
        writer.println(smClient.getString("managerServlet.sessions", displayPath));
        writer.println(smClient.getString("managerServlet.sessiondefaultmax", "" + maxInactiveInterval));
        Session[] sessions = manager.findSessions();
        int[] timeout = new int[maxCount + 1];
        int notimeout = 0;
        int expired = 0;
        long now = System.currentTimeMillis();
        for (int i = 0; i < sessions.length; i++) {
            int time;
            if (LAST_ACCESS_AT_START) {
                time = (int) ((now - sessions[i].getLastAccessedTimeInternal()) / 1000L);
            } else {
                time = (int) ((now - sessions[i].getThisAccessedTimeInternal()) / 1000L);
            }
            if (idle >= 0 && time >= idle * 60) {
                sessions[i].expire();
                expired++;
            }
            time = time / 60 / histoInterval;
            if (time < 0)
                notimeout++;
            else if (time >= maxCount)
                timeout[maxCount]++;
            else
                timeout[time]++;
        }
        if (timeout[0] > 0)
            writer.println(smClient.getString("managerServlet.sessiontimeout", "<" + histoInterval, "" + timeout[0]));
        for (int i = 1; i < maxCount; i++) {
            if (timeout[i] > 0)
                writer.println(smClient.getString("managerServlet.sessiontimeout", "" + (i) * histoInterval + " - <" + (i + 1) * histoInterval, "" + timeout[i]));
        }
        if (timeout[maxCount] > 0) {
            writer.println(smClient.getString("managerServlet.sessiontimeout", ">=" + maxCount * histoInterval, "" + timeout[maxCount]));
        }
        if (notimeout > 0)
            writer.println(smClient.getString("managerServlet.sessiontimeout.unlimited", "" + notimeout));
        if (idle >= 0)
            writer.println(smClient.getString("managerServlet.sessiontimeout.expired", ">" + idle, "" + expired));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.sessions[" + displayPath + "]", t);
        writer.println(smClient.getString("managerServlet.exception", t.toString()));
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) Session(org.apache.catalina.Session)

Example 63 with Session

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

the class ManagerBase method getSession.

/**
 * Returns information about the session with the given session id.
 *
 * <p>The session information is organized as a HashMap, mapping
 * session attribute names to the String representation of their values.
 *
 * @param sessionId Session id
 *
 * @return HashMap mapping session attribute names to the String
 * representation of their values, or null if no session with the
 * specified id exists, or if the session does not have any attributes
 */
public HashMap<String, String> getSession(String sessionId) {
    Session s = sessions.get(sessionId);
    if (s == null) {
        if (log.isInfoEnabled()) {
            log.info("Session not found " + sessionId);
        }
        return null;
    }
    Enumeration<String> ee = s.getSession().getAttributeNames();
    if (ee == null || !ee.hasMoreElements()) {
        return null;
    }
    HashMap<String, String> map = new HashMap<String, String>();
    while (ee.hasMoreElements()) {
        String attrName = ee.nextElement();
        map.put(attrName, getSessionAttribute(sessionId, attrName));
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Session(org.apache.catalina.Session)

Example 64 with Session

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

the class PersistentManagerBase method findSession.

/**
 * {@inheritDoc}
 * <p>
 * This method checks the persistence store if persistence is enabled,
 * otherwise just uses the functionality from ManagerBase.
 */
@Override
public Session findSession(String id) throws IOException {
    Session session = super.findSession(id);
    // safely.
    if (session != null) {
        synchronized (session) {
            session = super.findSession(session.getIdInternal());
            if (session != null) {
                // To keep any external calling code from messing up the
                // concurrency.
                session.access();
                session.endAccess();
            }
        }
    }
    if (session != null)
        return session;
    // See if the Session is in the Store
    session = swapIn(id);
    return session;
}
Also used : Session(org.apache.catalina.Session)

Example 65 with Session

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

the class PersistentValve method invoke.

// --------------------------------------------------------- Public Methods
/**
 * 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 PersistentManager) {
        Store store = ((PersistentManager) 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 PersistentManager) {
                    Session session = manager.findSession(newsessionId);
                    Store store = ((PersistentManager) manager).getStore();
                    if (store != null && session != null && session.isValid() && !isSessionStale(session, System.currentTimeMillis())) {
                        store.save(session);
                        ((PersistentManager) 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();
            }
        }
    }
}
Also used : Context(org.apache.catalina.Context) PersistentManager(org.apache.catalina.session.PersistentManager) Store(org.apache.catalina.Store) PersistentManager(org.apache.catalina.session.PersistentManager) Manager(org.apache.catalina.Manager) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) 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