Search in sources :

Example 16 with StringManager

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

the class HTMLHostManagerServlet method doPost.

/**
 * Process a POST 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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    StringManager smClient = StringManager.getManager(Constants.Package, request.getLocales());
    // Identify the request parameters that we need
    String command = request.getPathInfo();
    String name = request.getParameter("name");
    // 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) {
    // No command == list
    } else if (command.equals("/add")) {
        message = add(request, name, smClient);
    } else if (command.equals("/remove")) {
        message = remove(name, smClient);
    } else if (command.equals("/start")) {
        message = start(name, smClient);
    } else if (command.equals("/stop")) {
        message = stop(name, smClient);
    } else {
        // Try GET
        doGet(request, response);
    }
    list(request, response, message, smClient);
}
Also used : StringManager(org.apache.tomcat.util.res.StringManager)

Example 17 with StringManager

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

the class HostManagerServlet method getStringManager.

/**
 * @deprecated Use {@link StringManager#getManager(String, Enumeration)}.
 *             This method will be removed in Tomcat 8.
 */
@Deprecated
protected StringManager getStringManager(HttpServletRequest req) {
    Enumeration<Locale> requestedLocales = req.getLocales();
    while (requestedLocales.hasMoreElements()) {
        Locale locale = requestedLocales.nextElement();
        StringManager result = StringManager.getManager(Constants.Package, locale);
        if (result.getLocale().equals(locale)) {
            return result;
        }
    }
    // Return the default
    return sm;
}
Also used : Locale(java.util.Locale) StringManager(org.apache.tomcat.util.res.StringManager)

Example 18 with StringManager

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

the class ErrorReportValve method report.

// ------------------------------------------------------ Protected Methods
/**
 * Prints out an error report.
 *
 * @param request The request being processed
 * @param response The response being generated
 * @param throwable The exception that occurred (which possibly wraps
 *  a root cause exception
 */
protected void report(Request request, Response response, Throwable throwable) {
    int statusCode = response.getStatus();
    // and that error has not been reported.
    if (statusCode < 400 || response.getContentWritten() > 0 || !response.setErrorReported()) {
        return;
    }
    String message = RequestUtil.filter(response.getMessage());
    if (message == null) {
        if (throwable != null) {
            String exceptionMessage = throwable.getMessage();
            if (exceptionMessage != null && exceptionMessage.length() > 0) {
                message = RequestUtil.filter((new Scanner(exceptionMessage)).nextLine());
            }
        }
        if (message == null) {
            message = "";
        }
    }
    // Do nothing if there is no report for the specified status code and
    // no error message provided
    String report = null;
    StringManager smClient = StringManager.getManager(Constants.Package, request.getLocales());
    response.setLocale(smClient.getLocale());
    try {
        report = smClient.getString("http." + statusCode);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
    }
    if (report == null) {
        if (message.length() == 0) {
            return;
        } else {
            report = smClient.getString("errorReportValve.noDescription");
        }
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<html><head>");
    if (showServerInfo || showReport) {
        sb.append("<title>");
        if (showServerInfo) {
            sb.append(ServerInfo.getServerInfo()).append(" - ");
        }
        sb.append(smClient.getString("errorReportValve.errorReport"));
        sb.append("</title>");
        sb.append("<style><!--");
        sb.append(org.apache.catalina.util.TomcatCSS.TOMCAT_CSS);
        sb.append("--></style> ");
    } else {
        sb.append("<title>");
        sb.append(smClient.getString("errorReportValve.errorReport"));
        sb.append("</title>");
    }
    sb.append("</head><body>");
    sb.append("<h1>");
    sb.append(smClient.getString("errorReportValve.statusHeader", String.valueOf(statusCode), message)).append("</h1>");
    if (showReport) {
        sb.append("<HR size=\"1\" noshade=\"noshade\">");
        sb.append("<p><b>type</b> ");
        if (throwable != null) {
            sb.append(smClient.getString("errorReportValve.exceptionReport"));
        } else {
            sb.append(smClient.getString("errorReportValve.statusReport"));
        }
        sb.append("</p>");
        sb.append("<p><b>");
        sb.append(smClient.getString("errorReportValve.message"));
        sb.append("</b> <u>");
        sb.append(message).append("</u></p>");
        sb.append("<p><b>");
        sb.append(smClient.getString("errorReportValve.description"));
        sb.append("</b> <u>");
        sb.append(report);
        sb.append("</u></p>");
        if (throwable != null) {
            String stackTrace = getPartialServletStackTrace(throwable);
            sb.append("<p><b>");
            sb.append(smClient.getString("errorReportValve.exception"));
            sb.append("</b> <pre>");
            sb.append(RequestUtil.filter(stackTrace));
            sb.append("</pre></p>");
            int loops = 0;
            Throwable rootCause = throwable.getCause();
            while (rootCause != null && (loops < 10)) {
                stackTrace = getPartialServletStackTrace(rootCause);
                sb.append("<p><b>");
                sb.append(smClient.getString("errorReportValve.rootCause"));
                sb.append("</b> <pre>");
                sb.append(RequestUtil.filter(stackTrace));
                sb.append("</pre></p>");
                // In case root cause is somehow heavily nested
                rootCause = rootCause.getCause();
                loops++;
            }
            sb.append("<p><b>");
            sb.append(smClient.getString("errorReportValve.note"));
            sb.append("</b> <u>");
            sb.append(smClient.getString("errorReportValve.rootCauseInLogs", showServerInfo ? ServerInfo.getServerInfo() : ""));
            sb.append("</u></p>");
        }
        sb.append("<HR size=\"1\" noshade=\"noshade\">");
    }
    if (showServerInfo) {
        sb.append("<h3>").append(ServerInfo.getServerInfo()).append("</h3>");
    }
    sb.append("</body></html>");
    try {
        try {
            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            if (container.getLogger().isDebugEnabled()) {
                container.getLogger().debug("status.setContentType", t);
            }
        }
        Writer writer = response.getReporter();
        if (writer != null) {
            // If writer is null, it's an indication that the response has
            // been hard committed already, which should never happen
            writer.write(sb.toString());
            response.finishResponse();
        }
    } catch (IOException e) {
    // Ignore
    } catch (IllegalStateException e) {
    // Ignore
    }
}
Also used : Scanner(java.util.Scanner) IOException(java.io.IOException) Writer(java.io.Writer) StringManager(org.apache.tomcat.util.res.StringManager)

Example 19 with StringManager

use of org.apache.tomcat.util.res.StringManager in project tomcat 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(sm.getString("htmlManagerServlet.error.sessions", cn), e);
            message = smClient.getString("managerServlet.exception", e.toString());
        }
    } else if (command.equals("/sslConnectorCiphers")) {
        sslConnectorCiphers(request, response, smClient);
    } else if (command.equals("/sslConnectorCerts")) {
        sslConnectorCerts(request, response, smClient);
    } else if (command.equals("/sslConnectorTrustedCerts")) {
        sslConnectorTrustedCerts(request, response, smClient);
    } 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(jakarta.servlet.ServletException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StringManager(org.apache.tomcat.util.res.StringManager) ContextName(org.apache.catalina.util.ContextName)

Example 20 with StringManager

use of org.apache.tomcat.util.res.StringManager 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)

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