~ubuntu-branches/ubuntu/precise/commons-httpclient/precise-security

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/httpclient/HttpClient.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2006-09-15 20:07:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060915200743-t2md4cgfsb07wgn7
Tags: 3.0.1-0.1
* Non-maintainer upload.
* Bump debhelper Build-Depends to (>= 4.1.0) as required by cdbs' 
  debhelper.mk
* Put the coppyright holders in debian/copyright
* Include the jar file in the package. (Closes: #381354)
* Only include one copy of the docs.
  done by James Westby <jw+debian@jameswestby.net>  Mon, 14 Aug 2006 02:29:47 +0100

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v 1.76.2.6 2004/04/09 12:04:12 olegk Exp $
3
 
 * $Revision: 1.76.2.6 $
4
 
 * $Date: 2004/04/09 12:04:12 $
 
2
 * $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 $
 
3
 * $Revision: 354155 $
 
4
 * $Date: 2005-12-05 15:18:10 -0500 (Mon, 05 Dec 2005) $
5
5
 *
6
6
 * ====================================================================
7
7
 *
25
25
 * information on the Apache Software Foundation, please see
26
26
 * <http://www.apache.org/>.
27
27
 *
28
 
 * [Additional notices, if required by prior licensing conditions]
29
 
 *
30
28
 */
31
29
 
32
30
package org.apache.commons.httpclient;
33
31
 
34
32
import java.io.IOException;
35
 
import java.net.URL;
36
 
import java.security.Security; 
37
 
import java.security.Provider;  
 
33
import java.security.Provider;
 
34
import java.security.Security;
38
35
 
39
 
import org.apache.commons.httpclient.protocol.Protocol;
 
36
import org.apache.commons.httpclient.params.HttpClientParams;
40
37
import org.apache.commons.logging.Log;
41
38
import org.apache.commons.logging.LogFactory;
42
39
 
43
 
 
44
40
/**
45
41
 * <p>
46
42
 * An HTTP "user-agent", containing an {@link HttpState HTTP state} and
51
47
 * @author <a href="mailto:rwaldhoff@apache.org">Rodney Waldhoff</a>
52
48
 * @author Sean C. Sullivan
53
49
 * @author <a href="mailto:dion@apache.org">dIon Gillard</a>
54
 
 * @author Ortwin Gl�ck
 
50
 * @author Ortwin Gl?ck
55
51
 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
56
52
 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
57
53
 * @author Sam Maloney
58
54
 * @author Laura Werner
59
55
 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
60
56
 * 
61
 
 * @version $Revision: 1.76.2.6 $ $Date: 2004/04/09 12:04:12 $
 
57
 * @version $Revision: 354155 $ $Date: 2005-12-05 15:18:10 -0500 (Mon, 05 Dec 2005) $
62
58
 */
63
59
public class HttpClient {
64
60
 
69
65
    private static final Log LOG = LogFactory.getLog(HttpClient.class);
70
66
 
71
67
    static {
 
68
        
72
69
        if (LOG.isDebugEnabled()) {
73
70
            try {
74
71
                LOG.debug("Java version: " + System.getProperty("java.version"));
84
81
                    LOG.debug(provider.getName() + " " + provider.getVersion()
85
82
                       + ": " + provider.getInfo());   
86
83
                }
87
 
            } catch(SecurityException ignore) {
 
84
            } catch (SecurityException ignore) {
88
85
            }
89
86
        }
90
87
    }
91
88
    // ----------------------------------------------------------- Constructors
92
89
 
93
90
    /**
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}.
96
92
     * 
97
 
     * @see SimpleHttpConnectionManager
 
93
     * @see HttpClientParams
98
94
     */
99
95
    public HttpClient() {
100
 
        this(new SimpleHttpConnectionManager());
101
 
    }
102
 
 
103
 
    /**
104
 
     * Creates an instance of HttpClient with a user specified connection manager.
 
96
        this(new HttpClientParams());
 
97
    }
 
98
 
 
99
    /**
 
100
     * Creates an instance of HttpClient using the given 
 
101
     * {@link HttpClientParams parameter set}.
 
102
     * 
 
103
     * @param params The {@link HttpClientParams parameters} to use.
 
104
     * 
 
105
     * @see HttpClientParams
 
106
     * 
 
107
     * @since 3.0
 
108
     */
 
109
    public HttpClient(HttpClientParams params) {
 
110
        super();
 
111
        if (params == null) {
 
112
            throw new IllegalArgumentException("Params may not be null");  
 
113
        }
 
114
        this.params = params;
 
115
        this.httpConnectionManager = null;
 
116
        Class clazz = params.getConnectionManagerClass();
 
117
        if (clazz != null) {
 
118
            try {
 
119
                this.httpConnectionManager = (HttpConnectionManager) clazz.newInstance();
 
120
            } catch (Exception e) {
 
121
                LOG.warn("Error instantiating connection manager class, defaulting to"
 
122
                    + " SimpleHttpConnectionManager", 
 
123
                    e);
 
124
            }
 
125
        }
 
126
        if (this.httpConnectionManager == null) {
 
127
            this.httpConnectionManager = new SimpleHttpConnectionManager();
 
128
        }
 
129
        if (this.httpConnectionManager != null) {
 
130
            this.httpConnectionManager.getParams().setDefaults(this.params);
 
131
        }
 
132
    }
 
133
 
 
134
    /**
 
135
     * Creates an instance of HttpClient with a user specified 
 
136
     * {@link HttpClientParams parameter set} and 
 
137
     * {@link HttpConnectionManager HTTP connection manager}.
 
138
     * 
 
139
     * @param params The {@link HttpClientParams parameters} to use.
105
140
     * @param httpConnectionManager The {@link HttpConnectionManager connection manager}
106
141
     * to use.
107
142
     * 
108
 
     * @since 2.0
 
143
     * @since 3.0
109
144
     */
110
 
    public HttpClient(HttpConnectionManager httpConnectionManager) {
111
 
 
 
145
    public HttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager) {
 
146
        super();
112
147
        if (httpConnectionManager == null) {
113
148
            throw new IllegalArgumentException("httpConnectionManager cannot be null");  
114
149
        }
115
 
 
116
 
        this.state = new HttpState();
 
150
        if (params == null) {
 
151
            throw new IllegalArgumentException("Params may not be null");  
 
152
        }
 
153
        this.params = params; 
117
154
        this.httpConnectionManager = httpConnectionManager;
118
 
 
119
 
        this.hostConfiguration = new HostConfiguration();
120
 
        
 
155
        if (this.httpConnectionManager != null) {
 
156
            this.httpConnectionManager.getParams().setDefaults(this.params);
 
157
        }
 
158
    }
 
159
    
 
160
    /**
 
161
     * Creates an instance of HttpClient with a user specified 
 
162
     * {@link HttpConnectionManager HTTP connection manager}.
 
163
     * 
 
164
     * @param httpConnectionManager The {@link HttpConnectionManager connection manager}
 
165
     * to use.
 
166
     * 
 
167
     * @since 2.0
 
168
     */
 
169
    public HttpClient(HttpConnectionManager httpConnectionManager) {
 
170
        this(new HttpClientParams(), httpConnectionManager);
121
171
    }
122
172
    
123
173
    // ----------------------------------------------------- Instance Variables
131
181
    /**
132
182
     * The {@link HttpState HTTP state} associated with this HttpClient.
133
183
     */
134
 
    private HttpState state;
135
 
 
136
 
    /** 
137
 
     * The timout in milliseconds when waiting for a connection from the 
138
 
     * {@link HttpConnectionManager connection manager} 
139
 
     */
140
 
    private long httpConnectionTimeout = 0;
141
 
 
142
 
    /** 
143
 
     * The socket timeout in milliseconds.
144
 
     */
145
 
    private int timeoutInMilliseconds = 0;
146
 
 
147
 
    /** 
148
 
     * The connection timeout in milliseconds. 
149
 
     */
150
 
    private int connectionTimeout = 0;
 
184
    private HttpState state = new HttpState();
 
185
    
 
186
    /**
 
187
     * The {@link HttpClientParams collection of parameters} associated with this HttpClient.
 
188
     */
 
189
    private HttpClientParams params = null; 
151
190
 
152
191
    /** 
153
192
     * The {@link HostConfiguration host configuration} associated with
154
193
     * the HttpClient
155
194
     */
156
 
    private HostConfiguration hostConfiguration;
 
195
    private HostConfiguration hostConfiguration = new HostConfiguration();
157
196
    
158
 
    /** 
159
 
     * True if strict mode is enabled. 
160
 
     */
161
 
    private boolean strictMode = false;
162
 
 
163
197
    // ------------------------------------------------------------- Properties
164
198
 
165
199
    /**
192
226
     * which many HTTP servers expect.
193
227
     * 
194
228
     * @param strictMode <tt>true</tt> for strict mode, <tt>false</tt> otherwise
195
 
     * 
 
229
     *
196
230
     * @see #isStrictMode()
 
231
     *
 
232
     * @deprecated Use {@link HttpClientParams#setParameter(String, Object)}
 
233
     * to exercise a more granular control over HTTP protocol strictness.
197
234
     */
198
235
    public synchronized void setStrictMode(boolean strictMode) {
199
 
        this.strictMode = strictMode;
 
236
        if (strictMode) {
 
237
            this.params.makeStrict();
 
238
        } else {
 
239
            this.params.makeLenient();
 
240
        }
200
241
    }
201
242
 
202
243
    /**
203
244
     * Returns the value of the strict mode flag.
204
 
     *
 
245
     * 
205
246
     * @return <tt>true</tt> if strict mode is enabled, <tt>false</tt> otherwise
206
 
     * 
 
247
     *
207
248
     * @see #setStrictMode(boolean)
 
249
     *
 
250
     * @deprecated Use 
 
251
     * {@link org.apache.commons.httpclient.params.HttpClientParams#getParameter(String)} 
 
252
     * to exercise a more granular control over HTTP protocol strictness.
208
253
     */
209
254
    public synchronized boolean isStrictMode() {
210
 
        return strictMode;
 
255
        return false;
211
256
    }
212
257
 
213
258
    /**
216
261
     * infinite timeout.
217
262
     *
218
263
     * @param newTimeoutInMilliseconds Timeout in milliseconds
 
264
     * 
 
265
     * @deprecated Use 
 
266
     * {@link org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)},
 
267
     * {@link HttpConnectionManager#getParams()}.
 
268
     *
219
269
     */
220
270
    public synchronized void setTimeout(int newTimeoutInMilliseconds) {
221
 
        this.timeoutInMilliseconds = newTimeoutInMilliseconds;
 
271
        this.params.setSoTimeout(newTimeoutInMilliseconds);
222
272
    }
223
273
 
224
274
    /**
229
279
     * @param timeout the timeout in milliseconds
230
280
     * 
231
281
     * @see HttpConnectionManager#getConnection(HostConfiguration, long)
 
282
     * 
 
283
     * @deprecated Use 
 
284
     * {@link org.apache.commons.httpclient.params.HttpClientParams#setConnectionManagerTimeout(long)},
 
285
     * {@link HttpClient#getParams()}
232
286
     */
233
287
    public synchronized void setHttpConnectionFactoryTimeout(long timeout) {
234
 
        this.httpConnectionTimeout = timeout;
 
288
        this.params.setConnectionManagerTimeout(timeout);
235
289
    }
236
290
 
237
291
    /**
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.
240
294
     * 
 
295
     * @see HttpConnection#setConnectionTimeout(int)
241
296
     * @param newTimeoutInMilliseconds Timeout in milliseconds.
242
297
     * 
243
 
     * @see HttpConnection#setConnectionTimeout(int)
 
298
     * @deprecated Use 
 
299
     * {@link org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)},
 
300
     * {@link HttpConnectionManager#getParams()}.
244
301
     */
245
302
    public synchronized void setConnectionTimeout(int newTimeoutInMilliseconds) {
246
 
       this.connectionTimeout = newTimeoutInMilliseconds;
 
303
       this.httpConnectionManager.getParams().setConnectionTimeout(newTimeoutInMilliseconds);
247
304
    }
248
305
 
249
306
    // --------------------------------------------------------- Public Methods
250
307
 
251
 
    /**
252
 
     * Sets the host, port and default protocol (http) to be used when executing a
253
 
     * method.
254
 
     * 
255
 
     * @param host the host to connect to
256
 
     * @param port the port to connect to
257
 
     *
258
 
     * @see #getHostConfiguration()
259
 
     * 
260
 
     * @deprecated use {@link HostConfiguration}
261
 
     */
262
 
    public void startSession(String host, int port) {
263
 
        LOG.trace("enter HttpClient.startSession(String, int)");
264
 
        startSession(host, port, false);
265
 
    }
266
 
 
267
 
    /**
268
 
     * Sets the host, port and protocol to be used when executing a method.
269
 
     * 
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
273
 
     *
274
 
     * @see #getHostConfiguration()
275
 
     * 
276
 
     * @deprecated use {@link HostConfiguration}
277
 
     */    
278
 
    public void startSession(String host, int port, boolean https) {
279
 
        LOG.trace("enter HttpClient.startSession(String, int, boolean)");
280
 
 
281
 
        if (LOG.isDebugEnabled()) {
282
 
            LOG.debug("HttpClient.startSession(String,int,boolean): Host:"
283
 
                + host + " Port:" + port + " HTTPS:" + https);
284
 
        }
285
 
        
286
 
        this.hostConfiguration.setHost(host, port, https ? "https" : "http");
287
 
    }
288
 
 
289
 
    /**
290
 
     * Sets the host, port, default protocol (http) and credentials to be used when
291
 
     * executing a method.
292
 
     *
293
 
     * @param host the host to connect to
294
 
     * @param port the port to connect to
295
 
     * @param creds the default credentials to use
296
 
     *
297
 
     * @see #getHostConfiguration()
298
 
     * @see #getState()
299
 
     * @see #startSession(String, int, Credentials, boolean)
300
 
     * 
301
 
     * @deprecated use {@link HostConfiguration} and {@link HttpState} 
302
 
     */
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);
306
 
    }
307
 
 
308
 
    /**
309
 
     * Sets the host, port, protocol and credentials to be used when executing a
310
 
     * method.
311
 
     *
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
316
 
     *
317
 
     * @see #getHostConfiguration()
318
 
     * @see #getState()
319
 
     * 
320
 
     * @deprecated use {@link HostConfiguration} and {@link HttpState} 
321
 
     */
322
 
    public void startSession(String host, int port, Credentials creds, boolean https) {
323
 
        LOG.trace("enter HttpClient.startSession(String, int, Credentials, boolean)");
324
 
 
325
 
        if (LOG.isDebugEnabled()) {
326
 
            LOG.debug(
327
 
                "Starting HttpClient session"
328
 
                + " Host:" + host
329
 
                + " Port:" + port + " Credentials:" + creds
330
 
                + " HTTPS:" + https);
331
 
        }
332
 
        getState().setCredentials(null, creds);
333
 
        this.hostConfiguration.setHost(
334
 
            host,
335
 
            port,
336
 
            https ? "https" : "http"
337
 
        );
338
 
    }
339
 
 
340
 
    /**
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>.
344
 
     * <p>
345
 
     * Note that the path component is not utilized.
346
 
     * <p>
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
350
 
     *
351
 
     * @throws IllegalStateException not enough information to process
352
 
     * @throws URIException If the URI is bad.
353
 
     * 
354
 
     * @see #getHostConfiguration()
355
 
     * 
356
 
     * @deprecated use {@link HostConfiguration} 
357
 
     */
358
 
    public void startSession(URI uri) 
359
 
        throws URIException, IllegalStateException {
360
 
            
361
 
        LOG.trace("enter HttpClient.startSession(URI)");
362
 
 
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");
367
 
        }
368
 
 
369
 
        Protocol protocol = Protocol.getProtocol(scheme);
370
 
 
371
 
        String userinfo = uri.getUserinfo();
372
 
        if (userinfo != null) {
373
 
            getState().setCredentials(null,
374
 
                    new UsernamePasswordCredentials(userinfo));
375
 
        }
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");
380
 
        }
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");
386
 
        }
387
 
        this.hostConfiguration.setHost(host, null, port, protocol);
388
 
    }
389
 
 
390
 
    /**
391
 
     * Sets the host, port and protocol to be used when executing a method.
392
 
     * <p>
393
 
     * Note that everything but the protocol, host and port of the
394
 
     * given <i>url</i> is ignored.
395
 
     * </p>
396
 
     * @param url the {@link URL URL} from which the protocol, host, and port of
397
 
     * the session are determined
398
 
     * 
399
 
     * @exception IllegalArgumentException if the protocol is not http or https
400
 
     *
401
 
     * @see #getHostConfiguration()
402
 
     * 
403
 
     * @deprecated use {@link HostConfiguration} 
404
 
     */
405
 
    public void startSession(URL url) throws IllegalArgumentException {
406
 
        LOG.trace("enter HttpClient.startSession(String, int, Credentials, boolean)");
407
 
 
408
 
        int port = url.getPort();
409
 
        Protocol protocol = Protocol.getProtocol(url.getProtocol());
410
 
 
411
 
        hostConfiguration.setHost(url.getHost(), null, port, protocol);
412
 
    }
413
 
 
414
 
    /**
415
 
     * Sets the host, port, protocol and credentials to be used when executing a
416
 
     * method.
417
 
     * <p>
418
 
     * Note that everything but the protocol, host and port of the
419
 
     * given <i>url</i> is ignored.
420
 
     * </p>
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
424
 
     *  
425
 
     * @exception IllegalArgumentException if the protocol is not http or https
426
 
     *
427
 
     * @see #getHostConfiguration()   
428
 
     * @see #getState()
429
 
     * 
430
 
     * @deprecated use {@link HostConfiguration} and {@link HttpState} 
431
 
     */
432
 
    public void startSession(URL url, Credentials creds) 
433
 
        throws IllegalArgumentException {
434
 
            
435
 
        LOG.trace("enter HttpClient.startSession(URL, Credentials)");
436
 
        getState().setCredentials(null, creds);
437
 
        startSession(url);
438
 
    }
439
 
 
440
 
    /**
441
 
     * Sets the host, port, protocol(http) and proxy to be used when executing a
442
 
     * method.
443
 
     * 
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
448
 
     *
449
 
     * @see #getHostConfiguration()
450
 
     * 
451
 
     * @deprecated use {@link HostConfiguration} 
452
 
     */
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);
456
 
    }
457
 
 
458
 
    /**
459
 
     * Sets the host, port, protocol and proxy to be used when executing a
460
 
     * method.
461
 
     * 
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
467
 
     * 
468
 
     * @see #getHostConfiguration()
469
 
     * 
470
 
     * @deprecated use {@link HostConfiguration} 
471
 
     */
472
 
    public void startSession(String host, int port, 
473
 
        String proxyhost, int proxyport, boolean secure) {
474
 
            
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);
479
 
    }
480
 
 
481
 
    /**
 
308
   /**
482
309
     * Executes the given {@link HttpMethod HTTP method}.
483
310
     *
484
311
     * @param method the {@link HttpMethod HTTP method} to execute.
485
312
     * @return the method's response code
486
313
     *
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.
491
318
     */
494
321
            
495
322
        LOG.trace("enter HttpClient.executeMethod(HttpMethod)");
496
323
        // execute this method and use its host configuration, if it has one
497
 
        return executeMethod(
498
 
            method.getHostConfiguration() != null 
499
 
            ? method.getHostConfiguration()
500
 
            : getHostConfiguration(), 
501
 
            method,
502
 
            null
503
 
        );
504
 
        
 
324
        return executeMethod(null, method, null);
505
325
    }
506
326
 
507
327
    /**
518
338
    *                    cannot be recovered from.
519
339
    * @since 2.0
520
340
    */
521
 
    public int executeMethod(HostConfiguration hostConfiguration, HttpMethod method)
 
341
    public int executeMethod(final HostConfiguration hostConfiguration, final HttpMethod method)
522
342
        throws IOException, HttpException {
523
343
    
524
344
        LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod)");
525
 
    
 
345
 
526
346
        return executeMethod(hostConfiguration, method, null); 
527
347
    }
528
348
    
533
353
     * {@link HostConfiguration host configuration} with the given custom 
534
354
     * {@link HttpState HTTP state}.
535
355
     *
536
 
     * @param hostConfiguration The {@link HostConfiguration host configuration} to use.
 
356
     * @param hostconfig The {@link HostConfiguration host configuration} to use.
537
357
     * @param method the {@link HttpMethod HTTP method} to execute.
538
358
     * @param state the {@link HttpState HTTP state} to use when executing the method.
539
359
     * If <code>null</code>, the state returned by {@link #getState} will be used instead.
546
366
     *                    cannot be recovered from.
547
367
     * @since 2.0
548
368
     */
549
 
    public int executeMethod(HostConfiguration hostConfiguration, 
550
 
        HttpMethod method, HttpState state)
 
369
    public int executeMethod(HostConfiguration hostconfig, 
 
370
        final HttpMethod method, final HttpState state)
551
371
        throws IOException, HttpException  {
552
372
            
553
373
        LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)");
555
375
        if (method == null) {
556
376
            throw new IllegalArgumentException("HttpMethod parameter may not be null");
557
377
        }
558
 
 
559
 
        int soTimeout = 0;
560
 
        boolean strictMode = false;
561
 
        int connectionTimeout = 0;
562
 
        long httpConnectionTimeout = 0;
563
 
        HostConfiguration defaultHostConfiguration = null;
564
 
 
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
567
 
         * for each item.
568
 
         */
569
 
        synchronized (this) {
570
 
            soTimeout = this.timeoutInMilliseconds;
571
 
            strictMode = this.strictMode;
572
 
            connectionTimeout = this.connectionTimeout;
573
 
            httpConnectionTimeout = this.httpConnectionTimeout;
574
 
            if (state == null) {
575
 
                state = getState();
576
 
            }
577
 
            defaultHostConfiguration = getHostConfiguration();
578
 
        }
579
 
 
580
 
        HostConfiguration methodConfiguration 
581
 
            = new HostConfiguration(hostConfiguration);
582
 
        
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()
591
 
                );
592
 
            }
593
 
            if (!methodConfiguration.isProxySet() 
594
 
                && defaultHostConfiguration.isProxySet()) {
595
 
                    
596
 
                methodConfiguration.setProxy(
597
 
                    defaultHostConfiguration.getProxyHost(),
598
 
                    defaultHostConfiguration.getProxyPort() 
599
 
                );   
600
 
            }
601
 
            if (methodConfiguration.getLocalAddress() == null
602
 
                && defaultHostConfiguration.getLocalAddress() != null) {
603
 
                    
604
 
                methodConfiguration.setLocalAddress(defaultHostConfiguration.getLocalAddress());
605
 
            }
606
 
        }
607
 
        
608
 
        HttpConnectionManager connmanager = this.httpConnectionManager;
609
 
        if (state.getHttpConnectionManager() != null) {
610
 
            connmanager = state.getHttpConnectionManager();
611
 
        }
612
 
 
613
 
        HttpConnection connection = connmanager.getConnection(
614
 
            methodConfiguration,
615
 
            httpConnectionTimeout
616
 
        );
617
 
 
618
 
        try {
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.
623
 
            
624
 
            method.setStrictMode(strictMode);
625
 
            
626
 
            if (!connection.isOpen()) {
627
 
                connection.setConnectionTimeout(connectionTimeout);
628
 
                connection.open();
629
 
                if (connection.isProxied() && connection.isSecure()) {
630
 
                    method = new ConnectMethod(method);
631
 
                }
632
 
            }
633
 
            connection.setSoTimeout(soTimeout);
634
 
            
635
 
        } catch (IOException e) {
636
 
            connection.releaseConnection();
637
 
            throw e;
638
 
        } catch (RuntimeException e) {
639
 
            connection.releaseConnection();
640
 
            throw e;
641
 
        }
642
 
        
643
 
        return method.execute(state, connection);
644
 
    }
645
 
 
646
 
    /**
647
 
     * @deprecated this method has no effect. {@link HttpMethod#releaseConnection()}
648
 
     * should be used to release resources after a HttpMethod has been executed.
649
 
     * 
650
 
     * @see HttpMethod#releaseConnection()
651
 
     */
652
 
    public void endSession() throws IOException {
 
378
        HostConfiguration defaulthostconfig = getHostConfiguration();
 
379
        if (hostconfig == null) {
 
380
            hostconfig = defaulthostconfig;
 
381
        }
 
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);
 
388
            }
 
389
        }
 
390
        
 
391
        HttpMethodDirector methodDirector = new HttpMethodDirector(
 
392
                getHttpConnectionManager(),
 
393
                hostconfig,
 
394
                this.params,
 
395
                (state == null ? getState() : state));
 
396
        methodDirector.executeMethod(method);
 
397
        return method.getStatusCode();
653
398
    }
654
399
 
655
400
    /**
723
468
        HttpConnectionManager httpConnectionManager
724
469
    ) {
725
470
        this.httpConnectionManager = httpConnectionManager;
 
471
        if (this.httpConnectionManager != null) {
 
472
            this.httpConnectionManager.getParams().setDefaults(this.params);
 
473
        }
 
474
    }
 
475
 
 
476
    /**
 
477
     * Returns {@link HttpClientParams HTTP protocol parameters} associated with this HttpClient.
 
478
     * 
 
479
     * @since 3.0
 
480
     * 
 
481
     * @see HttpClientParams
 
482
     */
 
483
    public HttpClientParams getParams() {
 
484
        return this.params;
 
485
    }
 
486
 
 
487
    /**
 
488
     * Assigns {@link HttpClientParams HTTP protocol parameters} for this HttpClient.
 
489
     * 
 
490
     * @since 3.0
 
491
     * 
 
492
     * @see HttpClientParams
 
493
     */
 
494
    public void setParams(final HttpClientParams params) {
 
495
        if (params == null) {
 
496
            throw new IllegalArgumentException("Parameters may not be null");
 
497
        }
 
498
        this.params = params;
726
499
    }
727
500
 
728
501
}