Java Code Examples for org.apache.tomcat.util.res.StringManager#getString()

The following examples show how to use org.apache.tomcat.util.res.StringManager#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTMLManagerServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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<>();
    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;
}
 
Example 2
Source File: HTMLHostManagerServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * 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
    String command = request.getPathInfo();

    // 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("/list")) {
        // Nothing to do - always generate list
    } else if (command.equals("/add") || command.equals("/remove") ||
            command.equals("/start") || command.equals("/stop") ||
            command.equals("/persist")) {
        message = smClient.getString(
                "hostManagerServlet.postCommand", command);
    } else {
        message = smClient.getString(
                "hostManagerServlet.unknownCommand", command);
    }

    list(request, response, message, smClient);
}
 
Example 3
Source File: ManagerServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Upload the WAR file included in this request, and store it at the
 * specified file location.
 *
 * @param writer    Writer to render to
 * @param request   The servlet request we are processing
 * @param war       The file into which we should store the uploaded WAR
 * @param smClient  The StringManager used to construct i18n messages based
 *                  on the Locale of the client
 *
 * @exception IOException if an I/O error occurs during processing
 */
protected void uploadWar(PrintWriter writer, HttpServletRequest request,
        File war, StringManager smClient) throws IOException {

    if (war.exists() && !war.delete()) {
        String msg = smClient.getString("managerServlet.deleteFail", war);
        throw new IOException(msg);
    }

    try (ServletInputStream istream = request.getInputStream();
            BufferedOutputStream ostream =
                    new BufferedOutputStream(new FileOutputStream(war), 1024)) {
        byte buffer[] = new byte[1024];
        while (true) {
            int n = istream.read(buffer);
            if (n < 0) {
                break;
            }
            ostream.write(buffer, 0, n);
        }
    } catch (IOException e) {
        if (war.exists() && !war.delete()) {
            writer.println(
                    smClient.getString("managerServlet.deleteFail", war));
        }
        throw e;
    }

}
 
Example 4
Source File: HTMLManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: HTMLHostManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * 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
    String command = request.getPathInfo();

    // 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("/list")) {
        // Nothing to do - always generate list
    } else if (command.equals("/add") || command.equals("/remove") ||
            command.equals("/start") || command.equals("/stop")) {
        message = smClient.getString(
                "hostManagerServlet.postCommand", command);
    } else {
        message = smClient.getString(
                "hostManagerServlet.unknownCommand", command);
    }

    list(request, response, message, smClient);
}
 
Example 6
Source File: HTMLManagerServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: HTMLHostManagerServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * 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
    String command = request.getPathInfo();

    // 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("/list")) {
        // Nothing to do - always generate list
    } else if (command.equals("/add") || command.equals("/remove") ||
            command.equals("/start") || command.equals("/stop")) {
        message = smClient.getString(
                "hostManagerServlet.postCommand", command);
    } else {
        message = smClient.getString(
                "hostManagerServlet.unknownCommand", command);
    }

    list(request, response, message, smClient);
}
 
Example 8
Source File: HTMLManagerServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected String upload(HttpServletRequest request, StringManager smClient) {
    String message = "";

    try {
        while (true) {
            Part warPart = request.getPart("deployWar");
            if (warPart == null) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadNoFile");
                break;
            }
            String filename = warPart.getSubmittedFileName();
            if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadNotWar", filename);
                break;
            }
            // Get the filename if uploaded name includes a path
            if (filename.lastIndexOf('\\') >= 0) {
                filename =
                    filename.substring(filename.lastIndexOf('\\') + 1);
            }
            if (filename.lastIndexOf('/') >= 0) {
                filename =
                    filename.substring(filename.lastIndexOf('/') + 1);
            }

            // Identify the appBase of the owning Host of this Context
            // (if any)
            File file = new File(host.getAppBaseFile(), filename);
            if (file.exists()) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadWarExists",
                        filename);
                break;
            }

            ContextName cn = new ContextName(filename, true);
            String name = cn.getName();

            if ((host.findChild(name) != null) && !isDeployed(name)) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadInServerXml",
                        filename);
                break;
            }

            if (isServiced(name)) {
                message = smClient.getString("managerServlet.inService", name);
            } else {
                addServiced(name);
                try {
                    warPart.write(file.getAbsolutePath());
                    // Perform new deployment
                    check(name);
                } finally {
                    removeServiced(name);
                }
            }
            break;
        }
    } catch(Exception e) {
        message = smClient.getString
            ("htmlManagerServlet.deployUploadFail", e.getMessage());
        log(message, e);
    }
    return message;
}
 
Example 9
Source File: TestHttp2InitialConnection.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
protected String getResponseBodyFrameTrace(int streamId, String body) {
    if (testData.getExpectedStatus() == 200) {
        return super.getResponseBodyFrameTrace(streamId, body);
    } else if (testData.getExpectedStatus() == 400) {
        /*
         * Need to be careful here. The test wants the exact content length
         * in bytes.
         * This will vary depending on where the test is run due to:
         * - The length of the version string that appears once in the error
         *   page
         * - The status header uses a UTF-8 EN dash. When running in an IDE
         *   the UTF-8 properties files will be used directly rather than
         *   after native2ascii conversion.
         *
         * Note: The status header appears twice in the error page.
         */
        int serverInfoLength = ServerInfo.getServerInfo().getBytes().length;
        StringManager sm = StringManager.getManager(
                ErrorReportValve.class.getPackage().getName(), Locale.ENGLISH);
        String reason = sm.getString("http." + testData.getExpectedStatus() + ".reason");
        int descriptionLength = sm.getString("http." + testData.getExpectedStatus() + ".desc")
                .getBytes(StandardCharsets.UTF_8).length;
        int statusHeaderLength = sm
                .getString("errorReportValve.statusHeader",
                        String.valueOf(testData.getExpectedStatus()), reason)
                .getBytes(StandardCharsets.UTF_8).length;
        int typeLabelLength = sm.getString("errorReportValve.type")
                .getBytes(StandardCharsets.UTF_8).length;
        int statusReportLabelLength = sm.getString("errorReportValve.statusReport")
                .getBytes(StandardCharsets.UTF_8).length;
        int descriptionLabelLength = sm.getString("errorReportValve.description")
                .getBytes(StandardCharsets.UTF_8).length;
        // 196 bytes is the static length of the pure HTML code from the ErrorReportValve
        int len = 196 + org.apache.catalina.util.TomcatCSS.TOMCAT_CSS
                .getBytes(StandardCharsets.UTF_8).length +
                typeLabelLength + statusReportLabelLength + descriptionLabelLength +
                descriptionLength + serverInfoLength + statusHeaderLength * 2;
        String contentLength = String.valueOf(len);
        return getResponseBodyFrameTrace(streamId,
                testData.getExpectedStatus(), "text/html;charset=utf-8",
                "en", contentLength, contentLength);
    } else {
        Assert.fail();
        // To keep the IDE happy
        return null;
    }
}
 
Example 10
Source File: HTMLManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
protected String upload(HttpServletRequest request, StringManager smClient) {
    String message = "";

    try {
        while (true) {
            Part warPart = request.getPart("deployWar");
            if (warPart == null) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadNoFile");
                break;
            }
            String filename =
                extractFilename(warPart.getHeader("Content-Disposition"));
            if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadNotWar", filename);
                break;
            }
            // Get the filename if uploaded name includes a path
            if (filename.lastIndexOf('\\') >= 0) {
                filename =
                    filename.substring(filename.lastIndexOf('\\') + 1);
            }
            if (filename.lastIndexOf('/') >= 0) {
                filename =
                    filename.substring(filename.lastIndexOf('/') + 1);
            }

            // Identify the appBase of the owning Host of this Context
            // (if any)
            File file = new File(deployed, filename);
            if (file.exists()) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadWarExists",
                        filename);
                break;
            }
            
            ContextName cn = new ContextName(filename, true);
            String name = cn.getName();

            if ((host.findChild(name) != null) && !isDeployed(name)) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadInServerXml",
                        filename);
                break;
            }

            if (isServiced(name)) {
                message = smClient.getString("managerServlet.inService", name);
            } else {
                addServiced(name);
                try {
                    warPart.write(file.getAbsolutePath());
                    // Perform new deployment
                    check(name);
                } finally {
                    removeServiced(name);
                }
            }
            break;
        }
    } catch(Exception e) {
        message = smClient.getString
            ("htmlManagerServlet.deployUploadFail", e.getMessage());
        log(message, e);
    }
    return message;
}
 
Example 11
Source File: HTMLManagerServlet.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
protected String upload(HttpServletRequest request, StringManager smClient) {
    String message = "";

    try {
        while (true) {
            Part warPart = request.getPart("deployWar");
            if (warPart == null) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadNoFile");
                break;
            }
            String filename =
                extractFilename(warPart.getHeader("Content-Disposition"));
            if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadNotWar", filename);
                break;
            }
            // Get the filename if uploaded name includes a path
            if (filename.lastIndexOf('\\') >= 0) {
                filename =
                    filename.substring(filename.lastIndexOf('\\') + 1);
            }
            if (filename.lastIndexOf('/') >= 0) {
                filename =
                    filename.substring(filename.lastIndexOf('/') + 1);
            }

            // Identify the appBase of the owning Host of this Context
            // (if any)
            File file = new File(deployed, filename);
            if (file.exists()) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadWarExists",
                        filename);
                break;
            }
            
            ContextName cn = new ContextName(filename, true);
            String name = cn.getName();

            if ((host.findChild(name) != null) && !isDeployed(name)) {
                message = smClient.getString(
                        "htmlManagerServlet.deployUploadInServerXml",
                        filename);
                break;
            }

            if (isServiced(name)) {
                message = smClient.getString("managerServlet.inService", name);
            } else {
                addServiced(name);
                try {
                    warPart.write(file.getAbsolutePath());
                    // Perform new deployment
                    check(name);
                } finally {
                    removeServiced(name);
                }
            }
            break;
        }
    } catch(Exception e) {
        message = smClient.getString
            ("htmlManagerServlet.deployUploadFail", e.getMessage());
        log(message, e);
    }
    return message;
}