Search in sources :

Example 46 with Manager

use of org.apache.catalina.Manager 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)

Example 47 with Manager

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

the class HostConfig method checkUndeploy.

/**
 * Check for old versions of applications using parallel deployment that are
 * now unused (have no active sessions) and undeploy any that are found.
 */
public synchronized void checkUndeploy() {
    // Need ordered set of names
    SortedSet<String> sortedAppNames = new TreeSet<String>();
    sortedAppNames.addAll(deployed.keySet());
    if (sortedAppNames.size() < 2) {
        return;
    }
    Iterator<String> iter = sortedAppNames.iterator();
    ContextName previous = new ContextName(iter.next(), false);
    do {
        ContextName current = new ContextName(iter.next(), false);
        if (current.getPath().equals(previous.getPath())) {
            // Current and previous are same path - current will always
            // be a later version
            Context previousContext = (Context) host.findChild(previous.getName());
            Context currentContext = (Context) host.findChild(current.getName());
            if (previousContext != null && currentContext != null && currentContext.getState().isAvailable() && !isServiced(previous.getName())) {
                Manager manager = previousContext.getManager();
                if (manager != null) {
                    int sessionCount;
                    if (manager instanceof DistributedManager) {
                        sessionCount = ((DistributedManager) manager).getActiveSessionsFull();
                    } else {
                        sessionCount = manager.getActiveSessions();
                    }
                    if (sessionCount == 0) {
                        if (log.isInfoEnabled()) {
                            log.info(sm.getString("hostConfig.undeployVersion", previous.getName()));
                        }
                        DeployedApplication app = deployed.get(previous.getName());
                        String[] resources = app.redeployResources.keySet().toArray(new String[0]);
                        // Version is unused - undeploy it completely
                        // The -1 is a 'trick' to ensure all redeploy
                        // resources are removed
                        undeploy(app);
                        deleteRedeployResources(app, resources, -1, true);
                    }
                }
            }
        }
        previous = current;
    } while (iter.hasNext());
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) Context(org.apache.catalina.Context) TreeSet(java.util.TreeSet) DistributedManager(org.apache.catalina.DistributedManager) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) ContextName(org.apache.catalina.util.ContextName) DistributedManager(org.apache.catalina.DistributedManager)

Example 48 with Manager

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

the class UpdateValve method invoke.

@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
    if (getNext() == null) {
        return;
    }
    // check if we already filtered/processed this request
    if (request.getNote(ALREADY_FILTERED_NOTE) == null) {
        request.setNote(ALREADY_FILTERED_NOTE, Boolean.TRUE);
        try {
            getNext().invoke(request, response);
        } finally {
            request.removeNote(ALREADY_FILTERED_NOTE);
            if (request.getContext() == null) {
                return;
            }
            final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            try {
                ClassLoader applicationClassLoader = request.getContext().getLoader().getClassLoader();
                Thread.currentThread().setContextClassLoader(applicationClassLoader);
                Manager manager = request.getContext().getManager();
                ((RedissonSessionManager) manager).store(request.getSession(false));
            } finally {
                Thread.currentThread().setContextClassLoader(classLoader);
            }
        }
    } else {
        getNext().invoke(request, response);
    }
}
Also used : Manager(org.apache.catalina.Manager)

Example 49 with Manager

use of org.apache.catalina.Manager 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 50 with Manager

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

the class HTMLManagerServlet method list.

/**
 * Render an HTML list of the currently active Contexts in our virtual host,
 * and memory and server status information.
 *
 * @param request The request
 * @param response The response
 * @param message a message to display
 * @param smClient internationalized strings
 * @throws IOException an IO error occurred
 */
protected void list(HttpServletRequest request, HttpServletResponse response, String message, StringManager smClient) throws IOException {
    if (debug >= 1) {
        log("list: Listing contexts for virtual host '" + host.getName() + "'");
    }
    PrintWriter writer = response.getWriter();
    Object[] args = new Object[2];
    args[0] = request.getContextPath();
    args[1] = smClient.getString("htmlManagerServlet.title");
    // HTML Header Section
    writer.print(MessageFormat.format(Constants.HTML_HEADER_SECTION, args));
    // Body Header Section
    writer.print(MessageFormat.format(Constants.BODY_HEADER_SECTION, args));
    // Message Section
    args = new Object[3];
    args[0] = smClient.getString("htmlManagerServlet.messageLabel");
    if (message == null || message.length() == 0) {
        args[1] = "OK";
    } else {
        args[1] = Escape.htmlElementContent(message);
    }
    writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args));
    // Manager Section
    args = new Object[9];
    args[0] = smClient.getString("htmlManagerServlet.manager");
    args[1] = response.encodeURL(request.getContextPath() + "/html/list");
    args[2] = smClient.getString("htmlManagerServlet.list");
    // External link
    args[3] = (request.getContextPath() + "/" + smClient.getString("htmlManagerServlet.helpHtmlManagerFile"));
    args[4] = smClient.getString("htmlManagerServlet.helpHtmlManager");
    // External link
    args[5] = (request.getContextPath() + "/" + smClient.getString("htmlManagerServlet.helpManagerFile"));
    args[6] = smClient.getString("htmlManagerServlet.helpManager");
    args[7] = response.encodeURL(request.getContextPath() + "/status");
    args[8] = smClient.getString("statusServlet.title");
    writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args));
    // Apps Header Section
    args = new Object[7];
    args[0] = smClient.getString("htmlManagerServlet.appsTitle");
    args[1] = smClient.getString("htmlManagerServlet.appsPath");
    args[2] = smClient.getString("htmlManagerServlet.appsVersion");
    args[3] = smClient.getString("htmlManagerServlet.appsName");
    args[4] = smClient.getString("htmlManagerServlet.appsAvailable");
    args[5] = smClient.getString("htmlManagerServlet.appsSessions");
    args[6] = smClient.getString("htmlManagerServlet.appsTasks");
    writer.print(MessageFormat.format(APPS_HEADER_SECTION, args));
    // Apps Row Section
    // Create sorted map of deployed applications by context name.
    Container[] children = host.findChildren();
    String[] contextNames = new String[children.length];
    for (int i = 0; i < children.length; i++) {
        contextNames[i] = children[i].getName();
    }
    Arrays.sort(contextNames);
    String appsStart = smClient.getString("htmlManagerServlet.appsStart");
    String appsStop = smClient.getString("htmlManagerServlet.appsStop");
    String appsReload = smClient.getString("htmlManagerServlet.appsReload");
    String appsUndeploy = smClient.getString("htmlManagerServlet.appsUndeploy");
    String appsExpire = smClient.getString("htmlManagerServlet.appsExpire");
    String noVersion = "<i>" + smClient.getString("htmlManagerServlet.noVersion") + "</i>";
    boolean isHighlighted = true;
    boolean isDeployed = true;
    String highlightColor = null;
    for (String contextName : contextNames) {
        Context ctxt = (Context) host.findChild(contextName);
        if (ctxt != null) {
            // Bugzilla 34818, alternating row colors
            isHighlighted = !isHighlighted;
            if (isHighlighted) {
                highlightColor = "#C3F3C3";
            } else {
                highlightColor = "#FFFFFF";
            }
            String contextPath = ctxt.getPath();
            String displayPath = contextPath;
            if (displayPath.equals("")) {
                displayPath = "/";
            }
            StringBuilder tmp = new StringBuilder();
            tmp.append("path=");
            tmp.append(URLEncoder.DEFAULT.encode(displayPath, StandardCharsets.UTF_8));
            final String webappVersion = ctxt.getWebappVersion();
            if (webappVersion != null && webappVersion.length() > 0) {
                tmp.append("&version=");
                tmp.append(URLEncoder.DEFAULT.encode(webappVersion, StandardCharsets.UTF_8));
            }
            String pathVersion = tmp.toString();
            try {
                isDeployed = isDeployed(contextName);
            } catch (Exception e) {
                // Assume false on failure for safety
                isDeployed = false;
            }
            args = new Object[7];
            // External link
            args[0] = "<a href=\"" + URLEncoder.DEFAULT.encode(contextPath + "/", StandardCharsets.UTF_8) + "\" " + Constants.REL_EXTERNAL + ">" + Escape.htmlElementContent(displayPath) + "</a>";
            if (webappVersion == null || webappVersion.isEmpty()) {
                args[1] = noVersion;
            } else {
                args[1] = Escape.htmlElementContent(webappVersion);
            }
            if (ctxt.getDisplayName() == null) {
                args[2] = "&nbsp;";
            } else {
                args[2] = Escape.htmlElementContent(ctxt.getDisplayName());
            }
            args[3] = Boolean.valueOf(ctxt.getState().isAvailable());
            args[4] = Escape.htmlElementContent(response.encodeURL(request.getContextPath() + "/html/sessions?" + pathVersion));
            Manager manager = ctxt.getManager();
            if (manager instanceof DistributedManager && showProxySessions) {
                args[5] = Integer.valueOf(((DistributedManager) manager).getActiveSessionsFull());
            } else if (manager != null) {
                args[5] = Integer.valueOf(manager.getActiveSessions());
            } else {
                args[5] = Integer.valueOf(0);
            }
            args[6] = highlightColor;
            writer.print(MessageFormat.format(APPS_ROW_DETAILS_SECTION, args));
            args = new Object[14];
            args[0] = Escape.htmlElementContent(response.encodeURL(request.getContextPath() + "/html/start?" + pathVersion));
            args[1] = appsStart;
            args[2] = Escape.htmlElementContent(response.encodeURL(request.getContextPath() + "/html/stop?" + pathVersion));
            args[3] = appsStop;
            args[4] = Escape.htmlElementContent(response.encodeURL(request.getContextPath() + "/html/reload?" + pathVersion));
            args[5] = appsReload;
            args[6] = Escape.htmlElementContent(response.encodeURL(request.getContextPath() + "/html/undeploy?" + pathVersion));
            args[7] = appsUndeploy;
            args[8] = Escape.htmlElementContent(response.encodeURL(request.getContextPath() + "/html/expire?" + pathVersion));
            args[9] = appsExpire;
            args[10] = smClient.getString("htmlManagerServlet.expire.explain");
            if (manager == null) {
                args[11] = smClient.getString("htmlManagerServlet.noManager");
            } else {
                args[11] = Integer.valueOf(ctxt.getSessionTimeout());
            }
            args[12] = smClient.getString("htmlManagerServlet.expire.unit");
            args[13] = highlightColor;
            if (ctxt.getName().equals(this.context.getName())) {
                writer.print(MessageFormat.format(MANAGER_APP_ROW_BUTTON_SECTION, args));
            } else if (ctxt.getState().isAvailable() && isDeployed) {
                writer.print(MessageFormat.format(STARTED_DEPLOYED_APPS_ROW_BUTTON_SECTION, args));
            } else if (ctxt.getState().isAvailable() && !isDeployed) {
                writer.print(MessageFormat.format(STARTED_NONDEPLOYED_APPS_ROW_BUTTON_SECTION, args));
            } else if (!ctxt.getState().isAvailable() && isDeployed) {
                writer.print(MessageFormat.format(STOPPED_DEPLOYED_APPS_ROW_BUTTON_SECTION, args));
            } else {
                writer.print(MessageFormat.format(STOPPED_NONDEPLOYED_APPS_ROW_BUTTON_SECTION, args));
            }
        }
    }
    // Deploy Section
    args = new Object[8];
    args[0] = smClient.getString("htmlManagerServlet.deployTitle");
    args[1] = smClient.getString("htmlManagerServlet.deployServer");
    args[2] = response.encodeURL(request.getContextPath() + "/html/deploy");
    args[3] = smClient.getString("htmlManagerServlet.deployPath");
    args[4] = smClient.getString("htmlManagerServlet.deployVersion");
    args[5] = smClient.getString("htmlManagerServlet.deployConfig");
    args[6] = smClient.getString("htmlManagerServlet.deployWar");
    args[7] = smClient.getString("htmlManagerServlet.deployButton");
    writer.print(MessageFormat.format(DEPLOY_SECTION, args));
    args = new Object[4];
    args[0] = smClient.getString("htmlManagerServlet.deployUpload");
    args[1] = response.encodeURL(request.getContextPath() + "/html/upload");
    args[2] = smClient.getString("htmlManagerServlet.deployUploadFile");
    args[3] = smClient.getString("htmlManagerServlet.deployButton");
    writer.print(MessageFormat.format(UPLOAD_SECTION, args));
    // Config section
    args = new Object[5];
    args[0] = smClient.getString("htmlManagerServlet.configTitle");
    args[1] = smClient.getString("htmlManagerServlet.configSslReloadTitle");
    args[2] = response.encodeURL(request.getContextPath() + "/html/sslReload");
    args[3] = smClient.getString("htmlManagerServlet.configSslHostName");
    args[4] = smClient.getString("htmlManagerServlet.configReloadButton");
    writer.print(MessageFormat.format(CONFIG_SECTION, args));
    // Diagnostics section
    args = new Object[15];
    args[0] = smClient.getString("htmlManagerServlet.diagnosticsTitle");
    args[1] = smClient.getString("htmlManagerServlet.diagnosticsLeak");
    args[2] = response.encodeURL(request.getContextPath() + "/html/findleaks");
    args[3] = smClient.getString("htmlManagerServlet.diagnosticsLeakWarning");
    args[4] = smClient.getString("htmlManagerServlet.diagnosticsLeakButton");
    args[5] = smClient.getString("htmlManagerServlet.diagnosticsSsl");
    args[6] = response.encodeURL(request.getContextPath() + "/html/sslConnectorCiphers");
    args[7] = smClient.getString("htmlManagerServlet.diagnosticsSslConnectorCipherButton");
    args[8] = smClient.getString("htmlManagerServlet.diagnosticsSslConnectorCipherText");
    args[9] = response.encodeURL(request.getContextPath() + "/html/sslConnectorCerts");
    args[10] = smClient.getString("htmlManagerServlet.diagnosticsSslConnectorCertsButton");
    args[11] = smClient.getString("htmlManagerServlet.diagnosticsSslConnectorCertsText");
    args[12] = response.encodeURL(request.getContextPath() + "/html/sslConnectorTrustedCerts");
    args[13] = smClient.getString("htmlManagerServlet.diagnosticsSslConnectorTrustedCertsButton");
    args[14] = smClient.getString("htmlManagerServlet.diagnosticsSslConnectorTrustedCertsText");
    writer.print(MessageFormat.format(DIAGNOSTICS_SECTION, args));
    // Server Header Section
    args = new Object[9];
    args[0] = smClient.getString("htmlManagerServlet.serverTitle");
    args[1] = smClient.getString("htmlManagerServlet.serverVersion");
    args[2] = smClient.getString("htmlManagerServlet.serverJVMVersion");
    args[3] = smClient.getString("htmlManagerServlet.serverJVMVendor");
    args[4] = smClient.getString("htmlManagerServlet.serverOSName");
    args[5] = smClient.getString("htmlManagerServlet.serverOSVersion");
    args[6] = smClient.getString("htmlManagerServlet.serverOSArch");
    args[7] = smClient.getString("htmlManagerServlet.serverHostname");
    args[8] = smClient.getString("htmlManagerServlet.serverIPAddress");
    writer.print(MessageFormat.format(Constants.SERVER_HEADER_SECTION, args));
    // Server Row Section
    args = new Object[8];
    args[0] = ServerInfo.getServerInfo();
    args[1] = System.getProperty("java.runtime.version");
    args[2] = System.getProperty("java.vm.vendor");
    args[3] = System.getProperty("os.name");
    args[4] = System.getProperty("os.version");
    args[5] = System.getProperty("os.arch");
    try {
        InetAddress address = InetAddress.getLocalHost();
        args[6] = address.getHostName();
        args[7] = address.getHostAddress();
    } catch (UnknownHostException e) {
        args[6] = "-";
        args[7] = "-";
    }
    writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args));
    // HTML Tail Section
    writer.print(Constants.HTML_TAIL_SECTION);
    // Finish up the response
    writer.flush();
    writer.close();
}
Also used : Context(org.apache.catalina.Context) UnknownHostException(java.net.UnknownHostException) DistributedManager(org.apache.catalina.DistributedManager) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DistributedManager(org.apache.catalina.DistributedManager) Container(org.apache.catalina.Container) InetAddress(java.net.InetAddress) PrintWriter(java.io.PrintWriter)

Aggregations

Manager (org.apache.catalina.Manager)56 StringManager (org.apache.tomcat.util.res.StringManager)26 IOException (java.io.IOException)24 Session (org.apache.catalina.Session)24 Context (org.apache.catalina.Context)19 Container (org.apache.catalina.Container)13 LifecycleException (org.apache.catalina.LifecycleException)12 Lifecycle (org.apache.catalina.Lifecycle)11 Loader (org.apache.catalina.Loader)11 InstanceManager (org.apache.tomcat.InstanceManager)11 StandardManager (org.apache.catalina.session.StandardManager)10 Realm (org.apache.catalina.Realm)9 HttpSession (javax.servlet.http.HttpSession)8 ServletException (javax.servlet.ServletException)7 ArrayList (java.util.ArrayList)6 NamingException (javax.naming.NamingException)6 Cluster (org.apache.catalina.Cluster)6 DistributedManager (org.apache.catalina.DistributedManager)6 StandardContext (org.apache.catalina.core.StandardContext)6 WebappLoader (org.apache.catalina.loader.WebappLoader)6