View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.handler;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  import java.io.Writer;
25  
26  import javax.activation.MimeType;
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.eclipse.jetty.http.HttpHeader;
32  import org.eclipse.jetty.http.HttpMethod;
33  import org.eclipse.jetty.http.HttpStatus;
34  import org.eclipse.jetty.http.MimeTypes;
35  import org.eclipse.jetty.server.Dispatcher;
36  import org.eclipse.jetty.server.Request;
37  import org.eclipse.jetty.server.Response;
38  import org.eclipse.jetty.util.ByteArrayISO8859Writer;
39  import org.eclipse.jetty.util.log.Log;
40  import org.eclipse.jetty.util.log.Logger;
41  
42  /* ------------------------------------------------------------ */
43  /** Handler for Error pages
44   * An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
45   * {@link org.eclipse.jetty.server.Server#addBean(Object)}.
46   * It is called by the HttpResponse.sendError method to write a error page.
47   *
48   */
49  public class ErrorHandler extends AbstractHandler
50  {    
51      private static final Logger LOG = Log.getLogger(ErrorHandler.class);
52      public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
53      
54      boolean _showStacks=true;
55      boolean _showMessageInTitle=true;
56      String _cacheControl="must-revalidate,no-cache,no-store";
57  
58      /* ------------------------------------------------------------ */
59      /*
60       * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
61       */
62      @Override
63      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
64      {
65          String method = request.getMethod();
66          if (!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method))
67          {
68              baseRequest.setHandled(true);
69              return;
70          }
71          
72          if (this instanceof ErrorPageMapper)
73          {
74              String error_page=((ErrorPageMapper)this).getErrorPage(request);
75              if (error_page!=null && request.getServletContext()!=null)
76              {
77                  String old_error_page=(String)request.getAttribute(ERROR_PAGE);
78                  if (old_error_page==null || !old_error_page.equals(error_page))
79                  {
80                      request.setAttribute(ERROR_PAGE, error_page);
81  
82                      Dispatcher dispatcher = (Dispatcher) request.getServletContext().getRequestDispatcher(error_page);
83                      try
84                      {
85                          if(dispatcher!=null)
86                          {
87                              dispatcher.error(request, response);
88                              return;
89                          }
90                          LOG.warn("No error page "+error_page);
91                      }
92                      catch (ServletException e)
93                      {
94                          LOG.warn(Log.EXCEPTION, e);
95                          return;
96                      }
97                  }
98              }
99          }
100         
101         baseRequest.setHandled(true);
102         response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());    
103         if (_cacheControl!=null)
104             response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl);
105         ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
106         String reason=(response instanceof Response)?((Response)response).getReason():null;
107         handleErrorPage(request, writer, response.getStatus(), reason);
108         writer.flush();
109         response.setContentLength(writer.size());
110         writer.writeTo(response.getOutputStream());
111         writer.destroy();
112     }
113 
114     /* ------------------------------------------------------------ */
115     protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
116         throws IOException
117     {
118         writeErrorPage(request, writer, code, message, _showStacks);
119     }
120 
121     /* ------------------------------------------------------------ */
122     protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
123         throws IOException
124     {
125         if (message == null)
126             message=HttpStatus.getMessage(code);
127 
128         writer.write("<html>\n<head>\n");
129         writeErrorPageHead(request,writer,code,message);
130         writer.write("</head>\n<body>");
131         writeErrorPageBody(request,writer,code,message,showStacks);
132         writer.write("\n</body>\n</html>\n");
133     }
134 
135     /* ------------------------------------------------------------ */
136     protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
137         throws IOException
138         {
139         writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n");
140         writer.write("<title>Error ");
141         writer.write(Integer.toString(code));
142 
143         if (_showMessageInTitle)
144         {
145             writer.write(' ');
146             write(writer,message);
147         }
148         writer.write("</title>\n");
149     }
150 
151     /* ------------------------------------------------------------ */
152     protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
153         throws IOException
154     {
155         String uri= request.getRequestURI();
156 
157         writeErrorPageMessage(request,writer,code,message,uri);
158         if (showStacks)
159             writeErrorPageStacks(request,writer);
160         writer.write("<hr><i><small>Powered by Jetty://</small></i><hr/>\n");
161     }
162 
163     /* ------------------------------------------------------------ */
164     protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
165     throws IOException
166     {
167         writer.write("<h2>HTTP ERROR ");
168         writer.write(Integer.toString(code));
169         writer.write("</h2>\n<p>Problem accessing ");
170         write(writer,uri);
171         writer.write(". Reason:\n<pre>    ");
172         write(writer,message);
173         writer.write("</pre></p>");
174     }
175 
176     /* ------------------------------------------------------------ */
177     protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
178         throws IOException
179     {
180         Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
181         while(th!=null)
182         {
183             writer.write("<h3>Caused by:</h3><pre>");
184             StringWriter sw = new StringWriter();
185             PrintWriter pw = new PrintWriter(sw);
186             th.printStackTrace(pw);
187             pw.flush();
188             write(writer,sw.getBuffer().toString());
189             writer.write("</pre>\n");
190 
191             th =th.getCause();
192         }
193     }
194 
195 
196     /* ------------------------------------------------------------ */
197     /** Get the cacheControl.
198      * @return the cacheControl header to set on error responses.
199      */
200     public String getCacheControl()
201     {
202         return _cacheControl;
203     }
204 
205     /* ------------------------------------------------------------ */
206     /** Set the cacheControl.
207      * @param cacheControl the cacheControl header to set on error responses.
208      */
209     public void setCacheControl(String cacheControl)
210     {
211         _cacheControl = cacheControl;
212     }
213 
214     /* ------------------------------------------------------------ */
215     /**
216      * @return True if stack traces are shown in the error pages
217      */
218     public boolean isShowStacks()
219     {
220         return _showStacks;
221     }
222 
223     /* ------------------------------------------------------------ */
224     /**
225      * @param showStacks True if stack traces are shown in the error pages
226      */
227     public void setShowStacks(boolean showStacks)
228     {
229         _showStacks = showStacks;
230     }
231 
232     /* ------------------------------------------------------------ */
233     /**
234      * @param showMessageInTitle if true, the error message appears in page title
235      */
236     public void setShowMessageInTitle(boolean showMessageInTitle)
237     {
238         _showMessageInTitle = showMessageInTitle;
239     }
240 
241 
242     /* ------------------------------------------------------------ */
243     public boolean getShowMessageInTitle()
244     {
245         return _showMessageInTitle;
246     }
247 
248     /* ------------------------------------------------------------ */
249     protected void write(Writer writer,String string)
250         throws IOException
251     {
252         if (string==null)
253             return;
254 
255         for (int i=0;i<string.length();i++)
256         {
257             char c=string.charAt(i);
258 
259             switch(c)
260             {
261                 case '&' :
262                     writer.write("&amp;");
263                     break;
264                 case '<' :
265                     writer.write("&lt;");
266                     break;
267                 case '>' :
268                     writer.write("&gt;");
269                     break;
270 
271                 default:
272                     if (Character.isISOControl(c) && !Character.isWhitespace(c))
273                         writer.write('?');
274                     else
275                         writer.write(c);
276             }
277         }
278     }
279 
280     /* ------------------------------------------------------------ */
281     public interface ErrorPageMapper
282     {
283         String getErrorPage(HttpServletRequest request);
284     }
285 }