1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.server.handler;
14 import java.io.IOException;
15 import java.io.PrintWriter;
16 import java.io.StringWriter;
17 import java.io.Writer;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.eclipse.jetty.http.HttpHeaders;
23 import org.eclipse.jetty.http.HttpMethods;
24 import org.eclipse.jetty.http.HttpStatus;
25 import org.eclipse.jetty.http.MimeTypes;
26 import org.eclipse.jetty.server.HttpConnection;
27 import org.eclipse.jetty.server.Request;
28 import org.eclipse.jetty.util.ByteArrayISO8859Writer;
29 import org.eclipse.jetty.util.StringUtil;
30
31
32
33
34
35
36
37
38
39
40 public class ErrorHandler extends AbstractHandler
41 {
42 boolean _showStacks=true;
43
44
45
46
47
48 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
49 {
50 HttpConnection connection = HttpConnection.getCurrentConnection();
51 connection.getRequest().setHandled(true);
52 String method = request.getMethod();
53 if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST))
54 return;
55 response.setContentType(MimeTypes.TEXT_HTML_8859_1);
56 response.setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
57 ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
58 handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason());
59 writer.flush();
60 response.setContentLength(writer.size());
61 writer.writeTo(response.getOutputStream());
62 writer.destroy();
63 }
64
65
66 protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
67 throws IOException
68 {
69 writeErrorPage(request, writer, code, message, _showStacks);
70 }
71
72
73 protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
74 throws IOException
75 {
76 if (message == null)
77 message=HttpStatus.getCode(code).getMessage();
78 else
79 {
80 message= StringUtil.replace(message, "&", "&");
81 message= StringUtil.replace(message, "<", "<");
82 message= StringUtil.replace(message, ">", ">");
83 }
84
85 writer.write("<html>\n<head>\n");
86 writeErrorPageHead(request,writer,code,message);
87 writer.write("</head>\n<body>");
88 writeErrorPageBody(request,writer,code,message,showStacks);
89 writer.write("\n</body>\n</html>\n");
90 }
91
92
93 protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
94 throws IOException
95 {
96 writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n");
97 writer.write("<title>Error ");
98 writer.write(Integer.toString(code));
99 writer.write(' ');
100 if (message!=null)
101 writer.write(message);
102 writer.write("</title>\n");
103 }
104
105
106 protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
107 throws IOException
108 {
109 String uri= request.getRequestURI();
110 if (uri!=null)
111 {
112 uri= StringUtil.replace(uri, "&", "&");
113 uri= StringUtil.replace(uri, "<", "<");
114 uri= StringUtil.replace(uri, ">", ">");
115 }
116
117 writeErrorPageMessage(request,writer,code,message,uri);
118 if (showStacks)
119 writeErrorPageStacks(request,writer);
120 writer.write("<hr /><i><small>Powered by Jetty://</small></i>");
121 for (int i= 0; i < 20; i++)
122 writer.write("<br/> \n");
123 }
124
125
126 protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
127 throws IOException
128 {
129 writer.write("<h2>HTTP ERROR ");
130 writer.write(Integer.toString(code));
131 writer.write("</h2>\n<p>Problem accessing ");
132 writer.write(uri);
133 writer.write(". Reason:\n<pre> ");
134 writer.write(message);
135 writer.write("</pre></p>");
136 }
137
138
139 protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
140 throws IOException
141 {
142 Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
143 while(th!=null)
144 {
145 writer.write("<h3>Caused by:</h3><pre>");
146 StringWriter sw = new StringWriter();
147 PrintWriter pw = new PrintWriter(sw);
148 th.printStackTrace(pw);
149 pw.flush();
150 writer.write(sw.getBuffer().toString());
151 writer.write("</pre>\n");
152
153 th =th.getCause();
154 }
155 }
156
157
158
159
160
161
162 public boolean isShowStacks()
163 {
164 return _showStacks;
165 }
166
167
168
169
170
171 public void setShowStacks(boolean showStacks)
172 {
173 _showStacks = showStacks;
174 }
175
176 }