001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v 1.98 2004/10/07 16:14:15 olegk Exp $
003: * $Revision: 509577 $
004: * $Date: 2007-02-20 15:28:18 +0100 (Tue, 20 Feb 2007) $
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;
032:
033: import java.io.IOException;
034: import java.security.Provider;
035: import java.security.Security;
036:
037: import org.apache.commons.httpclient.params.HttpClientParams;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * <p>
043: * An HTTP "user-agent", containing an {@link HttpState HTTP state} and
044: * one or more {@link HttpConnection HTTP connections}, to which
045: * {@link HttpMethod HTTP methods} can be applied.
046: * </p>
047: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
048: * @author <a href="mailto:rwaldhoff@apache.org">Rodney Waldhoff</a>
049: * @author Sean C. Sullivan
050: * @author <a href="mailto:dion@apache.org">dIon Gillard</a>
051: * @author Ortwin Gl?ck
052: * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
053: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
054: * @author Sam Maloney
055: * @author Laura Werner
056: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
057: *
058: * @version $Revision: 509577 $ $Date: 2007-02-20 15:28:18 +0100 (Tue, 20 Feb 2007) $
059: */
060: public class HttpClient {
061:
062: // -------------------------------------------------------------- Constants
063:
064: /** Log object for this class. */
065: private static final Log LOG = LogFactory.getLog(HttpClient.class);
066:
067: static {
068:
069: if (LOG.isDebugEnabled()) {
070: try {
071: LOG.debug("Java version: "
072: + System.getProperty("java.version"));
073: LOG.debug("Java vendor: "
074: + System.getProperty("java.vendor"));
075: LOG.debug("Java class path: "
076: + System.getProperty("java.class.path"));
077: LOG.debug("Operating system name: "
078: + System.getProperty("os.name"));
079: LOG.debug("Operating system architecture: "
080: + System.getProperty("os.arch"));
081: LOG.debug("Operating system version: "
082: + System.getProperty("os.version"));
083:
084: Provider[] providers = Security.getProviders();
085: for (int i = 0; i < providers.length; i++) {
086: Provider provider = providers[i];
087: LOG.debug(provider.getName() + " "
088: + provider.getVersion() + ": "
089: + provider.getInfo());
090: }
091: } catch (SecurityException ignore) {
092: }
093: }
094: }
095:
096: // ----------------------------------------------------------- Constructors
097:
098: /**
099: * Creates an instance of HttpClient using default {@link HttpClientParams parameter set}.
100: *
101: * @see HttpClientParams
102: */
103: public HttpClient() {
104: this (new HttpClientParams());
105: }
106:
107: /**
108: * Creates an instance of HttpClient using the given
109: * {@link HttpClientParams parameter set}.
110: *
111: * @param params The {@link HttpClientParams parameters} to use.
112: *
113: * @see HttpClientParams
114: *
115: * @since 3.0
116: */
117: public HttpClient(HttpClientParams params) {
118: super ();
119: if (params == null) {
120: throw new IllegalArgumentException("Params may not be null");
121: }
122: this .params = params;
123: this .httpConnectionManager = null;
124: Class clazz = params.getConnectionManagerClass();
125: if (clazz != null) {
126: try {
127: this .httpConnectionManager = (HttpConnectionManager) clazz
128: .newInstance();
129: } catch (Exception e) {
130: LOG.warn(
131: "Error instantiating connection manager class, defaulting to"
132: + " SimpleHttpConnectionManager", e);
133: }
134: }
135: if (this .httpConnectionManager == null) {
136: this .httpConnectionManager = new SimpleHttpConnectionManager();
137: }
138: if (this .httpConnectionManager != null) {
139: this .httpConnectionManager.getParams().setDefaults(
140: this .params);
141: }
142: }
143:
144: /**
145: * Creates an instance of HttpClient with a user specified
146: * {@link HttpClientParams parameter set} and
147: * {@link HttpConnectionManager HTTP connection manager}.
148: *
149: * @param params The {@link HttpClientParams parameters} to use.
150: * @param httpConnectionManager The {@link HttpConnectionManager connection manager}
151: * to use.
152: *
153: * @since 3.0
154: */
155: public HttpClient(HttpClientParams params,
156: HttpConnectionManager httpConnectionManager) {
157: super ();
158: if (httpConnectionManager == null) {
159: throw new IllegalArgumentException(
160: "httpConnectionManager cannot be null");
161: }
162: if (params == null) {
163: throw new IllegalArgumentException("Params may not be null");
164: }
165: this .params = params;
166: this .httpConnectionManager = httpConnectionManager;
167: this .httpConnectionManager.getParams().setDefaults(this .params);
168: }
169:
170: /**
171: * Creates an instance of HttpClient with a user specified
172: * {@link HttpConnectionManager HTTP connection manager}.
173: *
174: * @param httpConnectionManager The {@link HttpConnectionManager connection manager}
175: * to use.
176: *
177: * @since 2.0
178: */
179: public HttpClient(HttpConnectionManager httpConnectionManager) {
180: this (new HttpClientParams(), httpConnectionManager);
181: }
182:
183: // ----------------------------------------------------- Instance Variables
184:
185: /**
186: * The {@link HttpConnectionManager connection manager} being used to manage
187: * connections for this HttpClient
188: */
189: private HttpConnectionManager httpConnectionManager;
190:
191: /**
192: * The {@link HttpState HTTP state} associated with this HttpClient.
193: */
194: private HttpState state = new HttpState();
195:
196: /**
197: * The {@link HttpClientParams collection of parameters} associated with this HttpClient.
198: */
199: private HttpClientParams params = null;
200:
201: /**
202: * The {@link HostConfiguration host configuration} associated with
203: * the HttpClient
204: */
205: private HostConfiguration hostConfiguration = new HostConfiguration();
206:
207: // ------------------------------------------------------------- Properties
208:
209: /**
210: * Returns {@link HttpState HTTP state} associated with the HttpClient.
211: *
212: * @see #setState(HttpState)
213: * @return the shared client state
214: */
215: public synchronized HttpState getState() {
216: return state;
217: }
218:
219: /**
220: * Assigns {@link HttpState HTTP state} for the HttpClient.
221: *
222: * @see #getState()
223: * @param state the new {@link HttpState HTTP state} for the client
224: */
225: public synchronized void setState(HttpState state) {
226: this .state = state;
227: }
228:
229: /**
230: * Defines how strictly the method follows the HTTP protocol specification
231: * (see RFC 2616 and other relevant RFCs).
232: *
233: * In the strict mode the method precisely
234: * implements the requirements of the specification, whereas in non-strict mode
235: * it attempts to mimic the exact behaviour of commonly used HTTP agents,
236: * which many HTTP servers expect.
237: *
238: * @param strictMode <tt>true</tt> for strict mode, <tt>false</tt> otherwise
239: *
240: * @see #isStrictMode()
241: *
242: * @deprecated Use {@link HttpClientParams#setParameter(String, Object)}
243: * to exercise a more granular control over HTTP protocol strictness.
244: */
245: public synchronized void setStrictMode(boolean strictMode) {
246: if (strictMode) {
247: this .params.makeStrict();
248: } else {
249: this .params.makeLenient();
250: }
251: }
252:
253: /**
254: * Returns the value of the strict mode flag.
255: *
256: * @return <tt>true</tt> if strict mode is enabled, <tt>false</tt> otherwise
257: *
258: * @see #setStrictMode(boolean)
259: *
260: * @deprecated Use
261: * {@link org.apache.commons.httpclient.params.HttpClientParams#getParameter(String)}
262: * to exercise a more granular control over HTTP protocol strictness.
263: */
264: public synchronized boolean isStrictMode() {
265: return false;
266: }
267:
268: /**
269: * Sets the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
270: * timeout for waiting for data. A timeout value of zero is interpreted as an
271: * infinite timeout.
272: *
273: * @param newTimeoutInMilliseconds Timeout in milliseconds
274: *
275: * @deprecated Use
276: * {@link org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)},
277: * {@link HttpConnectionManager#getParams()}.
278: *
279: */
280: public synchronized void setTimeout(int newTimeoutInMilliseconds) {
281: this .params.setSoTimeout(newTimeoutInMilliseconds);
282: }
283:
284: /**
285: * Sets the timeout in milliseconds used when retrieving an
286: * {@link HttpConnection HTTP connection} from the
287: * {@link HttpConnectionManager HTTP connection manager}.
288: *
289: * @param timeout the timeout in milliseconds
290: *
291: * @see HttpConnectionManager#getConnection(HostConfiguration, long)
292: *
293: * @deprecated Use
294: * {@link org.apache.commons.httpclient.params.HttpClientParams#setConnectionManagerTimeout(long)},
295: * {@link HttpClient#getParams()}
296: */
297: public synchronized void setHttpConnectionFactoryTimeout(
298: long timeout) {
299: this .params.setConnectionManagerTimeout(timeout);
300: }
301:
302: /**
303: * Sets the timeout until a connection is etablished. A value of zero
304: * means the timeout is not used. The default value is zero.
305: *
306: * @see HttpConnection#setConnectionTimeout(int)
307: * @param newTimeoutInMilliseconds Timeout in milliseconds.
308: *
309: * @deprecated Use
310: * {@link org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)},
311: * {@link HttpConnectionManager#getParams()}.
312: */
313: public synchronized void setConnectionTimeout(
314: int newTimeoutInMilliseconds) {
315: this .httpConnectionManager.getParams().setConnectionTimeout(
316: newTimeoutInMilliseconds);
317: }
318:
319: // --------------------------------------------------------- Public Methods
320:
321: /**
322: * Executes the given {@link HttpMethod HTTP method}.
323: *
324: * @param method the {@link HttpMethod HTTP method} to execute.
325: * @return the method's response code
326: *
327: * @throws IOException If an I/O (transport) error occurs. Some transport exceptions
328: * can be recovered from.
329: * @throws HttpException If a protocol exception occurs. Usually protocol exceptions
330: * cannot be recovered from.
331: */
332: public int executeMethod(HttpMethod method) throws IOException,
333: HttpException {
334:
335: LOG.trace("enter HttpClient.executeMethod(HttpMethod)");
336: // execute this method and use its host configuration, if it has one
337: return executeMethod(null, method, null);
338: }
339:
340: /**
341: * Executes the given {@link HttpMethod HTTP method} using custom
342: * {@link HostConfiguration host configuration}.
343: *
344: * @param hostConfiguration The {@link HostConfiguration host configuration} to use.
345: * If <code>null</code>, the host configuration returned by {@link #getHostConfiguration} will be used.
346: * @param method the {@link HttpMethod HTTP method} to execute.
347: * @return the method's response code
348: *
349: * @throws IOException If an I/O (transport) error occurs. Some transport exceptions
350: * can be recovered from.
351: * @throws HttpException If a protocol exception occurs. Usually protocol exceptions
352: * cannot be recovered from.
353: * @since 2.0
354: */
355: public int executeMethod(final HostConfiguration hostConfiguration,
356: final HttpMethod method) throws IOException, HttpException {
357:
358: LOG
359: .trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod)");
360:
361: return executeMethod(hostConfiguration, method, null);
362: }
363:
364: /**
365: * Executes the given {@link HttpMethod HTTP method} using the given custom
366: * {@link HostConfiguration host configuration} with the given custom
367: * {@link HttpState HTTP state}.
368: *
369: * @param hostconfig The {@link HostConfiguration host configuration} to use.
370: * If <code>null</code>, the host configuration returned by {@link #getHostConfiguration} will be used.
371: * @param method the {@link HttpMethod HTTP method} to execute.
372: * @param state the {@link HttpState HTTP state} to use when executing the method.
373: * If <code>null</code>, the state returned by {@link #getState} will be used.
374: *
375: * @return the method's response code
376: *
377: * @throws IOException If an I/O (transport) error occurs. Some transport exceptions
378: * can be recovered from.
379: * @throws HttpException If a protocol exception occurs. Usually protocol exceptions
380: * cannot be recovered from.
381: * @since 2.0
382: */
383: public int executeMethod(HostConfiguration hostconfig,
384: final HttpMethod method, final HttpState state)
385: throws IOException, HttpException {
386:
387: LOG
388: .trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)");
389:
390: if (method == null) {
391: throw new IllegalArgumentException(
392: "HttpMethod parameter may not be null");
393: }
394: HostConfiguration defaulthostconfig = getHostConfiguration();
395: if (hostconfig == null) {
396: hostconfig = defaulthostconfig;
397: }
398: URI uri = method.getURI();
399: if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
400: // make a deep copy of the host defaults
401: hostconfig = (HostConfiguration) hostconfig.clone();
402: if (uri.isAbsoluteURI()) {
403: hostconfig.setHost(uri);
404: }
405: }
406:
407: HttpMethodDirector methodDirector = new HttpMethodDirector(
408: getHttpConnectionManager(), hostconfig, this .params,
409: (state == null ? getState() : state));
410: methodDirector.executeMethod(method);
411: return method.getStatusCode();
412: }
413:
414: /**
415: * Returns the default host.
416: *
417: * @return The default host.
418: *
419: * @deprecated use #getHostConfiguration()
420: */
421: public String getHost() {
422: return hostConfiguration.getHost();
423: }
424:
425: /**
426: * Returns the default port.
427: *
428: * @return The default port.
429: *
430: * @deprecated use #getHostConfiguration()
431: */
432: public int getPort() {
433: return hostConfiguration.getPort();
434: }
435:
436: /**
437: * Returns the {@link HostConfiguration host configuration} associated with the
438: * HttpClient.
439: *
440: * @return {@link HostConfiguration host configuration}
441: *
442: * @since 2.0
443: */
444: public synchronized HostConfiguration getHostConfiguration() {
445: return hostConfiguration;
446: }
447:
448: /**
449: * Assigns the {@link HostConfiguration host configuration} to use with the
450: * HttpClient.
451: *
452: * @param hostConfiguration The {@link HostConfiguration host configuration} to set
453: *
454: * @since 2.0
455: */
456: public synchronized void setHostConfiguration(
457: HostConfiguration hostConfiguration) {
458: this .hostConfiguration = hostConfiguration;
459: }
460:
461: /**
462: * Returns the {@link HttpConnectionManager HTTP connection manager} associated
463: * with the HttpClient.
464: *
465: * @return {@link HttpConnectionManager HTTP connection manager}
466: *
467: * @since 2.0
468: */
469: public synchronized HttpConnectionManager getHttpConnectionManager() {
470: return httpConnectionManager;
471: }
472:
473: /**
474: * Assigns the {@link HttpConnectionManager HTTP connection manager} to use with
475: * the HttpClient.
476: *
477: * @param httpConnectionManager The {@link HttpConnectionManager HTTP connection manager}
478: * to set
479: *
480: * @since 2.0
481: */
482: public synchronized void setHttpConnectionManager(
483: HttpConnectionManager httpConnectionManager) {
484: this .httpConnectionManager = httpConnectionManager;
485: if (this .httpConnectionManager != null) {
486: this .httpConnectionManager.getParams().setDefaults(
487: this .params);
488: }
489: }
490:
491: /**
492: * Returns {@link HttpClientParams HTTP protocol parameters} associated with this HttpClient.
493: *
494: * @since 3.0
495: *
496: * @see HttpClientParams
497: */
498: public HttpClientParams getParams() {
499: return this .params;
500: }
501:
502: /**
503: * Assigns {@link HttpClientParams HTTP protocol parameters} for this HttpClient.
504: *
505: * @since 3.0
506: *
507: * @see HttpClientParams
508: */
509: public void setParams(final HttpClientParams params) {
510: if (params == null) {
511: throw new IllegalArgumentException(
512: "Parameters may not be null");
513: }
514: this.params = params;
515: }
516:
517: }
|