84
81
LOG.debug(provider.getName() + " " + provider.getVersion()
85
82
+ ": " + provider.getInfo());
87
} catch(SecurityException ignore) {
84
} catch (SecurityException ignore) {
91
88
// ----------------------------------------------------------- Constructors
94
* Creates an instance of HttpClient using a
95
* {@link SimpleHttpConnectionManager simple HTTP connection manager}.
91
* Creates an instance of HttpClient using default {@link HttpClientParams parameter set}.
97
* @see SimpleHttpConnectionManager
93
* @see HttpClientParams
99
95
public HttpClient() {
100
this(new SimpleHttpConnectionManager());
104
* Creates an instance of HttpClient with a user specified connection manager.
96
this(new HttpClientParams());
100
* Creates an instance of HttpClient using the given
101
* {@link HttpClientParams parameter set}.
103
* @param params The {@link HttpClientParams parameters} to use.
105
* @see HttpClientParams
109
public HttpClient(HttpClientParams params) {
111
if (params == null) {
112
throw new IllegalArgumentException("Params may not be null");
114
this.params = params;
115
this.httpConnectionManager = null;
116
Class clazz = params.getConnectionManagerClass();
119
this.httpConnectionManager = (HttpConnectionManager) clazz.newInstance();
120
} catch (Exception e) {
121
LOG.warn("Error instantiating connection manager class, defaulting to"
122
+ " SimpleHttpConnectionManager",
126
if (this.httpConnectionManager == null) {
127
this.httpConnectionManager = new SimpleHttpConnectionManager();
129
if (this.httpConnectionManager != null) {
130
this.httpConnectionManager.getParams().setDefaults(this.params);
135
* Creates an instance of HttpClient with a user specified
136
* {@link HttpClientParams parameter set} and
137
* {@link HttpConnectionManager HTTP connection manager}.
139
* @param params The {@link HttpClientParams parameters} to use.
105
140
* @param httpConnectionManager The {@link HttpConnectionManager connection manager}
110
public HttpClient(HttpConnectionManager httpConnectionManager) {
145
public HttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager) {
112
147
if (httpConnectionManager == null) {
113
148
throw new IllegalArgumentException("httpConnectionManager cannot be null");
116
this.state = new HttpState();
150
if (params == null) {
151
throw new IllegalArgumentException("Params may not be null");
153
this.params = params;
117
154
this.httpConnectionManager = httpConnectionManager;
119
this.hostConfiguration = new HostConfiguration();
155
if (this.httpConnectionManager != null) {
156
this.httpConnectionManager.getParams().setDefaults(this.params);
161
* Creates an instance of HttpClient with a user specified
162
* {@link HttpConnectionManager HTTP connection manager}.
164
* @param httpConnectionManager The {@link HttpConnectionManager connection manager}
169
public HttpClient(HttpConnectionManager httpConnectionManager) {
170
this(new HttpClientParams(), httpConnectionManager);
123
173
// ----------------------------------------------------- Instance Variables
229
279
* @param timeout the timeout in milliseconds
231
281
* @see HttpConnectionManager#getConnection(HostConfiguration, long)
284
* {@link org.apache.commons.httpclient.params.HttpClientParams#setConnectionManagerTimeout(long)},
285
* {@link HttpClient#getParams()}
233
287
public synchronized void setHttpConnectionFactoryTimeout(long timeout) {
234
this.httpConnectionTimeout = timeout;
288
this.params.setConnectionManagerTimeout(timeout);
238
* Sets the timeout until a connection is etablished. A timeout value of
239
* zero means the timeout is not used. The default value is zero.
292
* Sets the timeout until a connection is etablished. A value of zero
293
* means the timeout is not used. The default value is zero.
295
* @see HttpConnection#setConnectionTimeout(int)
241
296
* @param newTimeoutInMilliseconds Timeout in milliseconds.
243
* @see HttpConnection#setConnectionTimeout(int)
299
* {@link org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)},
300
* {@link HttpConnectionManager#getParams()}.
245
302
public synchronized void setConnectionTimeout(int newTimeoutInMilliseconds) {
246
this.connectionTimeout = newTimeoutInMilliseconds;
303
this.httpConnectionManager.getParams().setConnectionTimeout(newTimeoutInMilliseconds);
249
306
// --------------------------------------------------------- Public Methods
252
* Sets the host, port and default protocol (http) to be used when executing a
255
* @param host the host to connect to
256
* @param port the port to connect to
258
* @see #getHostConfiguration()
260
* @deprecated use {@link HostConfiguration}
262
public void startSession(String host, int port) {
263
LOG.trace("enter HttpClient.startSession(String, int)");
264
startSession(host, port, false);
268
* Sets the host, port and protocol to be used when executing a method.
270
* @param host the host to connect to
271
* @param port the port to connect to
272
* @param https when <code>true</code>, create an HTTPS session
274
* @see #getHostConfiguration()
276
* @deprecated use {@link HostConfiguration}
278
public void startSession(String host, int port, boolean https) {
279
LOG.trace("enter HttpClient.startSession(String, int, boolean)");
281
if (LOG.isDebugEnabled()) {
282
LOG.debug("HttpClient.startSession(String,int,boolean): Host:"
283
+ host + " Port:" + port + " HTTPS:" + https);
286
this.hostConfiguration.setHost(host, port, https ? "https" : "http");
290
* Sets the host, port, default protocol (http) and credentials to be used when
291
* executing a method.
293
* @param host the host to connect to
294
* @param port the port to connect to
295
* @param creds the default credentials to use
297
* @see #getHostConfiguration()
299
* @see #startSession(String, int, Credentials, boolean)
301
* @deprecated use {@link HostConfiguration} and {@link HttpState}
303
public void startSession(String host, int port, Credentials creds) {
304
LOG.trace("enter HttpClient.startSession(String, int, Credentials)");
305
startSession(host, port, creds, false);
309
* Sets the host, port, protocol and credentials to be used when executing a
312
* @param host the host to connect to
313
* @param port the port to connect to
314
* @param creds the default credentials to use
315
* @param https when <code>true</code>, create an HTTPS session
317
* @see #getHostConfiguration()
320
* @deprecated use {@link HostConfiguration} and {@link HttpState}
322
public void startSession(String host, int port, Credentials creds, boolean https) {
323
LOG.trace("enter HttpClient.startSession(String, int, Credentials, boolean)");
325
if (LOG.isDebugEnabled()) {
327
"Starting HttpClient session"
329
+ " Port:" + port + " Credentials:" + creds
330
+ " HTTPS:" + https);
332
getState().setCredentials(null, creds);
333
this.hostConfiguration.setHost(
336
https ? "https" : "http"
341
* Sets the host, port, protocol and credentials to be used when executing a
342
* method using the server specified by the scheme, userinfo, host and port
343
* of the given <i>uri</i>.
345
* Note that the path component is not utilized.
347
* @param uri an <code>HttpURL</code> or <code>HttpsURL</code> instance; the
348
* {@link URI URI} from which the scheme, userinfo, host and port of the
349
* session are determined
351
* @throws IllegalStateException not enough information to process
352
* @throws URIException If the URI is bad.
354
* @see #getHostConfiguration()
356
* @deprecated use {@link HostConfiguration}
358
public void startSession(URI uri)
359
throws URIException, IllegalStateException {
361
LOG.trace("enter HttpClient.startSession(URI)");
363
String scheme = uri.getScheme();
364
if (scheme == null) { // it may a URI instance or abs_path
365
LOG.error("no scheme to start a session");
366
throw new IllegalStateException("no scheme to start a session");
369
Protocol protocol = Protocol.getProtocol(scheme);
371
String userinfo = uri.getUserinfo();
372
if (userinfo != null) {
373
getState().setCredentials(null,
374
new UsernamePasswordCredentials(userinfo));
376
String host = uri.getHost();
377
if (host == null || host.length() == 0) {
378
LOG.error("no host to start a session");
379
throw new IllegalStateException("no host to start a session");
381
int port = uri.getPort();
382
if (port == -1) { // neither HttpURL or HttpsURL instance
383
LOG.error("HttpURL or HttpsURL instance required");
384
throw new IllegalStateException
385
("HttpURL or HttpsURL instance required");
387
this.hostConfiguration.setHost(host, null, port, protocol);
391
* Sets the host, port and protocol to be used when executing a method.
393
* Note that everything but the protocol, host and port of the
394
* given <i>url</i> is ignored.
396
* @param url the {@link URL URL} from which the protocol, host, and port of
397
* the session are determined
399
* @exception IllegalArgumentException if the protocol is not http or https
401
* @see #getHostConfiguration()
403
* @deprecated use {@link HostConfiguration}
405
public void startSession(URL url) throws IllegalArgumentException {
406
LOG.trace("enter HttpClient.startSession(String, int, Credentials, boolean)");
408
int port = url.getPort();
409
Protocol protocol = Protocol.getProtocol(url.getProtocol());
411
hostConfiguration.setHost(url.getHost(), null, port, protocol);
415
* Sets the host, port, protocol and credentials to be used when executing a
418
* Note that everything but the protocol, host and port of the
419
* given <i>url</i> is ignored.
421
* @param url the {@link URL URL} from which the protocol, host, and port of
422
* the session are determined
423
* @param creds the default credentials to use
425
* @exception IllegalArgumentException if the protocol is not http or https
427
* @see #getHostConfiguration()
430
* @deprecated use {@link HostConfiguration} and {@link HttpState}
432
public void startSession(URL url, Credentials creds)
433
throws IllegalArgumentException {
435
LOG.trace("enter HttpClient.startSession(URL, Credentials)");
436
getState().setCredentials(null, creds);
441
* Sets the host, port, protocol(http) and proxy to be used when executing a
444
* @param host the host to connect to
445
* @param port the port to connect to
446
* @param proxyhost the proxy host to connect via
447
* @param proxyport the proxy port to connect via
449
* @see #getHostConfiguration()
451
* @deprecated use {@link HostConfiguration}
453
public void startSession(String host, int port, String proxyhost, int proxyport) {
454
LOG.trace("enter HttpClient.startSession(String, int, String, int)");
455
startSession(host, port, proxyhost, proxyport, false);
459
* Sets the host, port, protocol and proxy to be used when executing a
462
* @param host the host to connect to
463
* @param port the port to connect to
464
* @param proxyhost the proxy host to connect via
465
* @param proxyport the proxy port to connect via
466
* @param secure whether or not to connect using HTTPS
468
* @see #getHostConfiguration()
470
* @deprecated use {@link HostConfiguration}
472
public void startSession(String host, int port,
473
String proxyhost, int proxyport, boolean secure) {
475
LOG.trace("enter HttpClient.startSession("
476
+ "String, int, String, int, boolean)");
477
this.hostConfiguration.setHost (host, port, secure ? "https" : "http");
478
this.hostConfiguration.setProxy(proxyhost, proxyport);
482
309
* Executes the given {@link HttpMethod HTTP method}.
484
311
* @param method the {@link HttpMethod HTTP method} to execute.
485
312
* @return the method's response code
487
314
* @throws IOException If an I/O (transport) error occurs. Some transport exceptions
488
* can be recovered from.
315
* can be recovered from.
489
316
* @throws HttpException If a protocol exception occurs. Usually protocol exceptions
490
317
* cannot be recovered from.
555
375
if (method == null) {
556
376
throw new IllegalArgumentException("HttpMethod parameter may not be null");
560
boolean strictMode = false;
561
int connectionTimeout = 0;
562
long httpConnectionTimeout = 0;
563
HostConfiguration defaultHostConfiguration = null;
565
/* access all synchronized data in a single block, this will keeps us
566
* from accessing data asynchronously as well having to regain the lock
569
synchronized (this) {
570
soTimeout = this.timeoutInMilliseconds;
571
strictMode = this.strictMode;
572
connectionTimeout = this.connectionTimeout;
573
httpConnectionTimeout = this.httpConnectionTimeout;
577
defaultHostConfiguration = getHostConfiguration();
580
HostConfiguration methodConfiguration
581
= new HostConfiguration(hostConfiguration);
583
if (hostConfiguration != defaultHostConfiguration) {
584
// we may need to apply some defaults
585
if (!methodConfiguration.isHostSet()) {
586
methodConfiguration.setHost(
587
defaultHostConfiguration.getHost(),
588
defaultHostConfiguration.getVirtualHost(),
589
defaultHostConfiguration.getPort(),
590
defaultHostConfiguration.getProtocol()
593
if (!methodConfiguration.isProxySet()
594
&& defaultHostConfiguration.isProxySet()) {
596
methodConfiguration.setProxy(
597
defaultHostConfiguration.getProxyHost(),
598
defaultHostConfiguration.getProxyPort()
601
if (methodConfiguration.getLocalAddress() == null
602
&& defaultHostConfiguration.getLocalAddress() != null) {
604
methodConfiguration.setLocalAddress(defaultHostConfiguration.getLocalAddress());
608
HttpConnectionManager connmanager = this.httpConnectionManager;
609
if (state.getHttpConnectionManager() != null) {
610
connmanager = state.getHttpConnectionManager();
613
HttpConnection connection = connmanager.getConnection(
615
httpConnectionTimeout
619
// Catch all possible exceptions to make sure to release the
620
// connection, as although the user may call
621
// Method->releaseConnection(), the method doesn't know about the
622
// connection until HttpMethod.execute() is called.
624
method.setStrictMode(strictMode);
626
if (!connection.isOpen()) {
627
connection.setConnectionTimeout(connectionTimeout);
629
if (connection.isProxied() && connection.isSecure()) {
630
method = new ConnectMethod(method);
633
connection.setSoTimeout(soTimeout);
635
} catch (IOException e) {
636
connection.releaseConnection();
638
} catch (RuntimeException e) {
639
connection.releaseConnection();
643
return method.execute(state, connection);
647
* @deprecated this method has no effect. {@link HttpMethod#releaseConnection()}
648
* should be used to release resources after a HttpMethod has been executed.
650
* @see HttpMethod#releaseConnection()
652
public void endSession() throws IOException {
378
HostConfiguration defaulthostconfig = getHostConfiguration();
379
if (hostconfig == null) {
380
hostconfig = defaulthostconfig;
382
URI uri = method.getURI();
383
if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
384
// make a deep copy of the host defaults
385
hostconfig = new HostConfiguration(hostconfig);
386
if (uri.isAbsoluteURI()) {
387
hostconfig.setHost(uri);
391
HttpMethodDirector methodDirector = new HttpMethodDirector(
392
getHttpConnectionManager(),
395
(state == null ? getState() : state));
396
methodDirector.executeMethod(method);
397
return method.getStatusCode();