annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 // ------------------------------------------------------------------------
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 // All rights reserved. This program and the accompanying materials
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 // are made available under the terms of the Eclipse Public License v1.0
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 // and Apache License v2.0 which accompanies this distribution.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 // The Eclipse Public License is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 // http://www.eclipse.org/legal/epl-v10.html
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 // The Apache License v2.0 is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 // http://www.opensource.org/licenses/apache2.0.php
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 // You may elect to redistribute this code under either of these licenses.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 package org.eclipse.jetty.server.handler;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import java.io.IOException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 import java.io.PrintWriter;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 import java.io.StringWriter;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 import java.io.Writer;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 import javax.servlet.ServletException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 import javax.servlet.http.HttpServletRequest;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 import javax.servlet.http.HttpServletResponse;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 import org.eclipse.jetty.http.HttpHeaders;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 import org.eclipse.jetty.http.HttpMethods;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 import org.eclipse.jetty.http.HttpStatus;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 import org.eclipse.jetty.http.MimeTypes;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 import org.eclipse.jetty.server.AbstractHttpConnection;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 import org.eclipse.jetty.server.Dispatcher;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 import org.eclipse.jetty.server.Request;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 import org.eclipse.jetty.util.ByteArrayISO8859Writer;
820
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
38 import org.slf4j.Logger;
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
39 import org.slf4j.LoggerFactory;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 /* ------------------------------------------------------------ */
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 /** Handler for Error pages
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 * An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 * {@link org.eclipse.jetty.server.Server#addBean(Object)}.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 * It is called by the HttpResponse.sendError method to write a error page.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 */
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 public class ErrorHandler extends AbstractHandler
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 {
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
50 private static final Logger LOG = LoggerFactory.getLogger(ErrorHandler.class);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
51 public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
52
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
53 boolean _showStacks=true;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
54 boolean _showMessageInTitle=true;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
55 String _cacheControl="must-revalidate,no-cache,no-store";
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
56
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
57 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
58 /*
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
59 * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
60 */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
61 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
62 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
63 AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
64 String method = request.getMethod();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
65 if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(HttpMethods.HEAD))
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
66 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
67 connection.getRequest().setHandled(true);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
68 return;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
69 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
70
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
71 connection.getRequest().setHandled(true);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
72 response.setContentType(MimeTypes.TEXT_HTML_8859_1);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
73 if (_cacheControl!=null)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
74 response.setHeader(HttpHeaders.CACHE_CONTROL, _cacheControl);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
75 ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
76 handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason());
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
77 writer.flush();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
78 response.setContentLength(writer.size());
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
79 writer.writeTo(response.getOutputStream());
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
80 writer.destroy();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
81 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
82
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
83 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
84 protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
85 throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
86 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
87 writeErrorPage(request, writer, code, message, _showStacks);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
88 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
89
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
90 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
91 protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
92 throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
93 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
94 if (message == null)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
95 message=HttpStatus.getMessage(code);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
97 writer.write("<html>\n<head>\n");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
98 writeErrorPageHead(request,writer,code,message);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
99 writer.write("</head>\n<body>");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
100 writeErrorPageBody(request,writer,code,message,showStacks);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
101 writer.write("\n</body>\n</html>\n");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
102 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
103
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
104 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
105 protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
106 throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
107 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
108 writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
109 writer.write("<title>Error ");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
110 writer.write(Integer.toString(code));
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
111
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
112 if (_showMessageInTitle)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
113 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
114 writer.write(' ');
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
115 write(writer,message);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
116 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
117 writer.write("</title>\n");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
118 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
120 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
121 protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
122 throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
123 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
124 String uri= request.getRequestURI();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
125
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
126 writeErrorPageMessage(request,writer,code,message,uri);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
127 if (showStacks)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
128 writeErrorPageStacks(request,writer);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
129 writer.write("<hr /><i><small>Powered by Jetty://</small></i>");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
130 for (int i= 0; i < 20; i++)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
131 writer.write("<br/> \n");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
132 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
134 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
135 protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
136 throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
137 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
138 writer.write("<h2>HTTP ERROR ");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
139 writer.write(Integer.toString(code));
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
140 writer.write("</h2>\n<p>Problem accessing ");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
141 write(writer,uri);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
142 writer.write(". Reason:\n<pre> ");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
143 write(writer,message);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
144 writer.write("</pre></p>");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
145 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
147 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
148 protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
149 throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
150 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
151 Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
152 while(th!=null)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
153 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
154 writer.write("<h3>Caused by:</h3><pre>");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
155 StringWriter sw = new StringWriter();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
156 PrintWriter pw = new PrintWriter(sw);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
157 th.printStackTrace(pw);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
158 pw.flush();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
159 write(writer,sw.getBuffer().toString());
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
160 writer.write("</pre>\n");
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
162 th =th.getCause();
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
163 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
164 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
165
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
167 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
168 /** Get the cacheControl.
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
169 * @return the cacheControl header to set on error responses.
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
170 */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
171 public String getCacheControl()
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
172 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
173 return _cacheControl;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
174 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
176 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
177 /** Set the cacheControl.
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
178 * @param cacheControl the cacheControl header to set on error responses.
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
179 */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
180 public void setCacheControl(String cacheControl)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
181 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
182 _cacheControl = cacheControl;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
183 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
185 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
186 /**
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
187 * @return True if stack traces are shown in the error pages
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
188 */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
189 public boolean isShowStacks()
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
190 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
191 return _showStacks;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
192 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
194 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
195 /**
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
196 * @param showStacks True if stack traces are shown in the error pages
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
197 */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
198 public void setShowStacks(boolean showStacks)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
199 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
200 _showStacks = showStacks;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
201 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
202
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
203 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
204 /**
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
205 * @param showMessageInTitle if true, the error message appears in page title
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
206 */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
207 public void setShowMessageInTitle(boolean showMessageInTitle)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
208 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
209 _showMessageInTitle = showMessageInTitle;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
210 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
211
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
212
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
213 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
214 public boolean getShowMessageInTitle()
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
215 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
216 return _showMessageInTitle;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
217 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
218
821
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
219 /* ------------------------------------------------------------ */
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
220 protected void write(Writer writer,String string)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
221 throws IOException
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
222 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
223 if (string==null)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
224 return;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
225
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
226 for (int i=0;i<string.length();i++)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
227 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
228 char c=string.charAt(i);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
229
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
230 switch(c)
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
231 {
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
232 case '&' :
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
233 writer.write("&amp;");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
234 break;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
235 case '<' :
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
236 writer.write("&lt;");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
237 break;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
238 case '>' :
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
239 writer.write("&gt;");
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
240 break;
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
241
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
242 default:
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
243 if (Character.isISOControl(c) && !Character.isWhitespace(c))
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
244 writer.write('?');
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
245 else
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
246 writer.write(c);
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
247 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
248 }
292f2e31ab41 remove ErrorHandler.ErrorPageMapper
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
249 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
250 }