Search in sources :

Example 11 with Scanner

use of java.util.Scanner in project tomcat 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 reason phrase for the specified status code and
    // no error message provided
    String reason = null;
    String description = null;
    StringManager smClient = StringManager.getManager(Constants.Package, request.getLocales());
    response.setLocale(smClient.getLocale());
    try {
        reason = smClient.getString("http." + statusCode + ".reason");
        description = smClient.getString("http." + statusCode + ".desc");
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
    }
    if (reason == null || description == null) {
        if (message.isEmpty()) {
            return;
        } else {
            reason = smClient.getString("errorReportValve.unknownReason");
            description = smClient.getString("errorReportValve.noDescription");
        }
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<!doctype html><html lang=\"");
    sb.append(smClient.getLocale().getLanguage()).append("\">");
    sb.append("<head>");
    sb.append("<title>");
    sb.append(smClient.getString("errorReportValve.statusHeader", String.valueOf(statusCode), reason));
    sb.append("</title>");
    sb.append("<style type=\"text/css\">");
    sb.append(TomcatCSS.TOMCAT_CSS);
    sb.append("</style>");
    sb.append("</head><body>");
    sb.append("<h1>");
    sb.append(smClient.getString("errorReportValve.statusHeader", String.valueOf(statusCode), reason)).append("</h1>");
    if (isShowReport()) {
        sb.append("<hr class=\"line\" />");
        sb.append("<p><b>");
        sb.append(smClient.getString("errorReportValve.type"));
        sb.append("</b> ");
        if (throwable != null) {
            sb.append(smClient.getString("errorReportValve.exceptionReport"));
        } else {
            sb.append(smClient.getString("errorReportValve.statusReport"));
        }
        sb.append("</p>");
        if (!message.isEmpty()) {
            sb.append("<p><b>");
            sb.append(smClient.getString("errorReportValve.message"));
            sb.append("</b> ");
            sb.append(message).append("</p>");
        }
        sb.append("<p><b>");
        sb.append(smClient.getString("errorReportValve.description"));
        sb.append("</b> ");
        sb.append(description);
        sb.append("</p>");
        if (throwable != null) {
            String stackTrace = getPartialServletStackTrace(throwable);
            sb.append("<p><b>");
            sb.append(smClient.getString("errorReportValve.exception"));
            sb.append("</b></p><pre>");
            sb.append(RequestUtil.filter(stackTrace));
            sb.append("</pre>");
            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></p><pre>");
                sb.append(RequestUtil.filter(stackTrace));
                sb.append("</pre>");
                // 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> ");
            sb.append(smClient.getString("errorReportValve.rootCauseInLogs"));
            sb.append("</p>");
        }
        sb.append("<hr class=\"line\" />");
    }
    if (isShowServerInfo()) {
        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 12 with Scanner

use of java.util.Scanner in project zookeeper by apache.

the class ReconfigBackupTest method getFileContent.

// upgrade this once we have Google-Guava or Java 7+
public static String getFileContent(File file) throws FileNotFoundException {
    Scanner sc = new Scanner(file);
    StringBuilder sb = new StringBuilder();
    while (sc.hasNextLine()) {
        sb.append(sc.nextLine() + "\n");
    }
    return sb.toString();
}
Also used : Scanner(java.util.Scanner)

Example 13 with Scanner

use of java.util.Scanner in project checkstyle by checkstyle.

the class InputRightCurlyDoWhile method foo4.

public void foo4() {
    int prog, user;
    prog = (int) (Math.random() * 10) + 1;
    Scanner input = new Scanner(System.in, "utf-8");
    if (input.hasNextInt()) {
        do {
            user = input.nextInt();
            if (user == prog) {
                String.CASE_INSENSITIVE_ORDER.equals("Good!");
            } else {
                if (user > 0 && user <= 10) {
                    String.CASE_INSENSITIVE_ORDER.equals("Bad! ");
                    if (prog < user) {
                        String.CASE_INSENSITIVE_ORDER.equals("My number is less than yours.");
                    } else {
                        String.CASE_INSENSITIVE_ORDER.equals("My number is greater than yours.");
                    }
                } else {
                    String.CASE_INSENSITIVE_ORDER.equals("Error!");
                }
            }
        } while (user != prog);
    } else {
        String.CASE_INSENSITIVE_ORDER.equals("Error!");
    }
    String.CASE_INSENSITIVE_ORDER.equals("Goodbye!");
}
Also used : Scanner(java.util.Scanner)

Example 14 with Scanner

use of java.util.Scanner in project cucumber-jvm by cucumber.

the class JSONPrettyFormatterTest method featureWithOutlineTest.

@Test
public void featureWithOutlineTest() throws Exception {
    File report = runFeaturesWithJSONPrettyFormatter(asList("cucumber/runtime/formatter/JSONPrettyFormatterTest.feature"));
    String expected = new Scanner(getClass().getResourceAsStream("JSONPrettyFormatterTest.json"), "UTF-8").useDelimiter("\\A").next();
    String actual = new Scanner(report, "UTF-8").useDelimiter("\\A").next();
    assertPrettyJsonEquals(expected, actual);
}
Also used : Scanner(java.util.Scanner) File(java.io.File) Test(org.junit.Test)

Example 15 with Scanner

use of java.util.Scanner in project cucumber-jvm by cucumber.

the class JUnitFormatterTest method should_format_scenario_outlines_with_the_junit_runner.

@Test
public void should_format_scenario_outlines_with_the_junit_runner() throws Exception {
    final File report = File.createTempFile("cucumber-jvm-junit", ".xml");
    final JUnitFormatter junitFormatter = createJUnitFormatter(report);
    // The JUnit runner will not call scenarioOutline() and examples() before executing the examples scenarios
    junitFormatter.uri(uri());
    junitFormatter.feature(feature("feature name"));
    junitFormatter.scenario(scenario("Scenario Outline", "outline name"));
    junitFormatter.step(step("keyword ", "step name \"arg1\""));
    junitFormatter.match(match());
    junitFormatter.result(result("passed"));
    junitFormatter.scenario(scenario("Scenario Outline", "outline name"));
    junitFormatter.step(step("keyword ", "step name \"arg2\""));
    junitFormatter.match(match());
    junitFormatter.result(result("passed"));
    junitFormatter.eof();
    junitFormatter.done();
    junitFormatter.close();
    String actual = new Scanner(new FileInputStream(report), "UTF-8").useDelimiter("\\A").next();
    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<testsuite failures=\"0\" tests=\"2\" name=\"cucumber.runtime.formatter.JUnitFormatter\" skipped=\"0\" time=\"0\">\n" + "    <testcase classname=\"feature name\" name=\"outline name\" time=\"0\">\n" + "        <system-out><![CDATA[" + "keyword step name \"arg1\"....................................................passed\n" + "]]></system-out>\n" + "    </testcase>\n" + "    <testcase classname=\"feature name\" name=\"outline name 2\" time=\"0\">\n" + "        <system-out><![CDATA[" + "keyword step name \"arg2\"....................................................passed\n" + "]]></system-out>\n" + "    </testcase>\n" + "</testsuite>\n";
    assertXmlEqual(expected, actual);
}
Also used : Scanner(java.util.Scanner) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Scanner (java.util.Scanner)567 File (java.io.File)97 IOException (java.io.IOException)63 NoSuchElementException (java.util.NoSuchElementException)59 ArrayList (java.util.ArrayList)56 Test (org.junit.Test)53 InputStream (java.io.InputStream)51 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)49 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)49 Response (com.github.scribejava.core.model.Response)49 InputMismatchException (java.util.InputMismatchException)47 FileNotFoundException (java.io.FileNotFoundException)42 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)31 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)29 FileInputStream (java.io.FileInputStream)27 HashMap (java.util.HashMap)26 Locale (java.util.Locale)23 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)18 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)18 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)18