KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: RootNotFoundHandler.java,v 1.11 2005/08/13 00:01:26 gregwilkins Exp $
3
// Copyright 2002-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

16 package org.mortbay.http.handler;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.mortbay.log.LogFactory;
23 import org.mortbay.http.HttpContext;
24 import org.mortbay.http.HttpException;
25 import org.mortbay.http.HttpFields;
26 import org.mortbay.http.HttpRequest;
27 import org.mortbay.http.HttpResponse;
28 import org.mortbay.util.ByteArrayISO8859Writer;
29 import org.mortbay.util.StringUtil;
30
31 /* ------------------------------------------------------------ */
32 /**
33  * @version $Id: RootNotFoundHandler.java,v 1.11 2005/08/13 00:01:26 gregwilkins Exp $
34  * @author Greg Wilkins (gregw)
35  */

36 public class RootNotFoundHandler extends NotFoundHandler
37 {
38     private static Log log = LogFactory.getLog(RootNotFoundHandler.class);
39
40     
41     /* ------------------------------------------------------------ */
42     public void handle(String JavaDoc pathInContext,
43                        String JavaDoc pathParams,
44                        HttpRequest request,
45                        HttpResponse response)
46         throws HttpException, IOException JavaDoc
47     {
48         log.debug("Root Not Found");
49         String JavaDoc method=request.getMethod();
50         
51         if (!method.equals(HttpRequest.__GET) ||
52             !request.getPath().equals("/"))
53         {
54             // don't bother with fancy format.
55
super.handle(pathInContext,pathParams,request,response);
56             return;
57         }
58
59         response.setStatus(404);
60         request.setHandled(true);
61         response.setReason("Not Found");
62         response.setContentType(HttpFields.__TextHtml);
63         
64         ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
65
66         String JavaDoc uri=request.getPath();
67         uri=StringUtil.replace(uri,"<","&lt;");
68         uri=StringUtil.replace(uri,">","&gt;");
69         
70         writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
71         writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
72         writer.write("No context on this server matched or handled this request.<BR>");
73         writer.write("Contexts known to this server are: <ul>");
74
75         HttpContext[] contexts = getHttpContext().getHttpServer().getContexts();
76         
77         for (int i=0;i<contexts.length;i++)
78         {
79             HttpContext context = contexts[i];
80             writer.write("<li><a HREF=\"");
81             writer.write(context.getContextPath());
82             writer.write("/\">");
83             writer.write(context.toString());
84             writer.write("</a></li>\n");
85         }
86         
87         writer.write("</ul><small><I>The links above may not work if a virtual host is configured</I></small>");
88
89     for (int i=0;i<10;i++)
90         writer.write("\n<!-- Padding for IE -->");
91     
92         writer.write("\n</BODY>\n</HTML>\n");
93         writer.flush();
94         response.setContentLength(writer.size());
95         OutputStream JavaDoc out=response.getOutputStream();
96         writer.writeTo(out);
97         out.close();
98     }
99 }
100
Popular Tags