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) && !method.equals(HttpMethods.HEAD))
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  
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(deScript(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 }