Mercurial Hosting > luan
comparison src/org/eclipse/jetty/server/handler/ErrorHandler.java @ 821:292f2e31ab41
remove ErrorHandler.ErrorPageMapper
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 14 Sep 2016 00:48:03 -0600 |
parents | 8e9db0bbf4f9 |
children | 0048a843297a |
comparison
equal
deleted
inserted
replaced
820:8e9db0bbf4f9 | 821:292f2e31ab41 |
---|---|
45 * It is called by the HttpResponse.sendError method to write a error page. | 45 * It is called by the HttpResponse.sendError method to write a error page. |
46 * | 46 * |
47 */ | 47 */ |
48 public class ErrorHandler extends AbstractHandler | 48 public class ErrorHandler extends AbstractHandler |
49 { | 49 { |
50 private static final Logger LOG = LoggerFactory.getLogger(ErrorHandler.class); | 50 private static final Logger LOG = LoggerFactory.getLogger(ErrorHandler.class); |
51 public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page"; | 51 public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page"; |
52 | 52 |
53 boolean _showStacks=true; | 53 boolean _showStacks=true; |
54 boolean _showMessageInTitle=true; | 54 boolean _showMessageInTitle=true; |
55 String _cacheControl="must-revalidate,no-cache,no-store"; | 55 String _cacheControl="must-revalidate,no-cache,no-store"; |
56 | 56 |
57 /* ------------------------------------------------------------ */ | 57 /* ------------------------------------------------------------ */ |
58 /* | 58 /* |
59 * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) | 59 * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) |
60 */ | 60 */ |
61 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException | 61 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException |
62 { | 62 { |
63 AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection(); | 63 AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection(); |
64 String method = request.getMethod(); | 64 String method = request.getMethod(); |
65 if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(HttpMethods.HEAD)) | 65 if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(HttpMethods.HEAD)) |
66 { | 66 { |
67 connection.getRequest().setHandled(true); | 67 connection.getRequest().setHandled(true); |
68 return; | 68 return; |
69 } | 69 } |
70 | 70 |
71 if (this instanceof ErrorPageMapper) | 71 connection.getRequest().setHandled(true); |
72 { | 72 response.setContentType(MimeTypes.TEXT_HTML_8859_1); |
73 String error_page=((ErrorPageMapper)this).getErrorPage(request); | 73 if (_cacheControl!=null) |
74 if (error_page!=null && request.getServletContext()!=null) | 74 response.setHeader(HttpHeaders.CACHE_CONTROL, _cacheControl); |
75 { | 75 ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); |
76 String old_error_page=(String)request.getAttribute(ERROR_PAGE); | 76 handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason()); |
77 if (old_error_page==null || !old_error_page.equals(error_page)) | 77 writer.flush(); |
78 { | 78 response.setContentLength(writer.size()); |
79 request.setAttribute(ERROR_PAGE, error_page); | 79 writer.writeTo(response.getOutputStream()); |
80 | 80 writer.destroy(); |
81 Dispatcher dispatcher = (Dispatcher) request.getServletContext().getRequestDispatcher(error_page); | 81 } |
82 try | 82 |
83 { | 83 /* ------------------------------------------------------------ */ |
84 if(dispatcher!=null) | 84 protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) |
85 { | 85 throws IOException |
86 dispatcher.error(request, response); | 86 { |
87 return; | 87 writeErrorPage(request, writer, code, message, _showStacks); |
88 } | 88 } |
89 LOG.warn("No error page "+error_page); | 89 |
90 } | 90 /* ------------------------------------------------------------ */ |
91 catch (ServletException e) | 91 protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) |
92 { | 92 throws IOException |
93 LOG.warn("EXCEPTION", e); | 93 { |
94 return; | 94 if (message == null) |
95 } | 95 message=HttpStatus.getMessage(code); |
96 } | 96 |
97 } | 97 writer.write("<html>\n<head>\n"); |
98 } | 98 writeErrorPageHead(request,writer,code,message); |
99 | 99 writer.write("</head>\n<body>"); |
100 connection.getRequest().setHandled(true); | 100 writeErrorPageBody(request,writer,code,message,showStacks); |
101 response.setContentType(MimeTypes.TEXT_HTML_8859_1); | 101 writer.write("\n</body>\n</html>\n"); |
102 if (_cacheControl!=null) | 102 } |
103 response.setHeader(HttpHeaders.CACHE_CONTROL, _cacheControl); | 103 |
104 ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); | 104 /* ------------------------------------------------------------ */ |
105 handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason()); | 105 protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) |
106 writer.flush(); | 106 throws IOException |
107 response.setContentLength(writer.size()); | 107 { |
108 writer.writeTo(response.getOutputStream()); | 108 writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n"); |
109 writer.destroy(); | 109 writer.write("<title>Error "); |
110 } | 110 writer.write(Integer.toString(code)); |
111 | 111 |
112 /* ------------------------------------------------------------ */ | 112 if (_showMessageInTitle) |
113 protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) | 113 { |
114 throws IOException | 114 writer.write(' '); |
115 { | 115 write(writer,message); |
116 writeErrorPage(request, writer, code, message, _showStacks); | 116 } |
117 } | 117 writer.write("</title>\n"); |
118 | 118 } |
119 /* ------------------------------------------------------------ */ | 119 |
120 protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) | 120 /* ------------------------------------------------------------ */ |
121 throws IOException | 121 protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) |
122 { | 122 throws IOException |
123 if (message == null) | 123 { |
124 message=HttpStatus.getMessage(code); | 124 String uri= request.getRequestURI(); |
125 | 125 |
126 writer.write("<html>\n<head>\n"); | 126 writeErrorPageMessage(request,writer,code,message,uri); |
127 writeErrorPageHead(request,writer,code,message); | 127 if (showStacks) |
128 writer.write("</head>\n<body>"); | 128 writeErrorPageStacks(request,writer); |
129 writeErrorPageBody(request,writer,code,message,showStacks); | 129 writer.write("<hr /><i><small>Powered by Jetty://</small></i>"); |
130 writer.write("\n</body>\n</html>\n"); | 130 for (int i= 0; i < 20; i++) |
131 } | 131 writer.write("<br/> \n"); |
132 | 132 } |
133 /* ------------------------------------------------------------ */ | 133 |
134 protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) | 134 /* ------------------------------------------------------------ */ |
135 throws IOException | 135 protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri) |
136 { | 136 throws IOException |
137 writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n"); | 137 { |
138 writer.write("<title>Error "); | 138 writer.write("<h2>HTTP ERROR "); |
139 writer.write(Integer.toString(code)); | 139 writer.write(Integer.toString(code)); |
140 | 140 writer.write("</h2>\n<p>Problem accessing "); |
141 if (_showMessageInTitle) | 141 write(writer,uri); |
142 { | 142 writer.write(". Reason:\n<pre> "); |
143 writer.write(' '); | 143 write(writer,message); |
144 write(writer,message); | 144 writer.write("</pre></p>"); |
145 } | 145 } |
146 writer.write("</title>\n"); | 146 |
147 } | 147 /* ------------------------------------------------------------ */ |
148 | 148 protected void writeErrorPageStacks(HttpServletRequest request, Writer writer) |
149 /* ------------------------------------------------------------ */ | 149 throws IOException |
150 protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) | 150 { |
151 throws IOException | 151 Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception"); |
152 { | 152 while(th!=null) |
153 String uri= request.getRequestURI(); | 153 { |
154 | 154 writer.write("<h3>Caused by:</h3><pre>"); |
155 writeErrorPageMessage(request,writer,code,message,uri); | 155 StringWriter sw = new StringWriter(); |
156 if (showStacks) | 156 PrintWriter pw = new PrintWriter(sw); |
157 writeErrorPageStacks(request,writer); | 157 th.printStackTrace(pw); |
158 writer.write("<hr /><i><small>Powered by Jetty://</small></i>"); | 158 pw.flush(); |
159 for (int i= 0; i < 20; i++) | 159 write(writer,sw.getBuffer().toString()); |
160 writer.write("<br/> \n"); | 160 writer.write("</pre>\n"); |
161 } | 161 |
162 | 162 th =th.getCause(); |
163 /* ------------------------------------------------------------ */ | 163 } |
164 protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri) | 164 } |
165 throws IOException | 165 |
166 { | 166 |
167 writer.write("<h2>HTTP ERROR "); | 167 /* ------------------------------------------------------------ */ |
168 writer.write(Integer.toString(code)); | 168 /** Get the cacheControl. |
169 writer.write("</h2>\n<p>Problem accessing "); | 169 * @return the cacheControl header to set on error responses. |
170 write(writer,uri); | 170 */ |
171 writer.write(". Reason:\n<pre> "); | 171 public String getCacheControl() |
172 write(writer,message); | 172 { |
173 writer.write("</pre></p>"); | 173 return _cacheControl; |
174 } | 174 } |
175 | 175 |
176 /* ------------------------------------------------------------ */ | 176 /* ------------------------------------------------------------ */ |
177 protected void writeErrorPageStacks(HttpServletRequest request, Writer writer) | 177 /** Set the cacheControl. |
178 throws IOException | 178 * @param cacheControl the cacheControl header to set on error responses. |
179 { | 179 */ |
180 Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception"); | 180 public void setCacheControl(String cacheControl) |
181 while(th!=null) | 181 { |
182 { | 182 _cacheControl = cacheControl; |
183 writer.write("<h3>Caused by:</h3><pre>"); | 183 } |
184 StringWriter sw = new StringWriter(); | 184 |
185 PrintWriter pw = new PrintWriter(sw); | 185 /* ------------------------------------------------------------ */ |
186 th.printStackTrace(pw); | 186 /** |
187 pw.flush(); | 187 * @return True if stack traces are shown in the error pages |
188 write(writer,sw.getBuffer().toString()); | 188 */ |
189 writer.write("</pre>\n"); | 189 public boolean isShowStacks() |
190 | 190 { |
191 th =th.getCause(); | 191 return _showStacks; |
192 } | 192 } |
193 } | 193 |
194 | 194 /* ------------------------------------------------------------ */ |
195 | 195 /** |
196 /* ------------------------------------------------------------ */ | 196 * @param showStacks True if stack traces are shown in the error pages |
197 /** Get the cacheControl. | 197 */ |
198 * @return the cacheControl header to set on error responses. | 198 public void setShowStacks(boolean showStacks) |
199 */ | 199 { |
200 public String getCacheControl() | 200 _showStacks = showStacks; |
201 { | 201 } |
202 return _cacheControl; | 202 |
203 } | 203 /* ------------------------------------------------------------ */ |
204 | 204 /** |
205 /* ------------------------------------------------------------ */ | 205 * @param showMessageInTitle if true, the error message appears in page title |
206 /** Set the cacheControl. | 206 */ |
207 * @param cacheControl the cacheControl header to set on error responses. | 207 public void setShowMessageInTitle(boolean showMessageInTitle) |
208 */ | 208 { |
209 public void setCacheControl(String cacheControl) | 209 _showMessageInTitle = showMessageInTitle; |
210 { | 210 } |
211 _cacheControl = cacheControl; | 211 |
212 } | 212 |
213 | 213 /* ------------------------------------------------------------ */ |
214 /* ------------------------------------------------------------ */ | 214 public boolean getShowMessageInTitle() |
215 /** | 215 { |
216 * @return True if stack traces are shown in the error pages | 216 return _showMessageInTitle; |
217 */ | 217 } |
218 public boolean isShowStacks() | 218 |
219 { | 219 /* ------------------------------------------------------------ */ |
220 return _showStacks; | 220 protected void write(Writer writer,String string) |
221 } | 221 throws IOException |
222 | 222 { |
223 /* ------------------------------------------------------------ */ | 223 if (string==null) |
224 /** | 224 return; |
225 * @param showStacks True if stack traces are shown in the error pages | 225 |
226 */ | 226 for (int i=0;i<string.length();i++) |
227 public void setShowStacks(boolean showStacks) | 227 { |
228 { | 228 char c=string.charAt(i); |
229 _showStacks = showStacks; | 229 |
230 } | 230 switch(c) |
231 | 231 { |
232 /* ------------------------------------------------------------ */ | 232 case '&' : |
233 /** | 233 writer.write("&"); |
234 * @param showMessageInTitle if true, the error message appears in page title | 234 break; |
235 */ | 235 case '<' : |
236 public void setShowMessageInTitle(boolean showMessageInTitle) | 236 writer.write("<"); |
237 { | 237 break; |
238 _showMessageInTitle = showMessageInTitle; | 238 case '>' : |
239 } | 239 writer.write(">"); |
240 | 240 break; |
241 | 241 |
242 /* ------------------------------------------------------------ */ | 242 default: |
243 public boolean getShowMessageInTitle() | 243 if (Character.isISOControl(c) && !Character.isWhitespace(c)) |
244 { | 244 writer.write('?'); |
245 return _showMessageInTitle; | 245 else |
246 } | 246 writer.write(c); |
247 | 247 } |
248 /* ------------------------------------------------------------ */ | 248 } |
249 protected void write(Writer writer,String string) | 249 } |
250 throws IOException | |
251 { | |
252 if (string==null) | |
253 return; | |
254 | |
255 for (int i=0;i<string.length();i++) | |
256 { | |
257 char c=string.charAt(i); | |
258 | |
259 switch(c) | |
260 { | |
261 case '&' : | |
262 writer.write("&"); | |
263 break; | |
264 case '<' : | |
265 writer.write("<"); | |
266 break; | |
267 case '>' : | |
268 writer.write(">"); | |
269 break; | |
270 | |
271 default: | |
272 if (Character.isISOControl(c) && !Character.isWhitespace(c)) | |
273 writer.write('?'); | |
274 else | |
275 writer.write(c); | |
276 } | |
277 } | |
278 } | |
279 | |
280 /* ------------------------------------------------------------ */ | |
281 public interface ErrorPageMapper | |
282 { | |
283 String getErrorPage(HttpServletRequest request); | |
284 } | |
285 } | 250 } |