View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-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  
14  package org.eclipse.jetty.servlet;
15  
16  import java.io.IOException;
17  import java.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.servlet.ServletContext;
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.http.HttpMethods;
28  import org.eclipse.jetty.server.Dispatcher;
29  import org.eclipse.jetty.server.HttpConnection;
30  import org.eclipse.jetty.server.Request;
31  import org.eclipse.jetty.server.handler.ContextHandler;
32  import org.eclipse.jetty.server.handler.ErrorHandler;
33  import org.eclipse.jetty.util.TypeUtil;
34  import org.eclipse.jetty.util.log.Log;
35  
36  /** Error Page Error Handler
37   * 
38   * An ErrorHandler that maps exceptions and status codes to URIs for dispatch using
39   * the internal ERROR style of dispatch.
40   * 
41   *
42   */
43  public class ErrorPageErrorHandler extends ErrorHandler
44  {
45      public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
46      
47      protected ServletContext _servletContext;
48      protected Map _errorPages; // code or exception to URL
49      protected List _errorPageList; // list of ErrorCode by range 
50  
51      /* ------------------------------------------------------------ */
52      /**
53       * @param context
54       */
55      public ErrorPageErrorHandler()
56      {}
57  
58      /* ------------------------------------------------------------ */
59      /* 
60       * @see org.eclipse.jetty.server.handler.ErrorHandler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
61       */
62      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
63      {
64          String method = request.getMethod();
65          if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST))
66          {
67              HttpConnection.getCurrentConnection().getRequest().setHandled(true);
68              return;
69          }
70          if (_errorPages!=null)
71          {
72              String error_page= null;
73              Class exClass= (Class)request.getAttribute(Dispatcher.ERROR_EXCEPTION_TYPE);
74              
75              if (ServletException.class.equals(exClass))
76              {
77                  error_page= (String)_errorPages.get(exClass.getName());
78                  if (error_page == null)
79                  {
80                      Throwable th= (Throwable)request.getAttribute(Dispatcher.ERROR_EXCEPTION);
81                      while (th instanceof ServletException)
82                          th= ((ServletException)th).getRootCause();
83                      if (th != null)
84                          exClass= th.getClass();
85                  }
86              }
87              
88              while (error_page == null && exClass != null )
89              {
90                  error_page= (String)_errorPages.get(exClass.getName());
91                  exClass= exClass.getSuperclass();
92              }
93              
94              if (error_page == null)
95              {
96                  // look for an exact code match
97                  Integer code=(Integer)request.getAttribute(Dispatcher.ERROR_STATUS_CODE);
98                  if (code!=null)
99                  {
100                     error_page= (String)_errorPages.get(TypeUtil.toString(code.intValue()));
101 
102                     // if still not found
103                     if ((error_page == null) && (_errorPageList != null))
104                     {
105                         // look for an error code range match.
106                         for (int i = 0; i < _errorPageList.size(); i++)
107                         {
108                             ErrorCodeRange errCode = (ErrorCodeRange) _errorPageList.get(i);
109                             if (errCode.isInRange(code.intValue()))
110                             {
111                                 error_page = errCode.getUri();
112                                 break;
113                             }
114                         }
115                     }
116                 }
117             }
118             
119             if (error_page!=null)
120             {
121                 String old_error_page=(String)request.getAttribute(ERROR_PAGE);
122                 if (old_error_page==null || !old_error_page.equals(error_page))
123                 {
124                     request.setAttribute(ERROR_PAGE, error_page);
125                     
126                     Dispatcher dispatcher = (Dispatcher) _servletContext.getRequestDispatcher(error_page);
127                     try
128                     {
129                         if(dispatcher!=null)
130                         {    
131                             dispatcher.error(request, response);
132                             return;
133                         }
134                         else
135                         {
136                             Log.warn("No error page "+error_page);
137                         }
138                     }
139                     catch (ServletException e)
140                     {
141                         Log.warn(Log.EXCEPTION, e);
142                         return;
143                     }
144                 }
145             }
146         }
147         
148         super.handle(target, baseRequest, request, response);
149     }
150 
151     /* ------------------------------------------------------------ */
152     /**
153      * @return Returns the errorPages.
154      */
155     public Map getErrorPages()
156     {
157         return _errorPages;
158     }
159     
160     /* ------------------------------------------------------------ */
161     /**
162      * @param errorPages The errorPages to set. A map of Exception class name  or error code as a string to URI string
163      */
164     public void setErrorPages(Map errorPages)
165     {
166         _errorPages = errorPages;
167     }
168 
169     /* ------------------------------------------------------------ */
170     /** Add Error Page mapping for an exception class
171      * This method is called as a result of an exception-type element in a web.xml file
172      * or may be called directly
173      * @param code The class (or superclass) of the matching exceptions
174      * @param uri The URI of the error page.
175      */
176     public void addErrorPage(Class exception,String uri)
177     {
178         if (_errorPages==null)
179             _errorPages=new HashMap();
180         _errorPages.put(exception.getName(),uri);
181     }
182     
183     /* ------------------------------------------------------------ */
184     /** Add Error Page mapping for a status code.
185      * This method is called as a result of an error-code element in a web.xml file
186      * or may be called directly
187      * @param code The HTTP status code to match
188      * @param uri The URI of the error page.
189      */
190     public void addErrorPage(int code,String uri)
191     {
192         if (_errorPages==null)
193             _errorPages=new HashMap();
194         _errorPages.put(TypeUtil.toString(code),uri);
195     }
196     
197     /* ------------------------------------------------------------ */
198     /** Add Error Page mapping for a status code range.
199      * This method is not available from web.xml and must be called
200      * directly.
201      * @param from The lowest matching status code
202      * @param to The highest matching status code
203      * @param uri The URI of the error page.
204      */
205     public void addErrorPage(int from, int to, String uri)
206     {
207         if (_errorPageList == null)
208         {
209             _errorPageList = new ArrayList();
210         }
211         _errorPageList.add(new ErrorCodeRange(from, to, uri));
212     }
213 
214     /* ------------------------------------------------------------ */
215     protected void doStart() throws Exception
216     {
217         super.doStart();
218         _servletContext=ContextHandler.getCurrentContext();
219     }
220 
221     /* ------------------------------------------------------------ */
222     protected void doStop() throws Exception
223     {
224         // TODO Auto-generated method stub
225         super.doStop();
226     }
227 
228     /* ------------------------------------------------------------ */
229     /* ------------------------------------------------------------ */
230     private class ErrorCodeRange
231     {
232         private int _from;
233         private int _to;
234         private String _uri;
235         
236         ErrorCodeRange(int from, int to, String uri)
237             throws IllegalArgumentException
238         {
239             if (from > to)
240                 throw new IllegalArgumentException("from>to");
241             
242             _from = from;
243             _to = to;
244             _uri = uri;
245         }
246         
247         boolean isInRange(int value)
248         {
249             if ((value >= _from) && (value <= _to))
250             {
251                 return true;
252             }
253             
254             return false;
255         }
256         
257         String getUri()
258         {
259             return _uri;
260         }
261         
262         public String toString()
263         {
264             return "from: " + _from + ",to: " + _to + ",uri: " + _uri;
265         }
266     }    
267 }