errorpagemanager.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 811 行 · 第 1/2 页

JAVA
811
字号
      out.println("<code><pre>");          /*	if (app != null && app.getServer().isClosed()) {	pw.println("Server is temporarily unavailable");	doStackTrace = false;	}	else      */          if (log.isLoggable(Level.FINE) || ! Alarm.isTest())	doStackTrace = true;      if (doStackTrace) {	out.println("<script language='javascript' type='text/javascript'>");	out.println("function show() { document.getElementById('trace').style.display = ''; }");	out.println("</script>");	out.print("<a style=\"text-decoration\" href=\"javascript:show();\">[show]</a> ");      }          if (compileException instanceof DisplayableException) {	DisplayableException dispExn = (DisplayableException) compileException;	dispExn.print(out);      }      else if (compileException != null)	out.println(escapeHtml(compileException.getMessage()));      else	out.println(escapeHtml(rootExn.toString()));      if (doStackTrace) {	out.println("<span id=\"trace\" style=\"display:none\">");	printStackTrace(out, lineMessage, e, rootExn, lineMap);	out.println("</span>");      }          /*       *if (doStackTrace || log.isLoggable(Level.FINE)) {       printStackTrace(out, lineMessage, e, rootExn, lineMap);       }      */      out.println("</pre></code>");      String version = null;      if (_app == null) {      }      else if (_app.getServer() != null	       && _app.getServer().getServerHeader() != null) {	version = _app.getServer().getServerHeader();      }      else if (CauchoSystem.isTesting()) {      }      else	version = com.caucho.Version.FULL_VERSION;          if (version != null) {	out.println("<p /><hr />");	out.println("<small>");		out.println(version);	  	out.println("</small>");      }            out.println("</body></html>");    }    else { // non-development mode      out.println("<html>");      out.println("<title>Server Error</title>");      out.println("<body>");      out.println("<h1>Server Error</h1>");      out.println("<p>The server is temporarily unavailable due to an");      out.println("internal error.  Please notify the system administrator");      out.println("of this problem.</p>");            out.println("<pre><code>");      out.println("Date: " + new QDate().formatISO8601(Alarm.getCurrentTime()));      if (Resin.getCurrent() != null)	out.println("Server: '" + Resin.getCurrent().getServerId() + "'");            out.println("</code></pre>");            out.println("</html>");      out.println("</body></html>");    }    String userAgent = request.getHeader("User-Agent");          if (userAgent != null && userAgent.indexOf("MSIE") >= 0) {      out.print(MSIE_PADDING);    }    out.close();  }  /**   * Sends an HTTP error to the browser.   *   * @param code the HTTP error code   * @param value a string message   */  public void sendError(CauchoRequest request,                        CauchoResponse response,                        int code, String message)    throws IOException  {    response.resetBuffer();        /* XXX: if we've already got an error, won't this just mask it?    if (responseStream.isCommitted())      throw new IllegalStateException("response can't sendError() after commit");    */    response.setStatus(code, message);        try {      if (handleErrorStatus(request, response, code, message)	  || code == HttpServletResponse.SC_NOT_MODIFIED)        return;      response.setContentType("text/html");      PrintWriter out = response.getWriter();      out.println("<html>");      if (! response.isCommitted()) {        out.print("<head><title>");        out.print(code);        out.print(" ");        out.print(message);        out.println("</title></head>");      }      out.println("<body>");      out.print("<h1>");      out.print(code);      out.print(" ");      out.print(message);      out.println("</h1>");      if (code == HttpServletResponse.SC_NOT_FOUND) {        out.println(L.l("{0} was not found on this server.",                        escapeHtml(request.getPageURI())));      }      String version = null;      if (_app == null) {      }      else if (_app.getServer() != null	       && _app.getServer().getServerHeader() != null) {	version = _app.getServer().getServerHeader();      }      else if (CauchoSystem.isTesting()) {      }      else	version = com.caucho.Version.FULL_VERSION;          if (version != null) {	out.println("<p /><hr />");	out.println("<small>");		out.println(version);	  	out.println("</small>");      }      out.println("</body></html>");      String userAgent = request.getHeader("User-Agent");            if (userAgent != null && userAgent.indexOf("MSIE") >= 0) {	out.write(MSIE_PADDING, 0, MSIE_PADDING.length);      }    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);    }  }    /**   * Handles an error status code.   *   * @return true if we've forwarded to an error page.   */  private boolean handleErrorStatus(CauchoRequest request,                                    CauchoResponse response,                                    int code, String message)    throws ServletException, IOException  {    if (code == HttpServletResponse.SC_OK ||        code == HttpServletResponse.SC_MOVED_TEMPORARILY ||        code == HttpServletResponse.SC_NOT_MODIFIED)      return false;    if (request.getRequestDepth(0) > 16)      return false;    else if (request.getAttribute(AbstractHttpRequest.ERROR_URI) != null)      return false;    response.killCache();        String location = getErrorPage(code);          if (location == null)      location = _defaultLocation;    if (location == null && _parent != null)      return _parent.handleErrorStatus(request, response, code, message);    if (_app == null && _appContainer == null)      return false;        if (location != null && ! location.equals(request.getRequestURI())) {      request.setAttribute(AbstractHttpRequest.STATUS_CODE,                            new Integer(code));      request.setAttribute(AbstractHttpRequest.MESSAGE,                           message);      request.setAttribute(AbstractHttpRequest.ERROR_URI,                           request.getRequestURI());      if (request instanceof AbstractHttpRequest)        request.setAttribute(AbstractHttpRequest.SERVLET_NAME,                             ((AbstractHttpRequest) request).getServletName());      try {        RequestDispatcher disp = null;        // can't use filters because of error pages due to filters        // or security.	if (_app != null)	  disp = _app.getRequestDispatcher(location);	else if (_appContainer != null)	  disp = _appContainer.getRequestDispatcher(location);        //disp.forward(request, this, "GET", false);	if (disp != null)	  ((RequestDispatcherImpl) disp).error(request, response);	else	  return false;      } catch (Throwable e) {        sendServletError(e, request, response);      }                return true;    }    return false;  }  /**   * Returns the URL of an error page for the given exception.   */  String getErrorPage(Throwable e)  {    return getErrorPage(e, Throwable.class);  }    /**   * Returns the URL of an error page for the given exception.   */  String getErrorPage(Throwable e, Class limit)  {    Class cl = e.getClass();    for (; cl != null; cl = cl.getSuperclass()) {      String location = (String) _errorPageMap.get(cl.getName());      if (location != null)        return location;      if (cl == limit)        break;    }        for (cl = e.getClass(); cl != null; cl = cl.getSuperclass()) {      String name = cl.getName();      int p = name.lastIndexOf('.');      if (p > 0) {        name = name.substring(p + 1);        String location =  (String) _errorPageMap.get(name);        if (location != null)          return location;      }            if (cl == limit)        break;    }    return null;  }    /**   * Returns the URL of an error page for the given exception.   */  String getErrorPage(int code)  {    Integer key = new Integer(code);    String location = (String) _errorPageMap.get(key);    if (location != null)      return location;    return (String) _errorPageMap.get(new Integer(0));  }  /**   * Escapes HTML symbols in a stack trace.   */  private void printStackTrace(PrintWriter out,			       String lineMessage,			       Throwable e,			       Throwable rootExn,                               LineMap lineMap)  {    CharArrayWriter writer = new CharArrayWriter();    PrintWriter pw = new PrintWriter(writer);    if (lineMessage != null)      pw.println(lineMessage);    if (lineMap != null)      lineMap.printStackTrace(e, pw);    else      ScriptStackTrace.printStackTrace(e, pw);    pw.close();    char []array = writer.toCharArray();    out.print(escapeHtml(new String(array)));  }  /**   * Escapes special symbols in a string.  For example '<' becomes '&lt;'   */  private String escapeHtml(String s)  {    if (s == null)      return null;    CharBuffer cb = new CharBuffer();    int lineCharacter = 0;    boolean startsWithSpace = false;        for (int i = 0; i < s.length(); i++) {      char ch = s.charAt(i);      lineCharacter++;            if (ch == '<')	cb.append("&lt;");      else if (ch == '&')	cb.append("&amp;");      /*      else if (ch == '%')	cb.append("%25");      */      else if (ch == '\n' || ch == '\r') {        lineCharacter = 0;	cb.append(ch);        startsWithSpace = false;      }      else if (lineCharacter > 70 && ch == ' ' && ! startsWithSpace) {        lineCharacter = 0;        cb.append('\n');        for (; i + 1 < s.length() && s.charAt(i + 1) == ' '; i++) {        }      }      else if (lineCharacter == 1 && (ch == ' ' || ch == '\t')) {        cb.append((char) ch);        startsWithSpace = true;      }      else	cb.append(ch);    }    return cb.toString();  }  public String toString()  {    return getClass().getSimpleName() + "[" + _app + "]";  }  static {    MSIE_PADDING = ("\n\n\n\n" +                    "<!--\n" +                    "   - Unfortunately, Microsoft has added a clever new\n" +                    "   - \"feature\" to Internet Explorer.  If the text in\n" +                    "   - an error's message is \"too small\", specifically\n" +                    "   - less than 512 bytes, Internet Explorer returns\n" +                    "   - its own error message.  Yes, you can turn that\n" +                    "   - off, but *surprise* it's pretty tricky to find\n" +                    "   - buried as a switch called \"smart error\n" +                    "   - messages\"  That means, of course, that many of\n" +                    "   - Resin's error messages are censored by default.\n" +                    "   - And, of course, you'll be shocked to learn that\n" +                    "   - IIS always returns error messages that are long\n" +                    "   - enough to make Internet Explorer happy.  The\n" +                    "   - workaround is pretty simple: pad the error\n" +                    "   - message with a big comment to push it over the\n" +                    "   - five hundred and twelve byte minimum.  Of course,\n" +                    "   - that's exactly what you're reading right now.\n" +                    "   -->\n").toCharArray();  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?