1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.IOException ; 35 import java.net.URL ; 36 import java.security.Security ; 37 import java.security.Provider ; 38 39 import org.apache.commons.httpclient.protocol.Protocol; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 44 63 public class HttpClient { 64 65 66 68 69 private static final Log LOG = LogFactory.getLog(HttpClient.class); 70 71 static { 72 if (LOG.isDebugEnabled()) { 73 try { 74 LOG.debug("Java version: " + System.getProperty("java.version")); 75 LOG.debug("Java vendor: " + System.getProperty("java.vendor")); 76 LOG.debug("Java class path: " + System.getProperty("java.class.path")); 77 LOG.debug("Operating system name: " + System.getProperty("os.name")); 78 LOG.debug("Operating system architecture: " + System.getProperty("os.arch")); 79 LOG.debug("Operating system version: " + System.getProperty("os.version")); 80 81 Provider [] providers = Security.getProviders(); 82 for (int i = 0; i < providers.length; i++) { 83 Provider provider = providers[i]; 84 LOG.debug(provider.getName() + " " + provider.getVersion() 85 + ": " + provider.getInfo()); 86 } 87 } catch(SecurityException ignore) { 88 } 89 } 90 } 91 93 99 public HttpClient() { 100 this(new SimpleHttpConnectionManager()); 101 } 102 103 110 public HttpClient(HttpConnectionManager httpConnectionManager) { 111 112 if (httpConnectionManager == null) { 113 throw new IllegalArgumentException ("httpConnectionManager cannot be null"); 114 } 115 116 this.state = new HttpState(); 117 this.httpConnectionManager = httpConnectionManager; 118 119 this.hostConfiguration = new HostConfiguration(); 120 121 } 122 123 125 129 private HttpConnectionManager httpConnectionManager; 130 131 134 private HttpState state; 135 136 140 private long httpConnectionTimeout = 0; 141 142 145 private int timeoutInMilliseconds = 0; 146 147 150 private int connectionTimeout = 0; 151 152 156 private HostConfiguration hostConfiguration; 157 158 161 private boolean strictMode = false; 162 163 165 171 public synchronized HttpState getState() { 172 return state; 173 } 174 175 181 public synchronized void setState(HttpState state) { 182 this.state = state; 183 } 184 185 198 public synchronized void setStrictMode(boolean strictMode) { 199 this.strictMode = strictMode; 200 } 201 202 209 public synchronized boolean isStrictMode() { 210 return strictMode; 211 } 212 213 220 public synchronized void setTimeout(int newTimeoutInMilliseconds) { 221 this.timeoutInMilliseconds = newTimeoutInMilliseconds; 222 } 223 224 233 public synchronized void setHttpConnectionFactoryTimeout(long timeout) { 234 this.httpConnectionTimeout = timeout; 235 } 236 237 245 public synchronized void setConnectionTimeout(int newTimeoutInMilliseconds) { 246 this.connectionTimeout = newTimeoutInMilliseconds; 247 } 248 249 251 262 public void startSession(String host, int port) { 263 LOG.trace("enter HttpClient.startSession(String, int)"); 264 startSession(host, port, false); 265 } 266 267 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 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 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 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) { 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) { 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 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 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 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 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 492 public int executeMethod(HttpMethod method) 493 throws IOException , HttpException { 494 495 LOG.trace("enter HttpClient.executeMethod(HttpMethod)"); 496 return executeMethod( 498 method.getHostConfiguration() != null 499 ? method.getHostConfiguration() 500 : getHostConfiguration(), 501 method, 502 null 503 ); 504 505 } 506 507 521 public int executeMethod(HostConfiguration hostConfiguration, HttpMethod method) 522 throws IOException , HttpException { 523 524 LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod)"); 525 526 return executeMethod(hostConfiguration, method, null); 527 } 528 529 530 531 549 public int executeMethod(HostConfiguration hostConfiguration, 550 HttpMethod method, HttpState state) 551 throws IOException , HttpException { 552 553 LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)"); 554 555 if (method == null) { 556 throw new IllegalArgumentException ("HttpMethod parameter may not be null"); 557 } 558 559 int soTimeout = 0; 560 boolean strictMode = false; 561 int connectionTimeout = 0; 562 long httpConnectionTimeout = 0; 563 HostConfiguration defaultHostConfiguration = null; 564 565 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 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 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 652 public void endSession() throws IOException { 653 } 654 655 662 public String getHost() { 663 return hostConfiguration.getHost(); 664 } 665 666 673 public int getPort() { 674 return hostConfiguration.getPort(); 675 } 676 677 685 public synchronized HostConfiguration getHostConfiguration() { 686 return hostConfiguration; 687 } 688 689 697 public synchronized void setHostConfiguration(HostConfiguration hostConfiguration) { 698 this.hostConfiguration = hostConfiguration; 699 } 700 701 709 public synchronized HttpConnectionManager getHttpConnectionManager() { 710 return httpConnectionManager; 711 } 712 713 722 public synchronized void setHttpConnectionManager( 723 HttpConnectionManager httpConnectionManager 724 ) { 725 this.httpConnectionManager = httpConnectionManager; 726 } 727 728 } 729 | Popular Tags |