org.apache.catalina.valves.ErrorReportValve Java Examples

The following examples show how to use org.apache.catalina.valves.ErrorReportValve. 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: 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;
    }
}