Search in sources :

Example 41 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", 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<>();
    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 42 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(javax.servlet.http.HttpSession) Session(org.apache.catalina.Session)

Example 43 with Session

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

the class TestPersistentManagerIntegration method backsUpOnce_56698.

@Test
public void backsUpOnce_56698() throws IOException, LifecycleException, InterruptedException {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.setDistributable(true);
    Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet());
    ctx.addServletMappingDecoded("/dummy", "DummyServlet");
    PersistentManager manager = new PersistentManager();
    TesterStore store = new TesterStore();
    manager.setStore(store);
    manager.setMaxIdleBackup(0);
    ctx.setManager(manager);
    tomcat.start();
    String sessionId = getUrl("http://localhost:" + getPort() + "/dummy").toString();
    // Note: PersistenceManager.findSession() silently updates
    // session.lastAccessedTime, so call it only once before other work.
    Session session = manager.findSession(sessionId);
    // Wait until request processing ends, as Request.recycle() updates
    // session.lastAccessedTime via session.endAccess().
    waitWhileSessionIsActive((StandardSession) session);
    long lastAccessedTime = session.getLastAccessedTimeInternal();
    // Session should be idle at least for 0 second (maxIdleBackup)
    // to be eligible for persistence, thus no need to wait.
    // Waiting a bit, to catch changes in last accessed time of a session
    waitForClockUpdate();
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());
    // session was not accessed, so no save will be performed
    waitForClockUpdate();
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());
    // access session
    session.access();
    session.endAccess();
    // session was accessed, so it will be saved once again
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId), store.getSavedIds());
    // session was not accessed, so once again no save will happen
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId), store.getSavedIds());
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) Tomcat(org.apache.catalina.startup.Tomcat) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.catalina.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 44 with Session

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

the class JvmRouteBinderValve method handlePossibleFailover.

private void handlePossibleFailover(Request request, DeltaSessionManager manager, String localJvmRoute) {
    String sessionId = request.getRequestedSessionId();
    if (sessionId != null) {
        // Get request JVM route
        String requestJvmRoute = null;
        int index = sessionId.indexOf(".");
        if (index > 0) {
            requestJvmRoute = sessionId.substring(index + 1, sessionId.length());
        }
        // If the requested JVM route doesn't equal the session's JVM route, handle failover
        if (requestJvmRoute != null && !requestJvmRoute.equals(localJvmRoute)) {
            if (manager.getLogger().isDebugEnabled()) {
                StringBuilder builder = new StringBuilder();
                builder.append(this).append(": Handling failover of session ").append(sessionId).append(" from ").append(requestJvmRoute).append(" to ").append(localJvmRoute);
                manager.getLogger().debug(builder.toString());
            }
            // Get the original session
            Session session = null;
            try {
                session = manager.findSession(sessionId);
            } catch (IOException e) {
                StringBuilder builder = new StringBuilder();
                builder.append(this).append(": Caught exception attempting to find session ").append(sessionId).append(" in ").append(manager);
                manager.getLogger().warn(builder.toString(), e);
            }
            if (session == null) {
                StringBuilder builder = new StringBuilder();
                builder.append(this).append(": Did not find session ").append(sessionId).append(" to failover in ").append(manager);
                manager.getLogger().warn(builder.toString());
            } else {
                // Change its session id. This removes the previous session and creates the new one.
                String baseSessionId = sessionId.substring(0, index);
                String newSessionId = baseSessionId + "." + localJvmRoute;
                session.setId(newSessionId);
                // Change the request's session id
                request.changeSessionId(newSessionId);
            }
        }
    }
}
Also used : IOException(java.io.IOException) Session(org.apache.catalina.Session)

Example 45 with Session

use of org.apache.catalina.Session in project Payara by payara.

the class FormAuthenticator method matchRequest.

/**
 * Does this request match the saved one (so that it must be the redirect
 * we signaled after successful authentication?
 *
 * @param request The request to be verified
 */
protected boolean matchRequest(HttpRequest request) {
    // Has a session been created?
    Session session = getSession(request, false);
    if (session == null)
        return (false);
    // Is there a saved request?
    SavedRequest sreq = (SavedRequest) session.getNote(Constants.FORM_REQUEST_NOTE);
    if (sreq == null)
        return (false);
    // Is there a saved principal?
    if (session.getNote(Constants.FORM_PRINCIPAL_NOTE) == null)
        return (false);
    // Does the request URI match?
    HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
    String requestURI = hreq.getRequestURI();
    if (requestURI == null)
        return (false);
    return (requestURI.equals(sreq.getRequestURI()));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) 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