View Javadoc

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