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