KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: NotFoundHandler.java,v 1.15 2005/08/13 00:01:26 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

16 package org.mortbay.http.handler;
17
18 import java.io.IOException JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.mortbay.log.LogFactory;
22 import org.mortbay.http.HttpException;
23 import org.mortbay.http.HttpFields;
24 import org.mortbay.http.HttpRequest;
25 import org.mortbay.http.HttpResponse;
26
27 /* ------------------------------------------------------------ */
28 /** Handler for resources that were not found.
29  * Implements OPTIONS and TRACE methods for the server.
30  *
31  * @version $Id: NotFoundHandler.java,v 1.15 2005/08/13 00:01:26 gregwilkins Exp $
32  * @author Greg Wilkins (gregw)
33  */

34 public class NotFoundHandler extends AbstractHttpHandler
35 {
36     private static Log log = LogFactory.getLog(NotFoundHandler.class);
37
38     /* ------------------------------------------------------------ */
39     public void handle(String JavaDoc pathInContext,
40                        String JavaDoc pathParams,
41                        HttpRequest request,
42                        HttpResponse response)
43         throws HttpException, IOException JavaDoc
44     {
45         log.debug("Not Found");
46         String JavaDoc method=request.getMethod();
47         
48         // Not found requests.
49
if (method.equals(HttpRequest.__GET) ||
50             method.equals(HttpRequest.__HEAD) ||
51             method.equals(HttpRequest.__POST) ||
52             method.equals(HttpRequest.__PUT) ||
53             method.equals(HttpRequest.__DELETE) ||
54             method.equals(HttpRequest.__MOVE) )
55         {
56             response.sendError(HttpResponse.__404_Not_Found,
57                                request.getPath()+" Not Found");
58         }
59         
60         else if (method.equals(HttpRequest.__OPTIONS))
61         {
62             // Handle OPTIONS request for entire server
63
if ("*".equals(request.getPath()))
64             {
65                 // 9.2
66
response.setIntField(HttpFields.__ContentLength,0);
67                 response.setField(HttpFields.__Allow,
68                                   "GET, HEAD, POST, PUT, DELETE, MOVE, OPTIONS, TRACE");
69                 response.commit();
70             }
71             else
72                 response.sendError(HttpResponse.__404_Not_Found);
73         }
74         else if (method.equals(HttpRequest.__TRACE))
75         {
76             handleTrace(request,response);
77         }
78         else
79         {
80             // Unknown METHOD
81
response.setField(HttpFields.__Allow,
82                               "GET, HEAD, POST, PUT, DELETE, MOVE, OPTIONS, TRACE");
83             response.sendError(HttpResponse.__405_Method_Not_Allowed);
84         }
85     }
86 }
87
Popular Tags