-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ErrorPageErrorHandler.java
292 lines (255 loc) · 9.39 KB
/
ErrorPageErrorHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.ee10.servlet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An ErrorHandler that maps exceptions and status codes to URIs for dispatch using
* the internal ERROR style of dispatch.
*/
public class ErrorPageErrorHandler extends ErrorHandler implements ErrorHandler.ErrorPageMapper
{
public static final String GLOBAL_ERROR_PAGE = "org.eclipse.jetty.server.error_page.global";
private static final Logger LOG = LoggerFactory.getLogger(ErrorPageErrorHandler.class);
private enum PageLookupTechnique
{
THROWABLE, STATUS_CODE, GLOBAL
}
private final Map<String, String> _errorPages = new HashMap<>(); // code or exception to URL
private final List<ErrorCodeRange> _errorPageList = new ArrayList<>(); // list of ErrorCode by range
private boolean _unwrapServletException = true;
/**
* @return True if ServletException is unwrapped for {@link Dispatcher#ERROR_EXCEPTION}
*/
public boolean isUnwrapServletException()
{
return _unwrapServletException;
}
/**
* @param unwrapServletException True if ServletException should be unwrapped for {@link Dispatcher#ERROR_EXCEPTION}
*/
public void setUnwrapServletException(boolean unwrapServletException)
{
_unwrapServletException = unwrapServletException;
}
@Override
public String getErrorPage(HttpServletRequest request)
{
String errorPage = null;
PageLookupTechnique pageSource = null;
Class<?> matchedThrowable = null;
Throwable error = (Throwable)request.getAttribute(Dispatcher.ERROR_EXCEPTION);
Throwable cause = error;
// Walk the cause hierarchy
while (errorPage == null && cause != null)
{
pageSource = PageLookupTechnique.THROWABLE;
Class<?> exClass = cause.getClass();
errorPage = _errorPages.get(exClass.getName());
// walk the inheritance hierarchy
while (errorPage == null)
{
exClass = exClass.getSuperclass();
if (exClass == null)
break;
errorPage = _errorPages.get(exClass.getName());
}
if (errorPage != null)
matchedThrowable = exClass;
cause = (cause instanceof ServletException) ? ((ServletException)cause).getRootCause() : null;
}
if (error instanceof ServletException && _unwrapServletException)
{
Throwable unwrapped = getFirstNonServletException(error, matchedThrowable);
if (unwrapped != null)
{
request.setAttribute(Dispatcher.ERROR_EXCEPTION, unwrapped);
request.setAttribute(Dispatcher.ERROR_EXCEPTION_TYPE, unwrapped.getClass());
}
}
Integer errorStatusCode = null;
if (errorPage == null)
{
pageSource = PageLookupTechnique.STATUS_CODE;
// look for an exact code match
errorStatusCode = (Integer)request.getAttribute(Dispatcher.ERROR_STATUS_CODE);
if (errorStatusCode != null)
{
errorPage = _errorPages.get(Integer.toString(errorStatusCode));
// if still not found
if (errorPage == null)
{
// look for an error code range match.
for (ErrorCodeRange errCode : _errorPageList)
{
if (errCode.isInRange(errorStatusCode))
{
errorPage = errCode.getUri();
break;
}
}
}
}
}
// Try servlet 3.x global error page.
if (errorPage == null)
{
pageSource = PageLookupTechnique.GLOBAL;
errorPage = _errorPages.get(GLOBAL_ERROR_PAGE);
}
if (LOG.isDebugEnabled())
{
StringBuilder dbg = new StringBuilder();
dbg.append("getErrorPage(");
dbg.append(request.getMethod()).append(' ');
dbg.append(request.getRequestURI());
dbg.append(") => error_page=").append(errorPage);
switch (pageSource)
{
case THROWABLE:
dbg.append(" (using matched Throwable ");
dbg.append(matchedThrowable.getName());
dbg.append(" / actually thrown as ");
Throwable originalThrowable = (Throwable)request.getAttribute(Dispatcher.ERROR_EXCEPTION);
dbg.append(originalThrowable.getClass().getName());
dbg.append(')');
LOG.debug(dbg.toString(), cause);
break;
case STATUS_CODE:
dbg.append(" (from status code ");
dbg.append(errorStatusCode);
dbg.append(')');
LOG.debug(dbg.toString());
break;
case GLOBAL:
dbg.append(" (from global default)");
LOG.debug(dbg.toString());
break;
default:
throw new IllegalStateException(pageSource.toString());
}
}
return errorPage;
}
/**
*
* @param t the initial exception
* @param matchedThrowable the class we found matching the error page (can be null)
* @return the first non {@link ServletException} from root cause chain
*/
private Throwable getFirstNonServletException(Throwable t, Class<?> matchedThrowable)
{
if (matchedThrowable != null && t.getClass() == matchedThrowable)
return t;
if (t instanceof ServletException && t.getCause() != null)
{
return getFirstNonServletException(t.getCause(), matchedThrowable);
}
return t;
}
public Map<String, String> getErrorPages()
{
return _errorPages;
}
/**
* @param errorPages a map of Exception class names or error codes as a string to URI string
*/
public void setErrorPages(Map<String, String> errorPages)
{
_errorPages.clear();
if (errorPages != null)
_errorPages.putAll(errorPages);
}
/**
* Adds ErrorPage mapping for an exception class.
* This method is called as a result of an exception-type element in a web.xml file
* or may be called directly
*
* @param exception The exception
* @param uri The URI of the error page.
*/
public void addErrorPage(Class<? extends Throwable> exception, String uri)
{
_errorPages.put(exception.getName(), uri);
}
/**
* Adds ErrorPage mapping for an exception class.
* This method is called as a result of an exception-type element in a web.xml file
* or may be called directly
*
* @param exceptionClassName The exception
* @param uri The URI of the error page.
*/
public void addErrorPage(String exceptionClassName, String uri)
{
_errorPages.put(exceptionClassName, uri);
}
/**
* Adds ErrorPage mapping for a status code.
* This method is called as a result of an error-code element in a web.xml file
* or may be called directly.
*
* @param code The HTTP status code to match
* @param uri The URI of the error page.
*/
public void addErrorPage(int code, String uri)
{
_errorPages.put(Integer.toString(code), uri);
}
/**
* Adds ErrorPage mapping for a status code range.
* This method is not available from web.xml and must be called directly.
*
* @param from The lowest matching status code
* @param to The highest matching status code
* @param uri The URI of the error page.
*/
public void addErrorPage(int from, int to, String uri)
{
_errorPageList.add(new ErrorCodeRange(from, to, uri));
}
private static class ErrorCodeRange
{
private final int _from;
private final int _to;
private final String _uri;
ErrorCodeRange(int from, int to, String uri)
throws IllegalArgumentException
{
if (from > to)
throw new IllegalArgumentException("from>to");
_from = from;
_to = to;
_uri = uri;
}
boolean isInRange(int value)
{
return _from <= value && value <= _to;
}
String getUri()
{
return _uri;
}
@Override
public String toString()
{
return "from: " + _from + ",to: " + _to + ",uri: " + _uri;
}
}
}