Search in sources :

Example 6 with StringManager

use of org.apache.tomcat.util.res.StringManager in project tomcat 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 > idle for this context
 * @param smClient i18n support for current client's locale
 */
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", Escape.htmlElementContent(displayPath)));
            return;
        }
        Manager manager = context.getManager();
        if (manager == null) {
            writer.println(smClient.getString("managerServlet.noManager", Escape.htmlElementContent(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;
        for (Session session : sessions) {
            int time = (int) (session.getIdleTimeInternal() / 1000L);
            if (idle >= 0 && time >= idle * 60) {
                session.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(sm.getString("managerServlet.error.sessions", displayPath), t);
        writer.println(smClient.getString("managerServlet.exception", t.toString()));
    }
}
Also used : SSLContext(org.apache.tomcat.util.net.SSLContext) ServletContext(jakarta.servlet.ServletContext) Context(org.apache.catalina.Context) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) Session(org.apache.catalina.Session)

Example 7 with StringManager

use of org.apache.tomcat.util.res.StringManager in project tomcat by apache.

the class ManagerServlet method doPut.

/**
 * Process a PUT request for the specified resource.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet-specified error occurs
 */
@Override
public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    StringManager smClient = StringManager.getManager(Constants.Package, request.getLocales());
    // Identify the request parameters that we need
    String command = request.getPathInfo();
    if (command == null) {
        command = request.getServletPath();
    }
    String path = request.getParameter("path");
    ContextName cn = null;
    if (path != null) {
        cn = new ContextName(path, request.getParameter("version"));
    }
    String config = request.getParameter("config");
    String tag = request.getParameter("tag");
    boolean update = false;
    if ((request.getParameter("update") != null) && (request.getParameter("update").equals("true"))) {
        update = true;
    }
    // Prepare our output writer to generate the response message
    response.setContentType("text/plain;charset=" + Constants.CHARSET);
    // Stop older versions of IE thinking they know best. We set text/plain
    // in the line above for a reason. IE's behaviour is unwanted at best
    // and dangerous at worst.
    response.setHeader("X-Content-Type-Options", "nosniff");
    PrintWriter writer = response.getWriter();
    // Process the requested command
    if (command == null) {
        writer.println(smClient.getString("managerServlet.noCommand"));
    } else if (command.equals("/deploy")) {
        deploy(writer, config, cn, tag, update, request, smClient);
    } else {
        writer.println(smClient.getString("managerServlet.unknownCommand", command));
    }
    // Finish up the response
    writer.flush();
    writer.close();
}
Also used : StringManager(org.apache.tomcat.util.res.StringManager) ContextName(org.apache.catalina.util.ContextName) PrintWriter(java.io.PrintWriter)

Example 8 with StringManager

use of org.apache.tomcat.util.res.StringManager in project tomcat70 by apache.

the class HttpMessages method getInstance.

public static HttpMessages getInstance(Locale locale) {
    HttpMessages result = instances.get(locale);
    if (result == null) {
        StringManager sm = StringManager.getManager("org.apache.tomcat.util.http.res", locale);
        if (Locale.getDefault().equals(sm.getLocale())) {
            result = DEFAULT;
        } else {
            result = new HttpMessages(sm);
        }
        instances.put(locale, result);
    }
    return result;
}
Also used : StringManager(org.apache.tomcat.util.res.StringManager)

Example 9 with StringManager

use of org.apache.tomcat.util.res.StringManager in project tomcat70 by apache.

the class HTMLManagerServlet method doGet.

// --------------------------------------------------------- Public Methods
/**
 * Process a GET request for the specified resource.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet-specified error occurs
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    StringManager smClient = StringManager.getManager(Constants.Package, request.getLocales());
    // Identify the request parameters that we need
    // By obtaining the command from the pathInfo, per-command security can
    // be configured in web.xml
    String command = request.getPathInfo();
    String path = request.getParameter("path");
    ContextName cn = null;
    if (path != null) {
        cn = new ContextName(path, request.getParameter("version"));
    }
    // Prepare our output writer to generate the response message
    response.setContentType("text/html; charset=" + Constants.CHARSET);
    String message = "";
    // Process the requested command
    if (command == null || command.equals("/")) {
    // No command == list
    } else if (command.equals("/list")) {
    // List always displayed - nothing to do here
    } else if (command.equals("/sessions")) {
        try {
            doSessions(cn, request, response, smClient);
            return;
        } catch (Exception e) {
            log("HTMLManagerServlet.sessions[" + cn + "]", e);
            message = smClient.getString("managerServlet.exception", e.toString());
        }
    } else if (command.equals("/upload") || command.equals("/deploy") || command.equals("/reload") || command.equals("/undeploy") || command.equals("/expire") || command.equals("/start") || command.equals("/stop")) {
        message = smClient.getString("managerServlet.postCommand", command);
    } else {
        message = smClient.getString("managerServlet.unknownCommand", command);
    }
    list(request, response, message, smClient);
}
Also used : ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StringManager(org.apache.tomcat.util.res.StringManager) ContextName(org.apache.catalina.util.ContextName)

Example 10 with StringManager

use of org.apache.tomcat.util.res.StringManager in project tomcat70 by apache.

the class HTMLManagerServlet method list.

/**
 * Render a 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
 */
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();
    // HTML Header Section
    writer.print(Constants.HTML_HEADER_SECTION);
    // Body Header Section
    Object[] args = new Object[2];
    args[0] = request.getContextPath();
    args[1] = smClient.getString("htmlManagerServlet.title");
    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] = RequestUtil.filter(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");
    args[3] = response.encodeURL(request.getContextPath() + "/" + smClient.getString("htmlManagerServlet.helpHtmlManagerFile"));
    args[4] = smClient.getString("htmlManagerServlet.helpHtmlManager");
    args[5] = response.encodeURL(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, "UTF-8"));
            if (ctxt.getWebappVersion().length() > 0) {
                tmp.append("&version=");
                tmp.append(URLEncoder.DEFAULT.encode(ctxt.getWebappVersion(), "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];
            args[0] = "<a href=\"" + URLEncoder.DEFAULT.encode(contextPath + "/", "UTF-8") + "\">" + RequestUtil.filter(displayPath) + "</a>";
            if ("".equals(ctxt.getWebappVersion())) {
                args[1] = noVersion;
            } else {
                args[1] = RequestUtil.filter(ctxt.getWebappVersion());
            }
            if (ctxt.getDisplayName() == null) {
                args[2] = "&nbsp;";
            } else {
                args[2] = RequestUtil.filter(ctxt.getDisplayName());
            }
            args[3] = Boolean.valueOf(ctxt.getState().isAvailable());
            args[4] = RequestUtil.filter(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] = RequestUtil.filter(response.encodeURL(request.getContextPath() + "/html/start?" + pathVersion));
            args[1] = appsStart;
            args[2] = RequestUtil.filter(response.encodeURL(request.getContextPath() + "/html/stop?" + pathVersion));
            args[3] = appsStop;
            args[4] = RequestUtil.filter(response.encodeURL(request.getContextPath() + "/html/reload?" + pathVersion));
            args[5] = appsReload;
            args[6] = RequestUtil.filter(response.encodeURL(request.getContextPath() + "/html/undeploy?" + pathVersion));
            args[7] = appsUndeploy;
            args[8] = RequestUtil.filter(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[7];
    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.deployConfig");
    args[5] = smClient.getString("htmlManagerServlet.deployWar");
    args[6] = 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));
    // Diagnostics section
    args = new Object[5];
    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");
    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(javax.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

StringManager (org.apache.tomcat.util.res.StringManager)27 PrintWriter (java.io.PrintWriter)9 IOException (java.io.IOException)8 ContextName (org.apache.catalina.util.ContextName)8 Context (org.apache.catalina.Context)6 Manager (org.apache.catalina.Manager)6 UnknownHostException (java.net.UnknownHostException)5 DistributedManager (org.apache.catalina.DistributedManager)4 Session (org.apache.catalina.Session)4 ServletException (jakarta.servlet.ServletException)3 Writer (java.io.Writer)3 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 Scanner (java.util.Scanner)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ServletException (javax.servlet.ServletException)2 Container (org.apache.catalina.Container)2 ServletContext (jakarta.servlet.ServletContext)1 HttpSession (jakarta.servlet.http.HttpSession)1