View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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  import java.nio.ByteBuffer;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.eclipse.jetty.http.HttpFields;
32  import org.eclipse.jetty.http.HttpHeader;
33  import org.eclipse.jetty.http.HttpMethod;
34  import org.eclipse.jetty.http.HttpStatus;
35  import org.eclipse.jetty.http.MimeTypes;
36  import org.eclipse.jetty.server.Dispatcher;
37  import org.eclipse.jetty.server.Request;
38  import org.eclipse.jetty.server.Response;
39  import org.eclipse.jetty.server.Server;
40  import org.eclipse.jetty.util.BufferUtil;
41  import org.eclipse.jetty.util.ByteArrayISO8859Writer;
42  import org.eclipse.jetty.util.Jetty;
43  import org.eclipse.jetty.util.StringUtil;
44  import org.eclipse.jetty.util.log.Log;
45  import org.eclipse.jetty.util.log.Logger;
46  
47  /* ------------------------------------------------------------ */
48  /** Handler for Error pages
49   * An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
50   * {@link org.eclipse.jetty.server.Server#addBean(Object)}.
51   * It is called by the HttpResponse.sendError method to write a error page via {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
52   * or via {@link #badMessageError(int, String, HttpFields)} for bad requests for which a dispatch cannot be done.
53   *
54   */
55  public class ErrorHandler extends AbstractHandler
56  {    
57      private static final Logger LOG = Log.getLogger(ErrorHandler.class);
58      public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
59      
60      boolean _showStacks=true;
61      boolean _showMessageInTitle=true;
62      String _cacheControl="must-revalidate,no-cache,no-store";
63  
64      /* ------------------------------------------------------------ */
65      /*
66       * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
67       */
68      @Override
69      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
70      {
71          String method = request.getMethod();
72          if (!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method))
73          {
74              baseRequest.setHandled(true);
75              return;
76          }
77          
78          if (this instanceof ErrorPageMapper)
79          {
80              String error_page=((ErrorPageMapper)this).getErrorPage(request);
81              if (error_page!=null && request.getServletContext()!=null)
82              {
83                  String old_error_page=(String)request.getAttribute(ERROR_PAGE);
84                  if (old_error_page==null || !old_error_page.equals(error_page))
85                  {
86                      request.setAttribute(ERROR_PAGE, error_page);
87  
88                      Dispatcher dispatcher = (Dispatcher) request.getServletContext().getRequestDispatcher(error_page);
89                      try
90                      {
91                          if(dispatcher!=null)
92                          {
93                              dispatcher.error(request, response);
94                              return;
95                          }
96                          LOG.warn("No error page "+error_page);
97                      }
98                      catch (ServletException e)
99                      {
100                         LOG.warn(Log.EXCEPTION, e);
101                         return;
102                     }
103                 }
104             }
105         }
106         
107         baseRequest.setHandled(true);
108         response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());    
109         if (_cacheControl!=null)
110             response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl);
111         ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
112         String reason=(response instanceof Response)?((Response)response).getReason():null;
113         handleErrorPage(request, writer, response.getStatus(), reason);
114         writer.flush();
115         response.setContentLength(writer.size());
116         writer.writeTo(response.getOutputStream());
117         writer.destroy();
118     }
119 
120     /* ------------------------------------------------------------ */
121     protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
122         throws IOException
123     {
124         writeErrorPage(request, writer, code, message, _showStacks);
125     }
126 
127     /* ------------------------------------------------------------ */
128     protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
129         throws IOException
130     {
131         if (message == null)
132             message=HttpStatus.getMessage(code);
133 
134         writer.write("<html>\n<head>\n");
135         writeErrorPageHead(request,writer,code,message);
136         writer.write("</head>\n<body>");
137         writeErrorPageBody(request,writer,code,message,showStacks);
138         writer.write("\n</body>\n</html>\n");
139     }
140 
141     /* ------------------------------------------------------------ */
142     protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
143         throws IOException
144         {
145         writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n");
146         writer.write("<title>Error ");
147         writer.write(Integer.toString(code));
148 
149         if (_showMessageInTitle)
150         {
151             writer.write(' ');
152             write(writer,message);
153         }
154         writer.write("</title>\n");
155     }
156 
157     /* ------------------------------------------------------------ */
158     protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
159         throws IOException
160     {
161         String uri= request.getRequestURI();
162 
163         writeErrorPageMessage(request,writer,code,message,uri);
164         if (showStacks)
165             writeErrorPageStacks(request,writer);
166 
167         Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
168             .writePoweredBy(writer,"<hr>","<hr/>\n");
169     }
170 
171     /* ------------------------------------------------------------ */
172     protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
173     throws IOException
174     {
175         writer.write("<h2>HTTP ERROR ");
176         writer.write(Integer.toString(code));
177         writer.write("</h2>\n<p>Problem accessing ");
178         write(writer,uri);
179         writer.write(". Reason:\n<pre>    ");
180         write(writer,message);
181         writer.write("</pre></p>");
182     }
183 
184     /* ------------------------------------------------------------ */
185     protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
186         throws IOException
187     {
188         Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
189         while(th!=null)
190         {
191             writer.write("<h3>Caused by:</h3><pre>");
192             StringWriter sw = new StringWriter();
193             PrintWriter pw = new PrintWriter(sw);
194             th.printStackTrace(pw);
195             pw.flush();
196             write(writer,sw.getBuffer().toString());
197             writer.write("</pre>\n");
198 
199             th =th.getCause();
200         }
201     }
202 
203     /* ------------------------------------------------------------ */
204     /** Bad Message Error body
205      * <p>Generate a error response body to be sent for a bad message.
206      * In this case there is something wrong with the request, so either
207      * a request cannot be built, or it is not safe to build a request.
208      * This method allows for a simple error page body to be returned 
209      * and some response headers to be set.
210      * @param status The error code that will be sent
211      * @param reason The reason for the error code (may be null)
212      * @param fields The header fields that will be sent with the response.
213      * @return The content as a ByteBuffer, or null for no body.
214      */
215     public ByteBuffer badMessageError(int status, String reason, HttpFields fields)
216     {
217         if (reason==null)
218             reason=HttpStatus.getMessage(status);
219         fields.put(HttpHeader.CONTENT_TYPE,MimeTypes.Type.TEXT_HTML_8859_1.asString());
220         return BufferUtil.toBuffer("<h1>Bad Message " + status + "</h1><pre>reason: " + reason + "</pre>");
221     }    
222     
223     /* ------------------------------------------------------------ */
224     /** Get the cacheControl.
225      * @return the cacheControl header to set on error responses.
226      */
227     public String getCacheControl()
228     {
229         return _cacheControl;
230     }
231 
232     /* ------------------------------------------------------------ */
233     /** Set the cacheControl.
234      * @param cacheControl the cacheControl header to set on error responses.
235      */
236     public void setCacheControl(String cacheControl)
237     {
238         _cacheControl = cacheControl;
239     }
240 
241     /* ------------------------------------------------------------ */
242     /**
243      * @return True if stack traces are shown in the error pages
244      */
245     public boolean isShowStacks()
246     {
247         return _showStacks;
248     }
249 
250     /* ------------------------------------------------------------ */
251     /**
252      * @param showStacks True if stack traces are shown in the error pages
253      */
254     public void setShowStacks(boolean showStacks)
255     {
256         _showStacks = showStacks;
257     }
258 
259     /* ------------------------------------------------------------ */
260     /**
261      * @param showMessageInTitle if true, the error message appears in page title
262      */
263     public void setShowMessageInTitle(boolean showMessageInTitle)
264     {
265         _showMessageInTitle = showMessageInTitle;
266     }
267 
268 
269     /* ------------------------------------------------------------ */
270     public boolean getShowMessageInTitle()
271     {
272         return _showMessageInTitle;
273     }
274 
275     /* ------------------------------------------------------------ */
276     protected void write(Writer writer,String string)
277         throws IOException
278     {
279         if (string==null)
280             return;
281 
282         writer.write(StringUtil.sanitizeXmlString(string));
283     }
284 
285     /* ------------------------------------------------------------ */
286     public interface ErrorPageMapper
287     {
288         String getErrorPage(HttpServletRequest request);
289     }
290 
291     /* ------------------------------------------------------------ */
292     public static ErrorHandler getErrorHandler(Server server, ContextHandler context)
293     {
294         ErrorHandler error_handler=null;
295         if (context!=null)
296             error_handler=context.getErrorHandler();
297         if (error_handler==null && server!=null)
298             error_handler = server.getBean(ErrorHandler.class);
299         return error_handler;
300     }
301 }