1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.jetty.servlet;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import javax.servlet.ServletContext;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.mortbay.jetty.handler.ContextHandler;
29 import org.mortbay.jetty.handler.ErrorHandler;
30 import org.mortbay.jetty.webapp.WebAppContext;
31 import org.mortbay.log.Log;
32 import org.mortbay.util.TypeUtil;
33
34
35
36
37
38
39
40
41 public class ErrorPageErrorHandler extends ErrorHandler
42 {
43 protected ServletContext _servletContext;
44 protected Map _errorPages;
45 protected List _errorPageList;
46
47
48
49
50
51 public ErrorPageErrorHandler()
52 {}
53
54
55
56
57
58 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException
59 {
60 if (_errorPages!=null)
61 {
62 String error_page= null;
63 Class exClass= (Class)request.getAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE);
64
65 if (ServletException.class.equals(exClass))
66 {
67 error_page= (String)_errorPages.get(exClass.getName());
68 if (error_page == null)
69 {
70 Throwable th= (Throwable)request.getAttribute(ServletHandler.__J_S_ERROR_EXCEPTION);
71 while (th instanceof ServletException)
72 th= ((ServletException)th).getRootCause();
73 if (th != null)
74 exClass= th.getClass();
75 }
76 }
77
78 while (error_page == null && exClass != null )
79 {
80 error_page= (String)_errorPages.get(exClass.getName());
81 exClass= exClass.getSuperclass();
82 }
83
84 if (error_page == null)
85 {
86
87 Integer code=(Integer)request.getAttribute(ServletHandler.__J_S_ERROR_STATUS_CODE);
88 if (code!=null)
89 {
90 error_page= (String)_errorPages.get(TypeUtil.toString(code.intValue()));
91
92
93 if ((error_page == null) && (_errorPageList != null))
94 {
95
96 for (int i = 0; i < _errorPageList.size(); i++)
97 {
98 ErrorCodeRange errCode = (ErrorCodeRange) _errorPageList.get(i);
99 if (errCode.isInRange(code.intValue()))
100 {
101 error_page = errCode.getUri();
102 break;
103 }
104 }
105 }
106 }
107 }
108
109 if (error_page!=null)
110 {
111 String old_error_page=(String)request.getAttribute(WebAppContext.ERROR_PAGE);
112 if (old_error_page==null || !old_error_page.equals(error_page))
113 {
114 request.setAttribute(WebAppContext.ERROR_PAGE, error_page);
115
116 Dispatcher dispatcher = (Dispatcher) _servletContext.getRequestDispatcher(error_page);
117 try
118 {
119 if(dispatcher!=null)
120 {
121 dispatcher.error(request, response);
122 return;
123 }
124 else
125 {
126 Log.warn("No error page "+error_page);
127 }
128 }
129 catch (ServletException e)
130 {
131 Log.warn(Log.EXCEPTION, e);
132 return;
133 }
134 }
135 }
136 }
137
138 super.handle(target, request, response, dispatch);
139 }
140
141
142
143
144
145 public Map getErrorPages()
146 {
147 return _errorPages;
148 }
149
150
151
152
153
154 public void setErrorPages(Map errorPages)
155 {
156 _errorPages = errorPages;
157 }
158
159
160
161
162
163
164
165
166 public void addErrorPage(Class exception,String uri)
167 {
168 if (_errorPages==null)
169 _errorPages=new HashMap();
170 _errorPages.put(exception.getName(),uri);
171 }
172
173
174
175
176
177
178
179
180 public void addErrorPage(int code,String uri)
181 {
182 if (_errorPages==null)
183 _errorPages=new HashMap();
184 _errorPages.put(TypeUtil.toString(code),uri);
185 }
186
187
188
189
190
191
192
193
194
195 public void addErrorPage(int from, int to, String uri)
196 {
197 if (_errorPageList == null)
198 {
199 _errorPageList = new ArrayList();
200 }
201 _errorPageList.add(new ErrorCodeRange(from, to, uri));
202 }
203
204
205 protected void doStart() throws Exception
206 {
207 super.doStart();
208 _servletContext=ContextHandler.getCurrentContext();
209 }
210
211
212 protected void doStop() throws Exception
213 {
214
215 super.doStop();
216 }
217
218
219
220 private class ErrorCodeRange
221 {
222 private int _from;
223 private int _to;
224 private String _uri;
225
226 ErrorCodeRange(int from, int to, String uri)
227 throws IllegalArgumentException
228 {
229 if (from > to)
230 throw new IllegalArgumentException("from>to");
231
232 _from = from;
233 _to = to;
234 _uri = uri;
235 }
236
237 boolean isInRange(int value)
238 {
239 if ((value >= _from) && (value <= _to))
240 {
241 return true;
242 }
243
244 return false;
245 }
246
247 String getUri()
248 {
249 return _uri;
250 }
251
252 public String toString()
253 {
254 return "from: " + _from + ",to: " + _to + ",uri: " + _uri;
255 }
256 }
257 }