KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > handler > ErrorPageHandler


1 // ========================================================================
2
// $Id: ErrorPageHandler.java,v 1.9 2005/03/15 10:03:44 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15
package org.mortbay.http.handler;
16 import java.io.IOException JavaDoc;
17 import java.io.Writer JavaDoc;
18 import java.net.URLDecoder JavaDoc;
19
20 import org.mortbay.http.HttpException;
21 import org.mortbay.http.HttpFields;
22 import org.mortbay.http.HttpRequest;
23 import org.mortbay.http.HttpResponse;
24 import org.mortbay.util.ByteArrayISO8859Writer;
25 import org.mortbay.util.StringUtil;
26 /* ------------------------------------------------------------ */
27 /** Handler for Error pages
28  * A handler that is registered at the org.mortbay.http.ErrorHandler
29  * context attributed and called by the HttpResponse.sendError method to write a
30  * error page.
31  *
32  * @version $Id: ErrorPageHandler.java,v 1.9 2005/03/15 10:03:44 gregwilkins Exp $
33  * @author Greg Wilkins (gregw)
34  */

35 public class ErrorPageHandler extends AbstractHttpHandler
36 {
37     /* ------------------------------------------------------------ */
38     public void handle(
39         String JavaDoc pathInContext,
40         String JavaDoc pathParams,
41         HttpRequest request,
42         HttpResponse response)
43         throws HttpException, IOException JavaDoc
44     {
45         response.setContentType(HttpFields.__TextHtml);
46         ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(2048);
47         writeErrorPage(request, writer, response.getStatus(), response.getReason());
48         writer.flush();
49         response.setContentLength(writer.size());
50         writer.writeTo(response.getOutputStream());
51         writer.destroy();
52     }
53     
54     /* ------------------------------------------------------------ */
55     protected void writeErrorPage(HttpRequest request, Writer JavaDoc writer, int code, String JavaDoc message)
56         throws IOException JavaDoc
57     {
58         if (message != null)
59         {
60             message=URLDecoder.decode(message,"UTF-8");
61             message= StringUtil.replace(message, "<", "&lt;");
62             message= StringUtil.replace(message, ">", "&gt;");
63         }
64         String JavaDoc uri= request.getPath();
65         uri= StringUtil.replace(uri, "<", "&lt;");
66         uri= StringUtil.replace(uri, ">", "&gt;");
67         writer.write("<html>\n<head>\n<title>Error ");
68         writer.write(Integer.toString(code));
69         writer.write(' ');
70         writer.write(message);
71         writer.write("</title>\n</head>\n<body>\n<h2>HTTP ERROR: ");
72         writer.write(Integer.toString(code));
73         writer.write("</h2><pre>");
74         writer.write(message);
75         writer.write("</pre>\n");
76         writer.write("<p>RequestURI=");
77         writer.write(uri);
78         writer.write(
79             "</p>\n<p><i><small><a HREF=\"http://jetty.mortbay.org\">Powered by Jetty://</a></small></i></p>");
80         for (int i= 0; i < 20; i++)
81             writer.write("\n ");
82         writer.write("\n</body>\n</html>\n");
83     }
84 }
85
Popular Tags