All Implemented Interfaces:
Serializable, Servlet, ServletConfig

public class ErrorServletextends HttpServlet
Common servlet for errors This is intended for webapps and local plugins. It is not appropriate for eepsites or remotely-accessible plugins, as it uses local console resources. See http://www.eclipse.org/jetty/documentation/current/custom-error-pages.html for how to add to web.xml or see examples in bundled webapps. Init parameters:
  • CSSPath - absolute URL of CSS to reference, starting with /, defaults to console theme
  • name - webapp or plugin name, will be translated with bundle, e.g. SusiMail
  • bundle - package of translation bundle, e.g. net.i2p.susimail.web.messages
Most strings are translated with the console bundle, as we're using the same strings as in error.jsp and error500.jsp. Supported error codes: 403, 404, and 500+. Others must be added here.
See Also:
  • Field Details

  • Constructor Details

    • ErrorServlet

      public ErrorServlet()
      Servlet for rendering error pages in webapps and plugins.
  • Method Details

    • init

      public void init() throws ServletException
      Load CSS path, webapp name, and bundle from init parameters.
      Overrides:
      init in class GenericServlet
      Throws:
      ServletException
    • doGet

      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
      Handle GET request and render error page HTML.
      Overrides:
      doGet in class HttpServlet
      Throws:
      ServletException
      IOException
    • doPost

      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
      Needed if the errored page was a POST
      Overrides:
      doPost in class HttpServlet
      Throws:
      ServletException
      IOException
    • outputMessage

      protected void outputMessage(PrintWriter out, int errorCode, String errorMsg, String errorURI, Throwable errorCause)
      Override for specific cases. This supports 403, 404, and 500+. Output HTML that goes inside the div.
      Parameters:
      out - where the HTML is written
      errorCode - e.g. 404
      errorMsg - non-null, may be empty, already HTML-escaped
      errorURI - non-null, may be empty, already HTML-escaped
      errorCause - may be null
    • printStylesheet

      private void printStylesheet(PrintWriter out, String themePath)
      Links the theme's stylesheet into the error page.
      Parameters:
      out - where the page is written
      themePath - path of the active theme
    • print404Error

      private void print404Error(PrintWriter out, String errorURI)
      Writes the not-found error page.
      Parameters:
      out - where the page is written
      errorURI - the address that was not found
    • printInternalError

      private void printInternalError(PrintWriter out, int errorCode, String errorMsg, String errorURI, Throwable errorCause)
      Writes the internal error page.
      Parameters:
      out - where the page is written
      errorCode - the HTTP status to show
      errorMsg - the message to show
      errorURI - the address that failed
      errorCause - the exception, may be null
    • printUnsupportedError

      private void printUnsupportedError(PrintWriter out, int errorCode)
      Writes the method-not-supported page.
      Parameters:
      out - where the page is written
      errorCode - the HTTP status to show
    • getSanitizedStackTrace

      private String getSanitizedStackTrace(Throwable errorCause)
      Formats a stack trace with the paths stripped.
      Parameters:
      errorCause - the exception to format
      Returns:
      the trace to show, null if there is none
    • printSystemInfo

      private void printSystemInfo(PrintWriter out)
      Writes the router version block of the error page.
      Parameters:
      out - where the page is written
    • _w

      protected String _w(String s)
      Translate a string using the webapp bundle.
      Parameters:
      s - the string to translate
      Returns:
      the translated string
    • _t

      protected String _t(String s)
      Translate a string using the console bundle.
      Parameters:
      s - the string to translate
      Returns:
      the translated string
    • _t

      protected String _t(String s, Object o)
      Translate a string using the console bundle.
      Parameters:
      s - the string to translate
      o - argument substituted into the translation
      Returns:
      the translated string
    • _t

      protected String _t(String s, Object o, Object o2)
      Translate a string using the console bundle.
      Parameters:
      s - the string to translate
      o - argument substituted into the translation
      o2 - second argument substituted into the translation
      Returns:
      the translated string