1 16 package org.mortbay.http.handler; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 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 36 public class RootNotFoundHandler extends NotFoundHandler 37 { 38 private static Log log = LogFactory.getLog(RootNotFoundHandler.class); 39 40 41 42 public void handle(String pathInContext, 43 String pathParams, 44 HttpRequest request, 45 HttpResponse response) 46 throws HttpException, IOException 47 { 48 log.debug("Root Not Found"); 49 String method=request.getMethod(); 50 51 if (!method.equals(HttpRequest.__GET) || 52 !request.getPath().equals("/")) 53 { 54 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 uri=request.getPath(); 67 uri=StringUtil.replace(uri,"<","<"); 68 uri=StringUtil.replace(uri,">",">"); 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 out=response.getOutputStream(); 96 writer.writeTo(out); 97 out.close(); 98 } 99 } 100 | Popular Tags |