001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/HttpURLConnection.java,v 1.15 2004/04/18 23:51:38 jsdever Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.util;
032:
033: import org.apache.commons.httpclient.HttpMethod;
034: import org.apache.commons.httpclient.Header;
035:
036: import org.apache.commons.logging.LogFactory;
037: import org.apache.commons.logging.Log;
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.io.OutputStream;
041: import java.net.URL;
042: import java.net.ProtocolException;
043: import java.security.Permission;
044:
045: /**
046: * Provides a <code>HttpURLConnection</code> wrapper around HttpClient's
047: * <code>HttpMethod</code>. This allows existing code to easily switch to
048: * HttpClieht without breaking existing interfaces using the JDK
049: * <code>HttpURLConnection</code>.
050: *
051: * Note 1: The current implementations wraps only a connected
052: * <code>HttpMethod</code>, ie a method that has alreayd been used to connect
053: * to an HTTP server.
054: *
055: * Note 2: It is a best try effort as different version of the JDK have
056: * different behaviours for <code>HttpURLConnection</code> (And I'm not even
057: * including the numerous <code>HttpURLConnection</code> bugs!).
058: *
059: * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
060: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
061: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
062: *
063: * @since 2.0
064: *
065: * @version $Id: HttpURLConnection.java 480424 2006-11-29 05:56:49Z bayard $
066: */
067: public class HttpURLConnection extends java.net.HttpURLConnection {
068:
069: // -------------------------------------------------------- Class Variables
070:
071: /** Log object for this class. */
072: private static final Log LOG = LogFactory
073: .getLog(HttpURLConnection.class);
074:
075: // ----------------------------------------------------- Instance Variables
076:
077: /**
078: * The <code>HttpMethod</code> object that was used to connect to the
079: * HTTP server. It contains all the returned data.
080: */
081: private HttpMethod method;
082:
083: /**
084: * The URL to which we are connected
085: */
086: private URL url;
087:
088: // ----------------------------------------------------------- Constructors
089:
090: /**
091: * Creates an <code>HttpURLConnection</code> from a <code>HttpMethod</code>.
092: *
093: * @param method the theMethod that was used to connect to the HTTP
094: * server and which contains the returned data.
095: * @param url the URL to which we are connected (includes query string)
096: */
097: public HttpURLConnection(HttpMethod method, URL url) {
098: super (url);
099: this .method = method;
100: this .url = url;
101: }
102:
103: /**
104: * Create an instance.
105: * @param url The URL.
106: * @see java.net.HttpURLConnection#HttpURLConnection(URL)
107: */
108: protected HttpURLConnection(URL url) {
109: super (url);
110: throw new RuntimeException(
111: "An HTTP URL connection can only be "
112: + "constructed from a HttpMethod class");
113: }
114:
115: // --------------------------------------------------------- Public Methods
116:
117: /**
118: * Gets an input stream for the HttpMethod response body.
119: * @throws IOException If an IO problem occurs.
120: * @return The input stream.
121: * @see java.net.HttpURLConnection#getInputStream()
122: * @see org.apache.commons.httpclient.HttpMethod#getResponseBodyAsStream()
123: */
124: public InputStream getInputStream() throws IOException {
125: LOG.trace("enter HttpURLConnection.getInputStream()");
126: return this .method.getResponseBodyAsStream();
127: }
128:
129: /**
130: * Not yet implemented.
131: * Return the error stream.
132: * @see java.net.HttpURLConnection#getErrorStream()
133: */
134: public InputStream getErrorStream() {
135: LOG.trace("enter HttpURLConnection.getErrorStream()");
136: throw new RuntimeException("Not implemented yet");
137: }
138:
139: /**
140: * Not yet implemented.
141: * @see java.net.HttpURLConnection#disconnect()
142: */
143: public void disconnect() {
144: LOG.trace("enter HttpURLConnection.disconnect()");
145: throw new RuntimeException("Not implemented yet");
146: }
147:
148: /**
149: * Not available: the data must have already been retrieved.
150: * @throws IOException If an IO problem occurs.
151: * @see java.net.HttpURLConnection#connect()
152: */
153: public void connect() throws IOException {
154: LOG.trace("enter HttpURLConnection.connect()");
155: throw new RuntimeException(
156: "This class can only be used with already"
157: + "retrieved data");
158: }
159:
160: /**
161: * Not yet implemented.
162: * @return true if we are using a proxy.
163: * @see java.net.HttpURLConnection#usingProxy()
164: */
165: public boolean usingProxy() {
166: LOG.trace("enter HttpURLConnection.usingProxy()");
167: throw new RuntimeException("Not implemented yet");
168: }
169:
170: /**
171: * Return the request method.
172: * @return The request method.
173: * @see java.net.HttpURLConnection#getRequestMethod()
174: * @see org.apache.commons.httpclient.HttpMethod#getName()
175: */
176: public String getRequestMethod() {
177: LOG.trace("enter HttpURLConnection.getRequestMethod()");
178: return this .method.getName();
179: }
180:
181: /**
182: * Return the response code.
183: * @return The response code.
184: * @throws IOException If an IO problem occurs.
185: * @see java.net.HttpURLConnection#getResponseCode()
186: * @see org.apache.commons.httpclient.HttpMethod#getStatusCode()
187: */
188: public int getResponseCode() throws IOException {
189: LOG.trace("enter HttpURLConnection.getResponseCode()");
190: return this .method.getStatusCode();
191: }
192:
193: /**
194: * Return the response message
195: * @return The response message
196: * @throws IOException If an IO problem occurs.
197: * @see java.net.HttpURLConnection#getResponseMessage()
198: * @see org.apache.commons.httpclient.HttpMethod#getStatusText()
199: */
200: public String getResponseMessage() throws IOException {
201: LOG.trace("enter HttpURLConnection.getResponseMessage()");
202: return this .method.getStatusText();
203: }
204:
205: /**
206: * Return the header field
207: * @param name the name of the header
208: * @return the header field.
209: * @see java.net.HttpURLConnection#getHeaderField(String)
210: * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders()
211: */
212: public String getHeaderField(String name) {
213: LOG.trace("enter HttpURLConnection.getHeaderField(String)");
214: // Note: Return the last matching header in the Header[] array, as in
215: // the JDK implementation.
216: Header[] headers = this .method.getResponseHeaders();
217: for (int i = headers.length - 1; i >= 0; i--) {
218: if (headers[i].getName().equalsIgnoreCase(name)) {
219: return headers[i].getValue();
220: }
221: }
222:
223: return null;
224: }
225:
226: /**
227: * Return the header field key
228: * @param keyPosition The key position
229: * @return The header field key.
230: * @see java.net.HttpURLConnection#getHeaderFieldKey(int)
231: * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders()
232: */
233: public String getHeaderFieldKey(int keyPosition) {
234: LOG.trace("enter HttpURLConnection.getHeaderFieldKey(int)");
235:
236: // Note: HttpClient does not consider the returned Status Line as
237: // a response header. However, getHeaderFieldKey(0) is supposed to
238: // return null. Hence the special case below ...
239:
240: if (keyPosition == 0) {
241: return null;
242: }
243:
244: // Note: HttpClient does not currently keep headers in the same order
245: // that they are read from the HTTP server.
246:
247: Header[] headers = this .method.getResponseHeaders();
248: if (keyPosition < 0 || keyPosition > headers.length) {
249: return null;
250: }
251:
252: return headers[keyPosition - 1].getName();
253: }
254:
255: /**
256: * Return the header field at the specified position
257: * @param position The position
258: * @return The header field.
259: * @see java.net.HttpURLConnection#getHeaderField(int)
260: * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders()
261: */
262: public String getHeaderField(int position) {
263: LOG.trace("enter HttpURLConnection.getHeaderField(int)");
264:
265: // Note: HttpClient does not consider the returned Status Line as
266: // a response header. However, getHeaderField(0) is supposed to
267: // return the status line. Hence the special case below ...
268:
269: if (position == 0) {
270: return this .method.getStatusLine().toString();
271: }
272:
273: // Note: HttpClient does not currently keep headers in the same order
274: // that they are read from the HTTP server.
275:
276: Header[] headers = this .method.getResponseHeaders();
277: if (position < 0 || position > headers.length) {
278: return null;
279: }
280:
281: return headers[position - 1].getValue();
282: }
283:
284: /**
285: * Return the URL
286: * @return The URL.
287: * @see java.net.HttpURLConnection#getURL()
288: */
289: public URL getURL() {
290: LOG.trace("enter HttpURLConnection.getURL()");
291: return this .url;
292: }
293:
294: // Note: We don't implement the following methods so that they default to
295: // the JDK implementation. They will all call
296: // <code>getHeaderField(String)</code> which we have overridden.
297:
298: // java.net.HttpURLConnection#getHeaderFieldDate(String, long)
299: // java.net.HttpURLConnection#getContentLength()
300: // java.net.HttpURLConnection#getContentType()
301: // java.net.HttpURLConnection#getContentEncoding()
302: // java.net.HttpURLConnection#getDate()
303: // java.net.HttpURLConnection#getHeaderFieldInt(String, int)
304: // java.net.HttpURLConnection#getExpiration()
305: // java.net.HttpURLConnection#getLastModified()
306:
307: /**
308: * Not available: the data must have already been retrieved.
309: */
310: public void setInstanceFollowRedirects(boolean isFollowingRedirects) {
311: LOG
312: .trace("enter HttpURLConnection.setInstanceFollowRedirects(boolean)");
313: throw new RuntimeException(
314: "This class can only be used with already"
315: + "retrieved data");
316: }
317:
318: /**
319: * Not yet implemented.
320: */
321: public boolean getInstanceFollowRedirects() {
322: LOG
323: .trace("enter HttpURLConnection.getInstanceFollowRedirects()");
324: throw new RuntimeException("Not implemented yet");
325: }
326:
327: /**
328: * Not available: the data must have already been retrieved.
329: * @see java.net.HttpURLConnection#setRequestMethod(String)
330: */
331: public void setRequestMethod(String method)
332: throws ProtocolException {
333: LOG.trace("enter HttpURLConnection.setRequestMethod(String)");
334: throw new RuntimeException(
335: "This class can only be used with already"
336: + "retrieved data");
337: }
338:
339: /**
340: * Not yet implemented.
341: * @see java.net.HttpURLConnection#getPermission()
342: */
343: public Permission getPermission() throws IOException {
344: LOG.trace("enter HttpURLConnection.getPermission()");
345: throw new RuntimeException("Not implemented yet");
346: }
347:
348: /**
349: * Not yet implemented.
350: * @see java.net.HttpURLConnection#getContent()
351: */
352: public Object getContent() throws IOException {
353: LOG.trace("enter HttpURLConnection.getContent()");
354: throw new RuntimeException("Not implemented yet");
355: }
356:
357: /**
358: * Not yet implemented.
359: */
360: public Object getContent(Class[] classes) throws IOException {
361: LOG.trace("enter HttpURLConnection.getContent(Class[])");
362: throw new RuntimeException("Not implemented yet");
363: }
364:
365: /**
366: * @see java.net.HttpURLConnection#getOutputStream()
367: */
368: public OutputStream getOutputStream() throws IOException {
369: LOG.trace("enter HttpURLConnection.getOutputStream()");
370: throw new RuntimeException(
371: "This class can only be used with already"
372: + "retrieved data");
373: }
374:
375: /**
376: * Not available: the data must have already been retrieved.
377: * @see java.net.HttpURLConnection#setDoInput(boolean)
378: */
379: public void setDoInput(boolean isInput) {
380: LOG.trace("enter HttpURLConnection.setDoInput()");
381: throw new RuntimeException(
382: "This class can only be used with already"
383: + "retrieved data");
384: }
385:
386: /**
387: * Not yet implemented.
388: * @see java.net.HttpURLConnection#getDoInput()
389: */
390: public boolean getDoInput() {
391: LOG.trace("enter HttpURLConnection.getDoInput()");
392: throw new RuntimeException("Not implemented yet");
393: }
394:
395: /**
396: * Not available: the data must have already been retrieved.
397: * @see java.net.HttpURLConnection#setDoOutput(boolean)
398: */
399: public void setDoOutput(boolean isOutput) {
400: LOG.trace("enter HttpURLConnection.setDoOutput()");
401: throw new RuntimeException(
402: "This class can only be used with already"
403: + "retrieved data");
404: }
405:
406: /**
407: * Not yet implemented.
408: * @see java.net.HttpURLConnection#getDoOutput()
409: */
410: public boolean getDoOutput() {
411: LOG.trace("enter HttpURLConnection.getDoOutput()");
412: throw new RuntimeException("Not implemented yet");
413: }
414:
415: /**
416: * Not available: the data must have already been retrieved.
417: * @see java.net.HttpURLConnection#setAllowUserInteraction(boolean)
418: */
419: public void setAllowUserInteraction(boolean isAllowInteraction) {
420: LOG
421: .trace("enter HttpURLConnection.setAllowUserInteraction(boolean)");
422: throw new RuntimeException(
423: "This class can only be used with already"
424: + "retrieved data");
425: }
426:
427: /**
428: * Not yet implemented.
429: * @see java.net.HttpURLConnection#getAllowUserInteraction()
430: */
431: public boolean getAllowUserInteraction() {
432: LOG.trace("enter HttpURLConnection.getAllowUserInteraction()");
433: throw new RuntimeException("Not implemented yet");
434: }
435:
436: /**
437: * Not available: the data must have already been retrieved.
438: * @see java.net.HttpURLConnection#setUseCaches(boolean)
439: */
440: public void setUseCaches(boolean isUsingCaches) {
441: LOG.trace("enter HttpURLConnection.setUseCaches(boolean)");
442: throw new RuntimeException(
443: "This class can only be used with already"
444: + "retrieved data");
445: }
446:
447: /**
448: * Not yet implemented.
449: * @see java.net.HttpURLConnection#getUseCaches()
450: */
451: public boolean getUseCaches() {
452: LOG.trace("enter HttpURLConnection.getUseCaches()");
453: throw new RuntimeException("Not implemented yet");
454: }
455:
456: /**
457: * Not available: the data must have already been retrieved.
458: * @see java.net.HttpURLConnection#setIfModifiedSince(long)
459: */
460: public void setIfModifiedSince(long modificationDate) {
461: LOG.trace("enter HttpURLConnection.setIfModifiedSince(long)");
462: throw new RuntimeException(
463: "This class can only be used with already"
464: + "retrieved data");
465: }
466:
467: /**
468: * Not yet implemented.
469: * @see java.net.HttpURLConnection#getIfModifiedSince()
470: */
471: public long getIfModifiedSince() {
472: LOG.trace("enter HttpURLConnection.getIfmodifiedSince()");
473: throw new RuntimeException("Not implemented yet");
474: }
475:
476: /**
477: * Not available: the data must have already been retrieved.
478: * @see java.net.HttpURLConnection#getDefaultUseCaches()
479: */
480: public boolean getDefaultUseCaches() {
481: LOG.trace("enter HttpURLConnection.getDefaultUseCaches()");
482: throw new RuntimeException("Not implemented yet");
483: }
484:
485: /**
486: * Not available: the data must have already been retrieved.
487: * @see java.net.HttpURLConnection#setDefaultUseCaches(boolean)
488: */
489: public void setDefaultUseCaches(boolean isUsingCaches) {
490: LOG
491: .trace("enter HttpURLConnection.setDefaultUseCaches(boolean)");
492: throw new RuntimeException(
493: "This class can only be used with already"
494: + "retrieved data");
495: }
496:
497: /**
498: * Not available: the data must have already been retrieved.
499: * @see java.net.HttpURLConnection#setRequestProperty(String,String)
500: */
501: public void setRequestProperty(String key, String value) {
502: LOG.trace("enter HttpURLConnection.setRequestProperty()");
503: throw new RuntimeException(
504: "This class can only be used with already"
505: + "retrieved data");
506: }
507:
508: /**
509: * Not yet implemented.
510: * @see java.net.HttpURLConnection#getRequestProperty(String)
511: */
512: public String getRequestProperty(String key) {
513: LOG.trace("enter HttpURLConnection.getRequestProperty()");
514: throw new RuntimeException("Not implemented yet");
515: }
516:
517: }
|