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.server.Request;
28  import org.eclipse.jetty.util.ByteArrayISO8859Writer;
29  import org.eclipse.jetty.util.StringUtil;
30  
31  
32  /* ------------------------------------------------------------ */
33  /** Handler for Error pages
34   * A handler that is registered at the org.eclipse.http.ErrorHandler
35   * context attributed and called by the HttpResponse.sendError method to write a
36   * error page.
37   * 
38   * 
39   */
40  public class ErrorHandler extends AbstractHandler
41  {
42      boolean _showStacks=true;
43      
44      /* ------------------------------------------------------------ */
45      /* 
46       * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
47       */
48      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) 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=HttpStatus.getCode(code).getMessage();
78  
79          writer.write("<html>\n<head>\n");
80          writeErrorPageHead(request,writer,code,message);
81          writer.write("</head>\n<body>");
82          writeErrorPageBody(request,writer,code,message,showStacks);
83          writer.write("\n</body>\n</html>\n");
84      }
85  
86      /* ------------------------------------------------------------ */
87      protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
88          throws IOException
89      {
90          writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n");
91          writer.write("<title>Error ");
92          writer.write(Integer.toString(code));
93          writer.write(' ');
94          if (message!=null)
95              writer.write(deScript(message));
96          writer.write("</title>\n");    
97      }
98  
99      /* ------------------------------------------------------------ */
100     protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
101         throws IOException
102     {
103         String uri= request.getRequestURI();
104         
105         writeErrorPageMessage(request,writer,code,message,uri);
106         if (showStacks)
107             writeErrorPageStacks(request,writer);
108         writer.write("<hr /><i><small>Powered by Jetty://</small></i>");
109         for (int i= 0; i < 20; i++)
110             writer.write("<br/>                                                \n");
111     }
112 
113     /* ------------------------------------------------------------ */
114     protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
115     throws IOException
116     {
117         writer.write("<h2>HTTP ERROR ");
118         writer.write(Integer.toString(code));
119         writer.write("</h2>\n<p>Problem accessing ");
120         writer.write(uri);
121         writer.write(". Reason:\n<pre>    ");
122         writer.write(deScript(message));
123         writer.write("</pre></p>");
124     }
125 
126     /* ------------------------------------------------------------ */
127     protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
128         throws IOException
129     {
130         Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
131         while(th!=null)
132         {
133             writer.write("<h3>Caused by:</h3><pre>");
134             StringWriter sw = new StringWriter();
135             PrintWriter pw = new PrintWriter(sw);
136             th.printStackTrace(pw);
137             pw.flush();
138             writer.write(deScript(sw.getBuffer().toString()));
139             writer.write("</pre>\n");
140 
141             th =th.getCause();
142         }
143     }
144         
145 
146     /* ------------------------------------------------------------ */
147     /**
148      * @return True if stack traces are shown in the error pages
149      */
150     public boolean isShowStacks()
151     {
152         return _showStacks;
153     }
154 
155     /* ------------------------------------------------------------ */
156     /**
157      * @param showStacks True if stack traces are shown in the error pages
158      */
159     public void setShowStacks(boolean showStacks)
160     {
161         _showStacks = showStacks;
162     }
163 
164     /* ------------------------------------------------------------ */
165     protected String deScript(String string)
166     {
167         if (string==null)
168             return null;
169         string=StringUtil.replace(string, "&", "&amp;");
170         string=StringUtil.replace(string, "<", "&lt;");
171         string=StringUtil.replace(string, ">", "&gt;");
172         return string;
173     }
174 }