001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /***************************************************************************
018: * Description: Base http request object. *
019: * Author: Keving Seguin [seguin@apache.org] *
020: * Version: $Revision: 1.6 $ *
021: ***************************************************************************/package org.apache.tomcat.util.http;
022:
023: import java.io.PrintWriter;
024: import java.io.StringWriter;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028:
029: import org.apache.tomcat.util.buf.MessageBytes;
030:
031: /**
032: * A general-purpose object for representing an HTTP
033: * request.
034: */
035: public class BaseRequest {
036:
037: // scheme constants
038: public static final String SCHEME_HTTP = "http";
039: public static final String SCHEME_HTTPS = "https";
040:
041: // request attributes
042: MessageBytes method = new MessageBytes();
043: MessageBytes protocol = new MessageBytes();
044: MessageBytes requestURI = new MessageBytes();
045: MessageBytes remoteAddr = new MessageBytes();
046: MessageBytes remoteHost = new MessageBytes();
047: MessageBytes serverName = new MessageBytes();
048: int serverPort = 80;
049: MessageBytes remoteUser = new MessageBytes();
050: MessageBytes authType = new MessageBytes();
051: MessageBytes queryString = new MessageBytes();
052: MessageBytes authorization = new MessageBytes();
053: String scheme = SCHEME_HTTP;
054: boolean secure = false;
055: int contentLength = 0;
056: MessageBytes contentType = new MessageBytes();
057: MimeHeaders headers = new MimeHeaders();
058: Cookies cookies = new Cookies();
059: HashMap attributes = new HashMap();
060:
061: MessageBytes tomcatInstanceId = new MessageBytes();
062:
063: /**
064: * Recycles this object and readies it further use.
065: */
066: public void recycle() {
067: method.recycle();
068: protocol.recycle();
069: requestURI.recycle();
070: remoteAddr.recycle();
071: remoteHost.recycle();
072: serverName.recycle();
073: serverPort = 80;
074: remoteUser.recycle();
075: authType.recycle();
076: queryString.recycle();
077: authorization.recycle();
078: scheme = SCHEME_HTTP;
079: secure = false;
080: contentLength = 0;
081: contentType.recycle();
082: headers.recycle();
083: cookies.recycle();
084: attributes.clear();
085: tomcatInstanceId.recycle();
086: }
087:
088: /**
089: * Get the method.
090: * @return the method
091: */
092: public MessageBytes method() {
093: return method;
094: }
095:
096: /**
097: * Get the protocol
098: * @return the protocol
099: */
100: public MessageBytes protocol() {
101: return protocol;
102: }
103:
104: /**
105: * Get the request uri
106: * @return the request uri
107: */
108: public MessageBytes requestURI() {
109: return requestURI;
110: }
111:
112: /**
113: * Get the remote address
114: * @return the remote address
115: */
116: public MessageBytes remoteAddr() {
117: return remoteAddr;
118: }
119:
120: /**
121: * Get the remote host
122: * @return the remote host
123: */
124: public MessageBytes remoteHost() {
125: return remoteHost;
126: }
127:
128: /**
129: * Get the server name
130: * @return the server name
131: */
132: public MessageBytes serverName() {
133: return serverName;
134: }
135:
136: /**
137: * Get the server port
138: * @return the server port
139: */
140: public int getServerPort() {
141: return serverPort;
142: }
143:
144: /**
145: * Set the server port
146: * @param i the server port
147: */
148: public void setServerPort(int i) {
149: serverPort = i;
150: }
151:
152: /**
153: * Get the remote user
154: * @return the remote user
155: */
156: public MessageBytes remoteUser() {
157: return remoteUser;
158: }
159:
160: /**
161: * Get the auth type
162: * @return the auth type
163: */
164: public MessageBytes authType() {
165: return authType;
166: }
167:
168: /**
169: * Get the query string
170: * @return the query string
171: */
172: public MessageBytes queryString() {
173: return queryString;
174: }
175:
176: /**
177: * Get the authorization credentials
178: * @return the authorization credentials
179: */
180: public MessageBytes authorization() {
181: return authorization;
182: }
183:
184: /**
185: * Get the scheme
186: * @return the scheme
187: */
188: public String getScheme() {
189: return scheme;
190: }
191:
192: /**
193: * Set the scheme.
194: * @param s the scheme
195: */
196: public void setScheme(String s) {
197: scheme = s;
198: }
199:
200: /**
201: * Get whether the request is secure or not.
202: * @return <code>true</code> if the request is secure.
203: */
204: public boolean getSecure() {
205: return secure;
206: }
207:
208: /**
209: * Set whether the request is secure or not.
210: * @param b <code>true</code> if the request is secure.
211: */
212: public void setSecure(boolean b) {
213: secure = b;
214: }
215:
216: /**
217: * Get the content length
218: * @return the content length
219: */
220: public int getContentLength() {
221: return contentLength;
222: }
223:
224: /**
225: * Set the content length
226: * @param i the content length
227: */
228: public void setContentLength(int i) {
229: contentLength = i;
230: }
231:
232: /**
233: * Get the content type
234: * @return the content type
235: */
236: public MessageBytes contentType() {
237: return contentType;
238: }
239:
240: /**
241: * Get this request's headers
242: * @return request headers
243: */
244: public MimeHeaders headers() {
245: return headers;
246: }
247:
248: /**
249: * Get cookies.
250: * @return request cookies.
251: */
252: public Cookies cookies() {
253: return cookies;
254: }
255:
256: /**
257: * Set an attribute on the request
258: * @param name attribute name
259: * @param value attribute value
260: */
261: public void setAttribute(String name, Object value) {
262: if (name == null || value == null) {
263: return;
264: }
265: attributes.put(name, value);
266: }
267:
268: /**
269: * Get an attribute on the request
270: * @param name attribute name
271: * @return attribute value
272: */
273: public Object getAttribute(String name) {
274: if (name == null) {
275: return null;
276: }
277:
278: return attributes.get(name);
279: }
280:
281: /**
282: * Get iterator over attribute names
283: * @return iterator over attribute names
284: */
285: public Iterator getAttributeNames() {
286: return attributes.keySet().iterator();
287: }
288:
289: /**
290: * Get the host id ( or jvmRoute )
291: * @return the jvm route
292: */
293: public MessageBytes instanceId() {
294: return tomcatInstanceId;
295: }
296:
297: // backward compat - jvmRoute is the id of this tomcat instance,
298: // used by a load balancer on the server side to implement sticky
299: // sessions, and on the tomcat side to format the session ids.
300: public MessageBytes jvmRoute() {
301: return tomcatInstanceId;
302: }
303:
304: private Object notes[] = new Object[16];
305:
306: public final Object getNote(int id) {
307: return notes[id];
308: }
309:
310: public final void setNote(int id, Object cr) {
311: notes[id] = cr;
312: }
313:
314: /**
315: * ** SLOW ** for debugging only!
316: */
317: public String toString() {
318: StringWriter sw = new StringWriter();
319: PrintWriter pw = new PrintWriter(sw);
320:
321: pw.println("=== BaseRequest ===");
322: pw.println("method = " + method.toString());
323: pw.println("protocol = " + protocol.toString());
324: pw.println("requestURI = " + requestURI.toString());
325: pw.println("remoteAddr = " + remoteAddr.toString());
326: pw.println("remoteHost = " + remoteHost.toString());
327: pw.println("serverName = " + serverName.toString());
328: pw.println("serverPort = " + serverPort);
329: pw.println("remoteUser = " + remoteUser.toString());
330: pw.println("authType = " + authType.toString());
331: pw.println("queryString = " + queryString.toString());
332: pw.println("scheme = " + scheme.toString());
333: pw.println("secure = " + secure);
334: pw.println("contentLength = " + contentLength);
335: pw.println("contentType = " + contentType);
336: pw.println("attributes = " + attributes.toString());
337: pw.println("headers = " + headers.toString());
338: pw.println("cookies = " + cookies.toString());
339: pw.println("jvmRoute = " + tomcatInstanceId.toString());
340: return sw.toString();
341: }
342:
343: }
|