1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.server.handler;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.io.Writer;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.eclipse.jetty.http.HttpHeaders;
31 import org.eclipse.jetty.http.HttpMethods;
32 import org.eclipse.jetty.http.HttpStatus;
33 import org.eclipse.jetty.http.MimeTypes;
34 import org.eclipse.jetty.server.AbstractHttpConnection;
35 import org.eclipse.jetty.server.Dispatcher;
36 import org.eclipse.jetty.server.Request;
37 import org.eclipse.jetty.util.ByteArrayISO8859Writer;
38 import org.eclipse.jetty.util.log.Log;
39 import org.eclipse.jetty.util.log.Logger;
40
41
42
43
44
45
46
47
48 public class ErrorHandler extends AbstractHandler
49 {
50 private static final Logger LOG = Log.getLogger(ErrorHandler.class);
51 public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
52
53 boolean _showStacks=true;
54 boolean _showMessageInTitle=true;
55 String _cacheControl="must-revalidate,no-cache,no-store";
56
57
58
59
60
61 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
62 {
63 AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
64 String method = request.getMethod();
65 if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(HttpMethods.HEAD))
66 {
67 connection.getRequest().setHandled(true);
68 return;
69 }
70
71 if (this instanceof ErrorPageMapper)
72 {
73 String error_page=((ErrorPageMapper)this).getErrorPage(request);
74 if (error_page!=null && request.getServletContext()!=null)
75 {
76 String old_error_page=(String)request.getAttribute(ERROR_PAGE);
77 if (old_error_page==null || !old_error_page.equals(error_page))
78 {
79 request.setAttribute(ERROR_PAGE, error_page);
80
81 Dispatcher dispatcher = (Dispatcher) request.getServletContext().getRequestDispatcher(error_page);
82 try
83 {
84 if(dispatcher!=null)
85 {
86 dispatcher.error(request, response);
87 return;
88 }
89 LOG.warn("No error page "+error_page);
90 }
91 catch (ServletException e)
92 {
93 LOG.warn(Log.EXCEPTION, e);
94 return;
95 }
96 }
97 }
98 }
99
100 connection.getRequest().setHandled(true);
101 response.setContentType(MimeTypes.TEXT_HTML_8859_1);
102 if (_cacheControl!=null)
103 response.setHeader(HttpHeaders.CACHE_CONTROL, _cacheControl);
104 ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
105 handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason());
106 writer.flush();
107 response.setContentLength(writer.size());
108 writer.writeTo(response.getOutputStream());
109 writer.destroy();
110 }
111
112
113 protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
114 throws IOException
115 {
116 writeErrorPage(request, writer, code, message, _showStacks);
117 }
118
119
120 protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
121 throws IOException
122 {
123 if (message == null)
124 message=HttpStatus.getMessage(code);
125
126 writer.write("<html>\n<head>\n");
127 writeErrorPageHead(request,writer,code,message);
128 writer.write("</head>\n<body>");
129 writeErrorPageBody(request,writer,code,message,showStacks);
130 writer.write("\n</body>\n</html>\n");
131 }
132
133
134 protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
135 throws IOException
136 {
137 writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n");
138 writer.write("<title>Error ");
139 writer.write(Integer.toString(code));
140
141 if (_showMessageInTitle)
142 {
143 writer.write(' ');
144 write(writer,message);
145 }
146 writer.write("</title>\n");
147 }
148
149
150 protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
151 throws IOException
152 {
153 String uri= request.getRequestURI();
154
155 writeErrorPageMessage(request,writer,code,message,uri);
156 if (showStacks)
157 writeErrorPageStacks(request,writer);
158 writer.write("<hr /><i><small>Powered by Jetty://</small></i>");
159 for (int i= 0; i < 20; i++)
160 writer.write("<br/> \n");
161 }
162
163
164 protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
165 throws IOException
166 {
167 writer.write("<h2>HTTP ERROR ");
168 writer.write(Integer.toString(code));
169 writer.write("</h2>\n<p>Problem accessing ");
170 write(writer,uri);
171 writer.write(". Reason:\n<pre> ");
172 write(writer,message);
173 writer.write("</pre></p>");
174 }
175
176
177 protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
178 throws IOException
179 {
180 Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
181 while(th!=null)
182 {
183 writer.write("<h3>Caused by:</h3><pre>");
184 StringWriter sw = new StringWriter();
185 PrintWriter pw = new PrintWriter(sw);
186 th.printStackTrace(pw);
187 pw.flush();
188 write(writer,sw.getBuffer().toString());
189 writer.write("</pre>\n");
190
191 th =th.getCause();
192 }
193 }
194
195
196
197
198
199
200 public String getCacheControl()
201 {
202 return _cacheControl;
203 }
204
205
206
207
208
209 public void setCacheControl(String cacheControl)
210 {
211 _cacheControl = cacheControl;
212 }
213
214
215
216
217
218 public boolean isShowStacks()
219 {
220 return _showStacks;
221 }
222
223
224
225
226
227 public void setShowStacks(boolean showStacks)
228 {
229 _showStacks = showStacks;
230 }
231
232
233
234
235
236 public void setShowMessageInTitle(boolean showMessageInTitle)
237 {
238 _showMessageInTitle = showMessageInTitle;
239 }
240
241
242
243 public boolean getShowMessageInTitle()
244 {
245 return _showMessageInTitle;
246 }
247
248
249 protected void write(Writer writer,String string)
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 }