KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: AbstractHttpHandler.java,v 1.12 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 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.HttpFields;
25 import org.mortbay.http.HttpHandler;
26 import org.mortbay.http.HttpRequest;
27 import org.mortbay.http.HttpResponse;
28 import org.mortbay.util.ByteArrayISO8859Writer;
29
30 /* ------------------------------------------------------------ */
31 /** Base HTTP Handler.
32  * This No-op handler is a good base for other handlers
33  *
34  * @version $Id: AbstractHttpHandler.java,v 1.12 2005/08/13 00:01:26 gregwilkins Exp $
35  * @author Greg Wilkins (gregw)
36  */

37 abstract public class AbstractHttpHandler implements HttpHandler
38 {
39     private static Log log = LogFactory.getLog(AbstractHttpHandler.class);
40
41     /* ----------------------------------------------------------------- */
42     private String JavaDoc _name;
43     
44     private transient HttpContext _context;
45     private transient boolean _started=false;
46
47     
48     /* ------------------------------------------------------------ */
49     public void setName(String JavaDoc name)
50     {
51         _name=name;
52     }
53     
54     /* ------------------------------------------------------------ */
55     public String JavaDoc getName()
56     {
57         if (_name==null)
58         {
59             _name=this.getClass().getName();
60             if (!log.isDebugEnabled())
61                 _name=_name.substring(_name.lastIndexOf('.')+1);
62         }
63         return _name;
64     }
65     
66     /* ------------------------------------------------------------ */
67     public HttpContext getHttpContext()
68     {
69         return _context;
70     }
71     
72     /* ------------------------------------------------------------ */
73     /** Initialize with a HttpContext.
74      * Called by addHandler methods of HttpContext.
75      * @param context Must be the HttpContext of the handler
76      */

77     public void initialize(HttpContext context)
78     {
79         if (_context==null)
80             _context=context;
81         else if (_context!=context)
82             throw new IllegalStateException JavaDoc("Can't initialize handler for different context");
83     }
84     
85     /* ----------------------------------------------------------------- */
86     public void start()
87         throws Exception JavaDoc
88     {
89         if (_context==null)
90             throw new IllegalStateException JavaDoc("No context for "+this);
91         _started=true;
92         if(log.isDebugEnabled())log.debug("Started "+this);
93     }
94     
95     /* ----------------------------------------------------------------- */
96     public void stop()
97         throws InterruptedException JavaDoc
98     {
99         _started=false;
100         if(log.isDebugEnabled())log.debug("Stopped "+this);
101     }
102
103     /* ----------------------------------------------------------------- */
104     public boolean isStarted()
105     {
106         return _started;
107     }
108     
109     /* ------------------------------------------------------------ */
110     public String JavaDoc toString()
111     {
112         return getName()+" in "+_context;
113     }
114
115     /* ----------------------------------------------------------------- */
116     public void handleTrace(HttpRequest request,
117                             HttpResponse response)
118         throws IOException JavaDoc
119     {
120         boolean trace=getHttpContext().getHttpServer().getTrace();
121         
122         // Handle TRACE by returning request header
123
response.setField(HttpFields.__ContentType,
124                           HttpFields.__MessageHttp);
125         if (trace)
126         {
127             OutputStream JavaDoc out = response.getOutputStream();
128             ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
129             writer.write(request.toString());
130             writer.flush();
131             response.setIntField(HttpFields.__ContentLength,writer.size());
132             writer.writeTo(out);
133             out.flush();
134         }
135         request.setHandled(true);
136     }
137 }
138
139
140
141
142
Popular Tags