KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > Request


1 /*
2  * @(#)Request.java 0.3-2 18/06/1999
3  *
4  * This file is part of the HTTPClient package
5  * Copyright (C) 1996-1999 Ronald Tschalär
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307, USA
21  *
22  * For questions, suggestions, bug-reports, enhancement-requests etc.
23  * I may be contacted at:
24  *
25  * ronald@innovation.ch
26  *
27  */

28
29 package HTTPClient;
30
31
32 /**
33  * This class represents an http request. It's used by classes which
34  * implement the HTTPClientModule interface.
35  *
36  * @version 0.3-2 18/06/1999
37  * @author Ronald Tschalär
38  */

39
40 public final class Request implements RoRequest
41 {
42     /** null headers */
43     private static final NVPair[] empty = new NVPair[0];
44
45     /** the current HTTPConnection */
46     private HTTPConnection connection;
47
48     /** the request method to be used (e.g. GET, POST, etc) */
49     private String JavaDoc method;
50
51     /** the request-uri */
52     private String JavaDoc req_uri;
53
54     /** the headers to be used */
55     private NVPair[] headers;
56
57     /** the entity (if any) */
58     private byte[] data;
59
60     /** or an output stream on which the entity will be written */
61     private HttpOutputStream stream;
62
63     /** are modules allowed to popup windows or otherwise prompt user? */
64     private boolean allow_ui;
65
66     /** number of millisecs to wait for an error from the server before sending
67     the entity (used when retrying requests) */

68             long delay_entity = 0;
69
70     /** number of retries so far */
71             int num_retries = 0;
72
73     /** disable pipelining of following request */
74             boolean dont_pipeline = false;
75
76     /** was this request aborted by the user? */
77             boolean aborted = false;
78
79     /** is this an internally generated subrequest? */
80             boolean internal_subrequest = false;
81
82
83     // Constructors
84

85     /**
86      * Creates a new request structure.
87      *
88      * @param con the current HTTPConnection
89      * @param method the request method
90      * @param req_uri the request-uri
91      * @param headers the request headers
92      * @param data the entity as a byte[]
93      * @param stream the entity as a stream
94      * @param allow_ui allow user interaction
95      */

96     public Request(HTTPConnection con, String JavaDoc method, String JavaDoc req_uri,
97            NVPair[] headers, byte[] data, HttpOutputStream stream,
98            boolean allow_ui)
99     {
100     this.connection = con;
101     this.method = method;
102     setRequestURI(req_uri);
103     setHeaders(headers);
104     this.data = data;
105     this.stream = stream;
106     this.allow_ui = allow_ui;
107     }
108
109
110     // Methods
111

112     /**
113      * @return the HTTPConnection this request is associated with
114      */

115     public HTTPConnection getConnection()
116     {
117     return connection;
118     }
119
120     /**
121      * @param con the HTTPConnection this request is associated with
122      */

123     public void setConnection(HTTPConnection con)
124     {
125     this.connection = con;
126     }
127
128
129     /**
130      * @return the request method
131      */

132     public String JavaDoc getMethod()
133     {
134     return method;
135     }
136
137     /**
138      * @param method the request method (e.g. GET, POST, etc)
139      */

140     public void setMethod(String JavaDoc method)
141     {
142     this.method = method;
143     }
144
145
146     /**
147      * @return the request-uri
148      */

149     public String JavaDoc getRequestURI()
150     {
151     return req_uri;
152     }
153
154     /**
155      * @param req_uri the request-uri
156      */

157     public void setRequestURI(String JavaDoc req_uri)
158     {
159     if (req_uri != null && req_uri.trim().length() > 0)
160     {
161         req_uri = req_uri.trim();
162         if (req_uri.charAt(0) != '/' && !req_uri.equals("*"))
163         req_uri = "/" + req_uri;
164         this.req_uri = req_uri;
165     }
166     else
167         this.req_uri = "/";
168     }
169
170
171     /**
172      * @return the headers making up this request
173      */

174     public NVPair[] getHeaders()
175     {
176     return headers;
177     }
178
179     /**
180      * @param headers the headers for this request
181      */

182     public void setHeaders(NVPair[] headers)
183     {
184     if (headers != null)
185         this.headers = headers;
186     else
187         this.headers = empty;
188     }
189
190
191     /**
192      * @return the body of this request
193      */

194     public byte[] getData()
195     {
196     return data;
197     }
198
199     /**
200      * @param data the entity for this request
201      */

202     public void setData(byte[] data)
203     {
204     this.data = data;
205     }
206
207
208     /**
209      * @return the output stream on which the body is written
210      */

211     public HttpOutputStream getStream()
212     {
213     return stream;
214     }
215
216     /**
217      * @param stream an output stream on which the entity is written
218      */

219     public void setStream(HttpOutputStream stream)
220     {
221     this.stream = stream;
222     }
223
224
225     /**
226      * @return true if the modules or handlers for this request may popup
227      * windows or otherwise interact with the user
228      */

229     public boolean allowUI()
230     {
231     return allow_ui;
232     }
233
234     /**
235      * @param allow_ui are modules and handlers allowed to popup windows or
236      * otherwise interact with the user?
237      */

238     public void setAllowUI(boolean allow_ui)
239     {
240     this.allow_ui = allow_ui;
241     }
242
243
244     /**
245      * @return a string containing the method and request-uri
246      */

247     public String JavaDoc toString()
248     {
249     return getClass().getName() + ": " + method + " " + req_uri;
250     }
251 }
252
253
Popular Tags