|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
HttpURLConnection.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.util;
|
|
58 |
|
|
59 |
import java.io.IOException;
|
|
60 |
import java.io.InputStream;
|
|
61 |
import java.io.OutputStream;
|
|
62 |
|
|
63 |
import java.net.ProtocolException;
|
|
64 |
import java.net.URL;
|
|
65 |
|
|
66 |
import java.security.Permission;
|
|
67 |
|
|
68 |
import org.apache.commons.httpclient.Header;
|
|
69 |
import org.apache.commons.httpclient.HttpMethod;
|
|
70 |
import org.apache.commons.httpclient.HttpMethodBase;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Provides a <code>HttpURLConnection</code> wrapper around HttpClient
|
|
74 |
* <code>HttpMethod</code>. This allows existing code to easily switch to
|
|
75 |
* HttpClieht without breaking existing interfaces using the JDK
|
|
76 |
* <code>HttpURLConnection<code>.
|
|
77 |
*
|
|
78 |
* Note: It is a best try effort as different version of the JDK have different
|
|
79 |
* behaviours for <code>HttpURLConnection</code> (And I'm not even including
|
|
80 |
* the numerous <code>HttpURLConnection</code> bugs!).
|
|
81 |
*
|
|
82 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
83 |
*
|
|
84 |
* @deprecated This class has been donated to the
|
|
85 |
* <a href="http://jakarta.apache.org/commons/httpclient/">Jakarta
|
|
86 |
* Commons-HttpClient</a> project, and is now maintained there as
|
|
87 |
* <code>org.apache.commons.httpclient.util.HttpURLConnection</code>.
|
|
88 |
*
|
|
89 |
* @version $Id: HttpURLConnection.java,v 1.6 2003/05/26 11:45:22 cmlenz Exp $
|
|
90 |
*/
|
|
91 |
public class HttpURLConnection extends java.net.HttpURLConnection |
|
92 |
{ |
|
93 |
/**
|
|
94 |
* The <code>HttpMethod</code> object that was used to connect to the
|
|
95 |
* HTTP server. It contains all the returned data.
|
|
96 |
*/
|
|
97 |
private HttpMethod method;
|
|
98 |
|
|
99 |
/**
|
|
100 |
* The URL to which we are connected
|
|
101 |
*/
|
|
102 |
private URL url;
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Creates an <code>HttpURLConnection</code> from a
|
|
106 |
* <code>HttpMethod</code>.
|
|
107 |
*
|
|
108 |
* @param theMethod the theMethod that was used to connect to the HTTP
|
|
109 |
* server and which contains the returned data.
|
|
110 |
* @param theURL the URL to which we are connected (includes query string)
|
|
111 |
*/
|
|
112 | 0 |
public HttpURLConnection(HttpMethod theMethod, URL theURL)
|
113 |
{ |
|
114 | 0 |
super(theURL);
|
115 | 0 |
this.method = theMethod;
|
116 | 0 |
this.url = theURL;
|
117 |
} |
|
118 |
|
|
119 |
/**
|
|
120 |
* @see java.net.HttpURLConnection#HttpURLConnection(URL)
|
|
121 |
*/
|
|
122 | 0 |
protected HttpURLConnection(URL theURL)
|
123 |
{ |
|
124 | 0 |
super(theURL);
|
125 | 0 |
throw new RuntimeException("An HTTP URL connection can only be " |
126 |
+ "constructed from a HttpMethod class");
|
|
127 |
} |
|
128 |
|
|
129 |
/**
|
|
130 |
* @see java.net.HttpURLConnection#getInputStream()
|
|
131 |
*/
|
|
132 | 0 |
public InputStream getInputStream() throws IOException |
133 |
{ |
|
134 | 0 |
return this.method.getResponseBodyAsStream(); |
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* @see java.net.HttpURLConnection#getErrorStream()
|
|
139 |
*/
|
|
140 | 0 |
public InputStream getErrorStream()
|
141 |
{ |
|
142 | 0 |
throw new RuntimeException("Not implemented yet"); |
143 |
} |
|
144 |
|
|
145 |
/**
|
|
146 |
* @see java.net.HttpURLConnection#disconnect()
|
|
147 |
*/
|
|
148 | 0 |
public void disconnect() |
149 |
{ |
|
150 | 0 |
throw new RuntimeException("Not implemented yet"); |
151 |
} |
|
152 |
|
|
153 |
/**
|
|
154 |
* @see java.net.HttpURLConnection#connect()
|
|
155 |
*/
|
|
156 | 0 |
public void connect() throws IOException |
157 |
{ |
|
158 | 0 |
throw new RuntimeException("This class can only be used with already" |
159 |
+ "retrieved data");
|
|
160 |
} |
|
161 |
|
|
162 |
/**
|
|
163 |
* @see java.net.HttpURLConnection#usingProxy()
|
|
164 |
*/
|
|
165 | 0 |
public boolean usingProxy() |
166 |
{ |
|
167 | 0 |
throw new RuntimeException("Not implemented yet"); |
168 |
} |
|
169 |
|
|
170 |
/**
|
|
171 |
* @see java.net.HttpURLConnection#getRequestMethod()
|
|
172 |
*/
|
|
173 | 0 |
public String getRequestMethod()
|
174 |
{ |
|
175 | 0 |
return this.method.getName(); |
176 |
} |
|
177 |
|
|
178 |
/**
|
|
179 |
* @see java.net.HttpURLConnection#getResponseCode()
|
|
180 |
*/
|
|
181 | 0 |
public int getResponseCode() throws IOException |
182 |
{ |
|
183 | 0 |
return this.method.getStatusCode(); |
184 |
} |
|
185 |
|
|
186 |
/**
|
|
187 |
* @see java.net.HttpURLConnection#getResponseMessage()
|
|
188 |
*/
|
|
189 | 0 |
public String getResponseMessage() throws IOException |
190 |
{ |
|
191 | 0 |
return this.method.getStatusText(); |
192 |
} |
|
193 |
|
|
194 |
/**
|
|
195 |
* @see java.net.HttpURLConnection#getHeaderField(String)
|
|
196 |
*/
|
|
197 | 0 |
public String getHeaderField(String theName)
|
198 |
{ |
|
199 |
// Note: Return the last matching header in the Header[] array, as in
|
|
200 |
// the JDK implementation.
|
|
201 |
|
|
202 | 0 |
Header[] headers = this.method.getResponseHeaders();
|
203 |
|
|
204 | 0 |
for (int i = headers.length - 1; i >= 0; i--) |
205 |
{ |
|
206 | 0 |
if (headers[i].getName().equalsIgnoreCase(theName))
|
207 |
{ |
|
208 | 0 |
return headers[i].getValue();
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 | 0 |
return null; |
213 |
} |
|
214 |
|
|
215 |
/**
|
|
216 |
* @see java.net.HttpURLConnection#getHeaderFieldKey(int)
|
|
217 |
*/
|
|
218 | 0 |
public String getHeaderFieldKey(int theKeyPosition) |
219 |
{ |
|
220 |
// Note: HttpClient does not consider the returned Status Line as
|
|
221 |
// a response header. However, getHeaderFieldKey(0) is supposed to
|
|
222 |
// return null. Hence the special case below ...
|
|
223 |
|
|
224 | 0 |
if (theKeyPosition == 0)
|
225 |
{ |
|
226 | 0 |
return null; |
227 |
} |
|
228 |
|
|
229 |
// Note: I hope the header fields are kept in the correct order when
|
|
230 |
// calling getRequestHeaders.
|
|
231 | 0 |
Header[] headers = this.method.getResponseHeaders();
|
232 |
|
|
233 | 0 |
if ((theKeyPosition < 0) || (theKeyPosition >= headers.length))
|
234 |
{ |
|
235 | 0 |
return null; |
236 |
} |
|
237 |
|
|
238 | 0 |
return headers[theKeyPosition - 1].getName();
|
239 |
} |
|
240 |
|
|
241 |
/**
|
|
242 |
* @see java.net.HttpURLConnection#getHeaderField(int)
|
|
243 |
*/
|
|
244 | 0 |
public String getHeaderField(int thePosition) |
245 |
{ |
|
246 |
// Note: HttpClient does not consider the returned Status Line as
|
|
247 |
// a response header. However, getHeaderField(0) is supposed to
|
|
248 |
// return the status line. Hence the special case below ...
|
|
249 |
|
|
250 | 0 |
if (thePosition == 0)
|
251 |
{ |
|
252 | 0 |
if (((HttpMethodBase) this.method).isHttp11()) |
253 |
{ |
|
254 | 0 |
return "HTTP/1.1 " + this.method.getStatusCode() |
255 |
+ " " + this.method.getStatusText(); |
|
256 |
} |
|
257 |
else
|
|
258 |
{ |
|
259 | 0 |
return "HTTP/1.0 " + this.method.getStatusCode() |
260 |
+ " " + this.method.getStatusText(); |
|
261 |
} |
|
262 |
} |
|
263 |
|
|
264 |
// Note: I hope the header fields are kept in the correct order when
|
|
265 |
// calling getRequestHeaders.
|
|
266 | 0 |
Header[] headers = this.method.getResponseHeaders();
|
267 |
|
|
268 | 0 |
if ((thePosition < 0) || (thePosition >= headers.length))
|
269 |
{ |
|
270 | 0 |
return null; |
271 |
} |
|
272 |
|
|
273 | 0 |
return headers[thePosition - 1].getValue();
|
274 |
} |
|
275 |
|
|
276 |
/**
|
|
277 |
* @see java.net.HttpURLConnection#getURL()
|
|
278 |
*/
|
|
279 | 0 |
public URL getURL()
|
280 |
{ |
|
281 | 0 |
return this.url; |
282 |
} |
|
283 |
|
|
284 |
// Note: We don't implement the following methods so that they default to
|
|
285 |
// the JDK implementation. They will all call
|
|
286 |
// <code>getHeaderField(String)</code> which we have overridden.
|
|
287 |
// java.net.HttpURLConnection#getHeaderFieldDate(String, long)
|
|
288 |
// java.net.HttpURLConnection#getContentLength()
|
|
289 |
// java.net.HttpURLConnection#getContentType()
|
|
290 |
// java.net.HttpURLConnection#getContentEncoding()
|
|
291 |
// java.net.HttpURLConnection#getDate()
|
|
292 |
// java.net.HttpURLConnection#getHeaderFieldInt(String, int)
|
|
293 |
// java.net.HttpURLConnection#getExpiration()
|
|
294 |
// java.net.HttpURLConnection#getLastModified()
|
|
295 |
|
|
296 |
/**
|
|
297 |
* @see java.net.HttpURLConnection#setInstanceFollowRedirects(boolean)
|
|
298 |
*/
|
|
299 | 0 |
public void setInstanceFollowRedirects(boolean isFollowingRedirects) |
300 |
{ |
|
301 | 0 |
throw new RuntimeException("This class can only be used with already" |
302 |
+ "retrieved data");
|
|
303 |
} |
|
304 |
|
|
305 |
/**
|
|
306 |
* @see java.net.HttpURLConnection#getInstanceFollowRedirects()
|
|
307 |
*/
|
|
308 | 0 |
public boolean getInstanceFollowRedirects() |
309 |
{ |
|
310 | 0 |
throw new RuntimeException("Not implemented yet"); |
311 |
} |
|
312 |
|
|
313 |
/**
|
|
314 |
* @see java.net.HttpURLConnection#setRequestMethod(String)
|
|
315 |
*/
|
|
316 | 0 |
public void setRequestMethod(String theMethod) throws ProtocolException |
317 |
{ |
|
318 | 0 |
throw new RuntimeException("This class can only be used with already" |
319 |
+ "retrieved data");
|
|
320 |
} |
|
321 |
|
|
322 |
/**
|
|
323 |
* @see java.net.HttpURLConnection#getPermission()
|
|
324 |
*/
|
|
325 | 0 |
public Permission getPermission() throws IOException |
326 |
{ |
|
327 | 0 |
throw new RuntimeException("Not implemented yet"); |
328 |
} |
|
329 |
|
|
330 |
/**
|
|
331 |
* @see java.net.HttpURLConnection#getContent()
|
|
332 |
*/
|
|
333 | 0 |
public Object getContent() throws IOException |
334 |
{ |
|
335 | 0 |
throw new RuntimeException("Not implemented yet"); |
336 |
} |
|
337 |
|
|
338 |
/**
|
|
339 |
* @see java.net.HttpURLConnection#getContent(Class[])
|
|
340 |
*/
|
|
341 | 0 |
public Object getContent(Class[] theClasses) throws IOException |
342 |
{ |
|
343 | 0 |
throw new RuntimeException("Not implemented yet"); |
344 |
} |
|
345 |
|
|
346 |
/**
|
|
347 |
* @see java.net.HttpURLConnection#getOutputStream()
|
|
348 |
*/
|
|
349 | 0 |
public OutputStream getOutputStream() throws IOException |
350 |
{ |
|
351 | 0 |
throw new RuntimeException("This class can only be used with already" |
352 |
+ "retrieved data");
|
|
353 |
} |
|
354 |
|
|
355 |
/**
|
|
356 |
* @see java.net.HttpURLConnection#setDoInput(boolean)
|
|
357 |
*/
|
|
358 | 0 |
public void setDoInput(boolean isInput) |
359 |
{ |
|
360 | 0 |
throw new RuntimeException("This class can only be used with already" |
361 |
+ "retrieved data");
|
|
362 |
} |
|
363 |
|
|
364 |
/**
|
|
365 |
* @see java.net.HttpURLConnection#getDoInput()
|
|
366 |
*/
|
|
367 | 0 |
public boolean getDoInput() |
368 |
{ |
|
369 | 0 |
throw new RuntimeException("Not implemented yet"); |
370 |
} |
|
371 |
|
|
372 |
/**
|
|
373 |
* @see java.net.HttpURLConnection#setDoOutput(boolean)
|
|
374 |
*/
|
|
375 | 0 |
public void setDoOutput(boolean isOutput) |
376 |
{ |
|
377 | 0 |
throw new RuntimeException("This class can only be used with already" |
378 |
+ "retrieved data");
|
|
379 |
} |
|
380 |
|
|
381 |
/**
|
|
382 |
* @see java.net.HttpURLConnection#getDoOutput()
|
|
383 |
*/
|
|
384 | 0 |
public boolean getDoOutput() |
385 |
{ |
|
386 | 0 |
throw new RuntimeException("Not implemented yet"); |
387 |
} |
|
388 |
|
|
389 |
/**
|
|
390 |
* @see java.net.HttpURLConnection#setAllowUserInteraction(boolean)
|
|
391 |
*/
|
|
392 | 0 |
public void setAllowUserInteraction(boolean isAllowInteraction) |
393 |
{ |
|
394 | 0 |
throw new RuntimeException("This class can only be used with already" |
395 |
+ "retrieved data");
|
|
396 |
} |
|
397 |
|
|
398 |
/**
|
|
399 |
* @see java.net.HttpURLConnection#getAllowUserInteraction()
|
|
400 |
*/
|
|
401 | 0 |
public boolean getAllowUserInteraction() |
402 |
{ |
|
403 | 0 |
throw new RuntimeException("Not implemented yet"); |
404 |
} |
|
405 |
|
|
406 |
/**
|
|
407 |
* @see java.net.HttpURLConnection#setUseCaches(boolean)
|
|
408 |
*/
|
|
409 | 0 |
public void setUseCaches(boolean isUsingCaches) |
410 |
{ |
|
411 | 0 |
throw new RuntimeException("This class can only be used with already" |
412 |
+ "retrieved data");
|
|
413 |
} |
|
414 |
|
|
415 |
/**
|
|
416 |
* @see java.net.HttpURLConnection#getUseCaches()
|
|
417 |
*/
|
|
418 | 0 |
public boolean getUseCaches() |
419 |
{ |
|
420 | 0 |
throw new RuntimeException("Not implemented yet"); |
421 |
} |
|
422 |
|
|
423 |
/**
|
|
424 |
* @see java.net.HttpURLConnection#setIfModifiedSince(long)
|
|
425 |
*/
|
|
426 | 0 |
public void setIfModifiedSince(long theModificationDate) |
427 |
{ |
|
428 | 0 |
throw new RuntimeException("This class can only be used with already" |
429 |
+ "retrieved data");
|
|
430 |
} |
|
431 |
|
|
432 |
/**
|
|
433 |
* @see java.net.HttpURLConnection#getIfModifiedSince()
|
|
434 |
*/
|
|
435 | 0 |
public long getIfModifiedSince() |
436 |
{ |
|
437 | 0 |
throw new RuntimeException("Not implemented yet"); |
438 |
} |
|
439 |
|
|
440 |
/**
|
|
441 |
* @see java.net.HttpURLConnection#getDefaultUseCaches()
|
|
442 |
*/
|
|
443 | 0 |
public boolean getDefaultUseCaches() |
444 |
{ |
|
445 | 0 |
throw new RuntimeException("Not implemented yet"); |
446 |
} |
|
447 |
|
|
448 |
/**
|
|
449 |
* @see java.net.HttpURLConnection#setDefaultUseCaches(boolean)
|
|
450 |
*/
|
|
451 | 0 |
public void setDefaultUseCaches(boolean isUsingCaches) |
452 |
{ |
|
453 | 0 |
throw new RuntimeException("This class can only be used with already" |
454 |
+ "retrieved data");
|
|
455 |
} |
|
456 |
|
|
457 |
/**
|
|
458 |
* @see java.net.HttpURLConnection#setRequestProperty(String, String)
|
|
459 |
*/
|
|
460 | 0 |
public void setRequestProperty(String theKey, String theValue) |
461 |
{ |
|
462 | 0 |
throw new RuntimeException("This class can only be used with already" |
463 |
+ "retrieved data");
|
|
464 |
} |
|
465 |
|
|
466 |
/**
|
|
467 |
* @see java.net.HttpURLConnection#getRequestProperty(String)
|
|
468 |
*/
|
|
469 | 0 |
public String getRequestProperty(String theKey)
|
470 |
{ |
|
471 | 0 |
throw new RuntimeException("Not implemented yet"); |
472 |
} |
|
473 |
} |
|
474 |
|
|