View Javadoc

    1   // ========================================================================
    2   // Copyright 1999-2005 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14  package org.mortbay.jetty.handler;
    15  import java.io.IOException;
    16  import java.io.PrintWriter;
    17  import java.io.StringWriter;
    18  import java.io.Writer;
    19
    20  import javax.servlet.http.HttpServletRequest;
    21  import javax.servlet.http.HttpServletResponse;
    22
    23  import org.mortbay.jetty.HttpConnection;
    24  import org.mortbay.jetty.HttpGenerator;
    25  import org.mortbay.jetty.HttpHeaders;
    26  import org.mortbay.jetty.HttpMethods;
    27  import org.mortbay.jetty.MimeTypes;
    28  import org.mortbay.util.ByteArrayISO8859Writer;
    29  import org.mortbay.util.StringUtil;
    30
    31
    32  /* ------------------------------------------------------------ */
    33  /** Handler for Error pages
    34   * A handler that is registered at the org.mortbay.http.ErrorHandler
    35   * context attributed and called by the HttpResponse.sendError method to write a
    36   * error page.
    37   *
    38   * @author Greg Wilkins (gregw)
    39   */
    40  public class ErrorHandler extends AbstractHandler
    41  {
    42      boolean _showStacks=true;
    43
    44      /* ------------------------------------------------------------ */
    45      /*
    46       * @see org.mortbay.jetty.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
    47       */
    48      public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException
    49      {
    50          HttpConnection connection = HttpConnection.getCurrentConnection();
    51          connection.getRequest().setHandled(true);
    52          String method = request.getMethod();
    53          if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST))
    54              return;
    55          response.setContentType(MimeTypes.TEXT_HTML_8859_1);
    56          response.setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
    57          ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
    58          handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason());
    59          writer.flush();
    60          response.setContentLength(writer.size());
    61          writer.writeTo(response.getOutputStream());
    62          writer.destroy();
    63      }
    64
    65      /* ------------------------------------------------------------ */
    66      protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
    67          throws IOException
    68      {
    69          writeErrorPage(request, writer, code, message, _showStacks);
    70      }
    71
    72      /* ------------------------------------------------------------ */
    73      protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
    74          throws IOException
    75      {
    76          if (message == null)
    77              message=HttpGenerator.getReason(code);
    78          else
    79          {
    80              message= StringUtil.replace(message, "&", "&amp;");
    81              message= StringUtil.replace(message, "<", "&lt;");
    82              message= StringUtil.replace(message, ">", "&gt;");
    83          }
    84
    85          writer.write("<html>\n<head>\n");
    86          writeErrorPageHead(request,writer,code,message);
    87          writer.write("</head>\n<body>");
    88          writeErrorPageBody(request,writer,code,message,showStacks);
    89          writer.write("\n</body>\n</html>\n");
    90      }
    91
    92      /* ------------------------------------------------------------ */
    93      protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
    94          throws IOException
    95      {
    96          writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n");
    97          writer.write("<title>Error ");
    98          writer.write(Integer.toString(code));
    99          writer.write(' ');
    100         if (message!=null)
    101             writer.write(message);
    102         writer.write("</title>\n");
    103     }
    104
    105     /* ------------------------------------------------------------ */
    106     protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
    107         throws IOException
    108     {
    109         String uri= request.getRequestURI();
    110         if (uri!=null)
    111         {
    112             uri= StringUtil.replace(uri, "&", "&amp;");
    113             uri= StringUtil.replace(uri, "<", "&lt;");
    114             uri= StringUtil.replace(uri, ">", "&gt;");
    115         }
    116
    117         writeErrorPageMessage(request,writer,code,message,uri);
    118         if (showStacks)
    119             writeErrorPageStacks(request,writer);
    120         writer.write("<p><i><small><a href=\"http://jetty.mortbay.org/\">Powered by Jetty://</a></small></i></p>");
    121         for (int i= 0; i < 20; i++)
    122             writer.write("<br/>                                                \n");
    123     }
    124
    125     /* ------------------------------------------------------------ */
    126     protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
    127     throws IOException
    128     {
    129         writer.write("<h2>HTTP ERROR: ");
    130         writer.write(Integer.toString(code));
    131         writer.write("</h2><pre>");
    132         writer.write(message);
    133         writer.write("</pre>\n<p>RequestURI=");
    134         writer.write(uri);
    135         writer.write("</p>");
    136     }
    137
    138     /* ------------------------------------------------------------ */
    139     protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
    140         throws IOException
    141     {
    142         Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
    143         while(th!=null)
    144         {
    145             writer.write("<h3>Caused by:</h3><pre>");
    146             StringWriter sw = new StringWriter();
    147             PrintWriter pw = new PrintWriter(sw);
    148             th.printStackTrace(pw);
    149             pw.flush();
    150             writer.write(sw.getBuffer().toString());
    151             writer.write("</pre>\n");
    152
    153             th =th.getCause();
    154         }
    155     }
    156
    157
    158     /* ------------------------------------------------------------ */
    159     /**
    160      * @return True if stack traces are shown in the error pages
    161      */
    162     public boolean isShowStacks()
    163     {
    164         return _showStacks;
    165     }
    166
    167     /* ------------------------------------------------------------ */
    168     /**
    169      * @param showStacks True if stack traces are shown in the error pages
    170      */
    171     public void setShowStacks(boolean showStacks)
    172     {
    173         _showStacks = showStacks;
    174     }
    175
    176 }