001: /*
002: * @(#)Request.java 0.3-2E 16/06/2000
003: *
004: * This file is part of the HTTPClient package
005: * Copyright (C) 1996-2000 Ronald Tschalär
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free
019: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
020: * MA 02111-1307, USA
021: *
022: * For questions, suggestions, bug-reports, enhancement-requests etc.
023: * I may be contacted at:
024: *
025: * ronald@innovation.ch
026: *
027: */
028:
029: package HTTPClient;
030:
031: /**
032: * This class represents an http request. It's used by classes which
033: * implement the HTTPClientModule interface.
034: *
035: * @version 0.3-2E 16/06/2000
036: * @author Ronald Tschalär
037: */
038:
039: public final class Request implements RoRequest {
040: /** null headers */
041: private static final NVPair[] empty = new NVPair[0];
042:
043: /** the current HTTPConnection */
044: private HTTPConnection connection;
045:
046: /** the request method to be used (e.g. GET, POST, etc) */
047: private String method;
048:
049: /** the request-uri */
050: private String req_uri;
051:
052: /** the headers to be used */
053: private NVPair[] headers;
054:
055: /** the entity (if any) */
056: private byte[] data;
057:
058: /** or an output stream on which the entity will be written */
059: private HttpOutputStream stream;
060:
061: /** are modules allowed to popup windows or otherwise prompt user? */
062: private boolean allow_ui;
063:
064: /** number of millisecs to wait for an error from the server before sending
065: the entity (used when retrying requests) */
066: long delay_entity = 0;
067:
068: /** number of retries so far */
069: int num_retries = 0;
070:
071: /** disable pipelining of following request */
072: boolean dont_pipeline = false;
073:
074: /** was this request aborted by the user? */
075: boolean aborted = false;
076:
077: /** is this an internally generated subrequest? */
078: boolean internal_subrequest = false;
079:
080: // Constructors
081:
082: /**
083: * Creates a new request structure.
084: *
085: * @param con the current HTTPConnection
086: * @param method the request method
087: * @param req_uri the request-uri
088: * @param headers the request headers
089: * @param data the entity as a byte[]
090: * @param stream the entity as a stream
091: * @param allow_ui allow user interaction
092: */
093: public Request(HTTPConnection con, String method, String req_uri,
094: NVPair[] headers, byte[] data, HttpOutputStream stream,
095: boolean allow_ui) {
096: this .connection = con;
097: this .method = method;
098: setRequestURI(req_uri);
099: setHeaders(headers);
100: this .data = data;
101: this .stream = stream;
102: this .allow_ui = allow_ui;
103: }
104:
105: // Methods
106:
107: /**
108: * @return the HTTPConnection this request is associated with
109: */
110: public HTTPConnection getConnection() {
111: return connection;
112: }
113:
114: /**
115: * @param con the HTTPConnection this request is associated with
116: */
117: public void setConnection(HTTPConnection con) {
118: this .connection = con;
119: }
120:
121: /**
122: * @return the request method
123: */
124: public String getMethod() {
125: return method;
126: }
127:
128: /**
129: * @param method the request method (e.g. GET, POST, etc)
130: */
131: public void setMethod(String method) {
132: this .method = method;
133: }
134:
135: /**
136: * @return the request-uri
137: */
138: public String getRequestURI() {
139: return req_uri;
140: }
141:
142: /**
143: * @param req_uri the request-uri
144: */
145: public void setRequestURI(String req_uri) {
146: if (req_uri != null && req_uri.trim().length() > 0) {
147: req_uri = req_uri.trim();
148: if (req_uri.charAt(0) != '/' && !req_uri.equals("*")
149: && !method.equals("CONNECT"))
150: req_uri = "/" + req_uri;
151: this .req_uri = req_uri;
152: } else
153: this .req_uri = "/";
154: }
155:
156: /**
157: * @return the headers making up this request
158: */
159: public NVPair[] getHeaders() {
160: return headers;
161: }
162:
163: /**
164: * @param headers the headers for this request
165: */
166: public void setHeaders(NVPair[] headers) {
167: if (headers != null)
168: this .headers = headers;
169: else
170: this .headers = empty;
171: }
172:
173: /**
174: * @return the body of this request
175: */
176: public byte[] getData() {
177: return data;
178: }
179:
180: /**
181: * @param data the entity for this request
182: */
183: public void setData(byte[] data) {
184: this .data = data;
185: }
186:
187: /**
188: * @return the output stream on which the body is written
189: */
190: public HttpOutputStream getStream() {
191: return stream;
192: }
193:
194: /**
195: * @param stream an output stream on which the entity is written
196: */
197: public void setStream(HttpOutputStream stream) {
198: this .stream = stream;
199: }
200:
201: /**
202: * @return true if the modules or handlers for this request may popup
203: * windows or otherwise interact with the user
204: */
205: public boolean allowUI() {
206: return allow_ui;
207: }
208:
209: /**
210: * @param allow_ui are modules and handlers allowed to popup windows or
211: * otherwise interact with the user?
212: */
213: public void setAllowUI(boolean allow_ui) {
214: this .allow_ui = allow_ui;
215: }
216:
217: /**
218: * @return a string containing the method and request-uri
219: */
220: public String toString() {
221: return getClass().getName() + ": " + method + " " + req_uri;
222: }
223: }
|