KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > BaseRequest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /***************************************************************************
19  * Description: Base http request object. *
20  * Author: Keving Seguin [seguin@apache.org] *
21  * Version: $Revision: 467222 $ *
22  ***************************************************************************/

23
24 package org.apache.tomcat.util.http;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.apache.tomcat.util.buf.MessageBytes;
33
34 /**
35  * A general-purpose object for representing an HTTP
36  * request.
37  */

38 public class BaseRequest {
39
40     // scheme constants
41
public static final String JavaDoc SCHEME_HTTP = "http";
42     public static final String JavaDoc SCHEME_HTTPS = "https";
43
44     // request attributes
45
MessageBytes method = MessageBytes.newInstance();
46     MessageBytes protocol = MessageBytes.newInstance();
47     MessageBytes requestURI = MessageBytes.newInstance();
48     MessageBytes remoteAddr = MessageBytes.newInstance();
49     MessageBytes remoteHost = MessageBytes.newInstance();
50     MessageBytes serverName = MessageBytes.newInstance();
51     int serverPort = 80;
52     MessageBytes remoteUser = MessageBytes.newInstance();
53     MessageBytes authType = MessageBytes.newInstance();
54     MessageBytes queryString = MessageBytes.newInstance();
55     MessageBytes authorization = MessageBytes.newInstance();
56     String JavaDoc scheme = SCHEME_HTTP;
57     boolean secure = false;
58     int contentLength = 0;
59     MessageBytes contentType = MessageBytes.newInstance();
60     MimeHeaders headers = new MimeHeaders();
61     Cookies cookies = new Cookies();
62     HashMap JavaDoc attributes = new HashMap JavaDoc();
63
64     MessageBytes tomcatInstanceId = MessageBytes.newInstance();
65     
66     /**
67      * Recycles this object and readies it further use.
68      */

69     public void recycle() {
70         method.recycle();
71         protocol.recycle();
72         requestURI.recycle();
73         remoteAddr.recycle();
74         remoteHost.recycle();
75         serverName.recycle();
76         serverPort = 80;
77         remoteUser.recycle();
78         authType.recycle();
79         queryString.recycle();
80         authorization.recycle();
81         scheme = SCHEME_HTTP;
82         secure = false;
83         contentLength = 0;
84         contentType.recycle();
85         headers.recycle();
86         cookies.recycle();
87         attributes.clear();
88         tomcatInstanceId.recycle();
89     }
90
91     /**
92      * Get the method.
93      * @return the method
94      */

95     public MessageBytes method() {
96         return method;
97     }
98
99     /**
100      * Get the protocol
101      * @return the protocol
102      */

103     public MessageBytes protocol() {
104         return protocol;
105     }
106
107     /**
108      * Get the request uri
109      * @return the request uri
110      */

111     public MessageBytes requestURI() {
112         return requestURI;
113     }
114
115     /**
116      * Get the remote address
117      * @return the remote address
118      */

119     public MessageBytes remoteAddr() {
120         return remoteAddr;
121     }
122
123     /**
124      * Get the remote host
125      * @return the remote host
126      */

127     public MessageBytes remoteHost() {
128         return remoteHost;
129     }
130
131     /**
132      * Get the server name
133      * @return the server name
134      */

135     public MessageBytes serverName() {
136         return serverName;
137     }
138
139     /**
140      * Get the server port
141      * @return the server port
142      */

143     public int getServerPort() {
144         return serverPort;
145     }
146
147     /**
148      * Set the server port
149      * @param i the server port
150      */

151     public void setServerPort(int i) {
152         serverPort = i;
153     }
154
155     /**
156      * Get the remote user
157      * @return the remote user
158      */

159     public MessageBytes remoteUser() {
160         return remoteUser;
161     }
162
163     /**
164      * Get the auth type
165      * @return the auth type
166      */

167     public MessageBytes authType() {
168         return authType;
169     }
170
171     /**
172      * Get the query string
173      * @return the query string
174      */

175     public MessageBytes queryString() {
176         return queryString;
177     }
178
179     /**
180      * Get the authorization credentials
181      * @return the authorization credentials
182      */

183     public MessageBytes authorization() {
184         return authorization;
185     }
186
187     /**
188      * Get the scheme
189      * @return the scheme
190      */

191     public String JavaDoc getScheme() {
192         return scheme;
193     }
194
195     /**
196      * Set the scheme.
197      * @param s the scheme
198      */

199     public void setScheme(String JavaDoc s) {
200         scheme = s;
201     }
202
203     /**
204      * Get whether the request is secure or not.
205      * @return <code>true</code> if the request is secure.
206      */

207     public boolean getSecure() {
208         return secure;
209     }
210
211     /**
212      * Set whether the request is secure or not.
213      * @param b <code>true</code> if the request is secure.
214      */

215     public void setSecure(boolean b) {
216         secure = b;
217     }
218
219     /**
220      * Get the content length
221      * @return the content length
222      */

223     public int getContentLength() {
224         return contentLength;
225     }
226
227     /**
228      * Set the content length
229      * @param i the content length
230      */

231     public void setContentLength(int i) {
232         contentLength = i;
233     }
234
235     /**
236      * Get the content type
237      * @return the content type
238      */

239     public MessageBytes contentType() {
240         return contentType;
241     }
242
243     /**
244      * Get this request's headers
245      * @return request headers
246      */

247     public MimeHeaders headers() {
248         return headers;
249     }
250
251     /**
252      * Get cookies.
253      * @return request cookies.
254      */

255     public Cookies cookies() {
256         return cookies;
257     }
258
259     /**
260      * Set an attribute on the request
261      * @param name attribute name
262      * @param value attribute value
263      */

264     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
265         if (name == null || value == null) {
266             return;
267         }
268         attributes.put(name, value);
269     }
270
271     /**
272      * Get an attribute on the request
273      * @param name attribute name
274      * @return attribute value
275      */

276     public Object JavaDoc getAttribute(String JavaDoc name) {
277         if (name == null) {
278             return null;
279         }
280
281         return attributes.get(name);
282     }
283
284     /**
285      * Get iterator over attribute names
286      * @return iterator over attribute names
287      */

288     public Iterator JavaDoc getAttributeNames() {
289         return attributes.keySet().iterator();
290     }
291
292     /**
293      * Get the host id ( or jvmRoute )
294      * @return the jvm route
295      */

296     public MessageBytes instanceId() {
297         return tomcatInstanceId;
298     }
299
300     // backward compat - jvmRoute is the id of this tomcat instance,
301
// used by a load balancer on the server side to implement sticky
302
// sessions, and on the tomcat side to format the session ids.
303
public MessageBytes jvmRoute() {
304         return tomcatInstanceId;
305     }
306
307     private Object JavaDoc notes[]=new Object JavaDoc[16];
308     
309     public final Object JavaDoc getNote(int id) {
310         return notes[id];
311     }
312
313     public final void setNote(int id, Object JavaDoc cr) {
314         notes[id]=cr;
315     }
316     
317     /**
318      * ** SLOW ** for debugging only!
319      */

320     public String JavaDoc toString() {
321         StringWriter JavaDoc sw = new StringWriter JavaDoc();
322         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
323
324         pw.println("=== BaseRequest ===");
325         pw.println("method = " + method.toString());
326         pw.println("protocol = " + protocol.toString());
327         pw.println("requestURI = " + requestURI.toString());
328         pw.println("remoteAddr = " + remoteAddr.toString());
329         pw.println("remoteHost = " + remoteHost.toString());
330         pw.println("serverName = " + serverName.toString());
331         pw.println("serverPort = " + serverPort);
332         pw.println("remoteUser = " + remoteUser.toString());
333         pw.println("authType = " + authType.toString());
334         pw.println("queryString = " + queryString.toString());
335         pw.println("scheme = " + scheme.toString());
336         pw.println("secure = " + secure);
337         pw.println("contentLength = " + contentLength);
338         pw.println("contentType = " + contentType);
339         pw.println("attributes = " + attributes.toString());
340         pw.println("headers = " + headers.toString());
341         pw.println("cookies = " + cookies.toString());
342         pw.println("jvmRoute = " + tomcatInstanceId.toString());
343         return sw.toString();
344     }
345     
346 }
347
Popular Tags