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
}
}
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();
}
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!");
}
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);
}
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);
}
Aggregations