2 // ========================================================================
3 // Copyright (c) 1995-2016 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.servlet;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
30 import org.eclipse.jetty.server.Dispatcher;
31 import org.eclipse.jetty.server.handler.ContextHandler;
32 import org.eclipse.jetty.server.handler.ErrorHandler;
34 /* ------------------------------------------------------------ */
35 /** Error Page Error Handler
37 * An ErrorHandler that maps exceptions and status codes to URIs for dispatch using
38 * the internal ERROR style of dispatch.
41 public class ErrorPageErrorHandler extends ErrorHandler implements ErrorHandler.ErrorPageMapper
43 public final static String GLOBAL_ERROR_PAGE = "org.eclipse.jetty.server.error_page.global";
45 protected ServletContext _servletContext;
46 private final Map<String,String> _errorPages= new HashMap<String,String>(); // code or exception to URL
47 private final List<ErrorCodeRange> _errorPageList=new ArrayList<ErrorCodeRange>(); // list of ErrorCode by range
49 /* ------------------------------------------------------------ */
50 public ErrorPageErrorHandler()
53 /* ------------------------------------------------------------ */
55 public String getErrorPage(HttpServletRequest request)
57 String error_page= null;
59 Throwable th= (Throwable)request.getAttribute(Dispatcher.ERROR_EXCEPTION);
61 // Walk the cause hierarchy
62 while (error_page == null && th != null )
64 Class<?> exClass=th.getClass();
65 error_page= (String)_errorPages.get(exClass.getName());
67 // walk the inheritance hierarchy
68 while (error_page == null)
70 exClass= exClass.getSuperclass();
73 error_page= (String)_errorPages.get(exClass.getName());
76 th=(th instanceof ServletException)?((ServletException)th).getRootCause():null;
79 if (error_page == null)
81 // look for an exact code match
82 Integer code=(Integer)request.getAttribute(Dispatcher.ERROR_STATUS_CODE);
85 error_page= (String)_errorPages.get(Integer.toString(code));
88 if ((error_page == null) && (_errorPageList != null))
90 // look for an error code range match.
91 for (int i = 0; i < _errorPageList.size(); i++)
93 ErrorCodeRange errCode = (ErrorCodeRange) _errorPageList.get(i);
94 if (errCode.isInRange(code))
96 error_page = errCode.getUri();
104 //try servlet 3.x global error page
105 if (error_page == null)
106 error_page = _errorPages.get(GLOBAL_ERROR_PAGE);
112 /* ------------------------------------------------------------ */
114 * @return Returns the errorPages.
116 public Map<String,String> getErrorPages()
121 /* ------------------------------------------------------------ */
123 * @param errorPages The errorPages to set. A map of Exception class name or error code as a string to URI string
125 public void setErrorPages(Map<String,String> errorPages)
128 if (errorPages!=null)
129 _errorPages.putAll(errorPages);
132 /* ------------------------------------------------------------ */
133 /** Add Error Page mapping for an exception class
134 * This method is called as a result of an exception-type element in a web.xml file
135 * or may be called directly
136 * @param exception The exception
137 * @param uri The URI of the error page.
139 public void addErrorPage(Class<? extends Throwable> exception,String uri)
141 _errorPages.put(exception.getName(),uri);
144 /* ------------------------------------------------------------ */
145 /** Add Error Page mapping for an exception class
146 * This method is called as a result of an exception-type element in a web.xml file
147 * or may be called directly
148 * @param exceptionClassName The exception
149 * @param uri The URI of the error page.
151 public void addErrorPage(String exceptionClassName,String uri)
153 _errorPages.put(exceptionClassName,uri);
156 /* ------------------------------------------------------------ */
157 /** Add Error Page mapping for a status code.
158 * This method is called as a result of an error-code element in a web.xml file
159 * or may be called directly
160 * @param code The HTTP status code to match
161 * @param uri The URI of the error page.
163 public void addErrorPage(int code,String uri)
165 _errorPages.put(Integer.toString(code),uri);
168 /* ------------------------------------------------------------ */
169 /** Add Error Page mapping for a status code range.
170 * This method is not available from web.xml and must be called
172 * @param from The lowest matching status code
173 * @param to The highest matching status code
174 * @param uri The URI of the error page.
176 public void addErrorPage(int from, int to, String uri)
178 _errorPageList.add(new ErrorCodeRange(from, to, uri));
181 /* ------------------------------------------------------------ */
183 protected void doStart() throws Exception
186 _servletContext=ContextHandler.getCurrentContext();
189 /* ------------------------------------------------------------ */
190 /* ------------------------------------------------------------ */
191 private class ErrorCodeRange
197 ErrorCodeRange(int from, int to, String uri)
198 throws IllegalArgumentException
201 throw new IllegalArgumentException("from>to");
208 boolean isInRange(int value)
210 if ((value >= _from) && (value <= _to))
224 public String toString()
226 return "from: " + _from + ",to: " + _to + ",uri: " + _uri;