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