Search in sources :

Example 56 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 57 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 58 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 59 with Scanner

use of java.util.Scanner in project hibernate-orm by hibernate.

the class TestConsole method start.

private void start() {
    Scanner scanner = new Scanner(System.in);
    PrintStream out = System.out;
    while (true) {
        out.println("-----------------------------------------------");
        out.println("1 - list persons             5 - list addresses");
        out.println("2 - list person history      6 - list addresses history");
        out.println("3 - new person               7 - new address");
        out.println("4 - modify person            8 - modify address");
        out.println("9 - get person at revision  10 - get address at revision");
        out.println("                             0 - end");
        try {
            int choice = scanner.nextInt();
            scanner.nextLine();
            entityManager.getTransaction().begin();
            StringBuilder sb;
            int personId;
            int addressId;
            int revision;
            switch(choice) {
                case 1:
                    sb = new StringBuilder();
                    printPersons(sb);
                    out.println(sb.toString());
                    break;
                case 2:
                    out.print("Person id: ");
                    personId = scanner.nextInt();
                    scanner.nextLine();
                    sb = new StringBuilder();
                    printPersonHistory(sb, personId);
                    out.println(sb.toString());
                    break;
                case 3:
                    Person p = readNewPerson(out, scanner);
                    entityManager.persist(p);
                    break;
                case 4:
                    out.print("Person id: ");
                    personId = scanner.nextInt();
                    scanner.nextLine();
                    readModifyPerson(out, scanner, personId);
                    break;
                case 5:
                    sb = new StringBuilder();
                    printAddresses(sb);
                    out.println(sb.toString());
                    break;
                case 6:
                    out.print("Address id: ");
                    addressId = scanner.nextInt();
                    scanner.nextLine();
                    sb = new StringBuilder();
                    printAddressHistory(sb, addressId);
                    out.println(sb.toString());
                    break;
                case 7:
                    Address a = readNewAddress(out, scanner);
                    entityManager.persist(a);
                    break;
                case 8:
                    out.print("Address id: ");
                    addressId = scanner.nextInt();
                    scanner.nextLine();
                    readModifyAddress(out, scanner, addressId);
                    break;
                case 9:
                    out.print("Person id: ");
                    personId = scanner.nextInt();
                    scanner.nextLine();
                    out.print("Revision number: ");
                    revision = scanner.nextInt();
                    scanner.nextLine();
                    if (revision <= 0) {
                        System.out.println("Revision must be greater then 0!");
                        continue;
                    }
                    sb = new StringBuilder();
                    printPersonAtRevision(sb, personId, revision);
                    out.println(sb.toString());
                    break;
                case 10:
                    out.print("Address id: ");
                    addressId = scanner.nextInt();
                    scanner.nextLine();
                    out.print("Revision number: ");
                    revision = scanner.nextInt();
                    scanner.nextLine();
                    if (revision <= 0) {
                        System.out.println("Revision must be greater then 0!");
                        continue;
                    }
                    sb = new StringBuilder();
                    printAddressAtRevision(sb, addressId, revision);
                    out.println(sb.toString());
                    break;
                case 0:
                    return;
            }
        } catch (InputMismatchException e) {
        // continuing
        } finally {
            entityManager.getTransaction().commit();
        }
    }
}
Also used : Scanner(java.util.Scanner) PrintStream(java.io.PrintStream) InputMismatchException(java.util.InputMismatchException)

Example 60 with Scanner

use of java.util.Scanner in project jmonkeyengine by jMonkeyEngine.

the class MTLLoader method load.

@SuppressWarnings("empty-statement")
public Object load(AssetInfo info) throws IOException {
    reset();
    this.key = info.getKey();
    this.assetManager = info.getManager();
    folderName = info.getKey().getFolder();
    matList = new MaterialList();
    InputStream in = null;
    try {
        in = info.openStream();
        scan = new Scanner(in);
        scan.useLocale(Locale.US);
        while (readLine()) ;
    } finally {
        if (in != null) {
            in.close();
        }
    }
    if (matName != null) {
        // still have a material in the vars
        createMaterial();
        resetMaterial();
    }
    MaterialList list = matList;
    return list;
}
Also used : Scanner(java.util.Scanner) InputStream(java.io.InputStream) MaterialList(com.jme3.material.MaterialList)

Aggregations

Scanner (java.util.Scanner)862 File (java.io.File)135 ArrayList (java.util.ArrayList)77 IOException (java.io.IOException)72 Test (org.junit.Test)70 NoSuchElementException (java.util.NoSuchElementException)59 InputStream (java.io.InputStream)54 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)49 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)49 Response (com.github.scribejava.core.model.Response)49 FileNotFoundException (java.io.FileNotFoundException)47 InputMismatchException (java.util.InputMismatchException)47 HashMap (java.util.HashMap)35 FileInputStream (java.io.FileInputStream)33 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)31 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)29 Locale (java.util.Locale)24 BigInteger (java.math.BigInteger)23 List (java.util.List)22 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)18