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