1 /** 2 * Copyright 2014 Internet2 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 /* 17 * $Header: /home/hagleyj/i2mi/grouper-misc/grouperClient/src/ext/edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/HttpClient.java,v 1.1 2008-11-30 10:57:19 mchyzer Exp $ 18 * $Revision: 1.1 $ 19 * $Date: 2008-11-30 10:57:19 $ 20 * 21 * ==================================================================== 22 * 23 * Licensed to the Apache Software Foundation (ASF) under one or more 24 * contributor license agreements. See the NOTICE file distributed with 25 * this work for additional information regarding copyright ownership. 26 * The ASF licenses this file to You under the Apache License, Version 2.0 27 * (the "License"); you may not use this file except in compliance with 28 * the License. You may obtain a copy of the License at 29 * 30 * http://www.apache.org/licenses/LICENSE-2.0 31 * 32 * Unless required by applicable law or agreed to in writing, software 33 * distributed under the License is distributed on an "AS IS" BASIS, 34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 * See the License for the specific language governing permissions and 36 * limitations under the License. 37 * ==================================================================== 38 * 39 * This software consists of voluntary contributions made by many 40 * individuals on behalf of the Apache Software Foundation. For more 41 * information on the Apache Software Foundation, please see 42 * <http://www.apache.org/>. 43 * 44 */ 45 46 package edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient; 47 48 import java.io.IOException; 49 import java.security.Provider; 50 import java.security.Security; 51 52 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpClientParams; 53 import edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log; 54 import edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.LogFactory; 55 56 /** 57 * <p> 58 * An HTTP "user-agent", containing an {@link HttpState HTTP state} and 59 * one or more {@link HttpConnection HTTP connections}, to which 60 * {@link HttpMethod HTTP methods} can be applied. 61 * </p> 62 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 63 * @author <a href="mailto:rwaldhoff@apache.org">Rodney Waldhoff</a> 64 * @author Sean C. Sullivan 65 * @author <a href="mailto:dion@apache.org">dIon Gillard</a> 66 * @author Ortwin Gl?ck 67 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a> 68 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 69 * @author Sam Maloney 70 * @author Laura Werner 71 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 72 * 73 * @version $Revision: 1.1 $ $Date: 2008-11-30 10:57:19 $ 74 */ 75 public class HttpClient { 76 77 78 // -------------------------------------------------------------- Constants 79 80 /** Log object for this class. */ 81 private static final Log LOG = LogFactory.getLog(HttpClient.class); 82 83 static { 84 85 if (LOG.isDebugEnabled()) { 86 try { 87 LOG.debug("Java version: " + System.getProperty("java.version")); 88 LOG.debug("Java vendor: " + System.getProperty("java.vendor")); 89 LOG.debug("Java class path: " + System.getProperty("java.class.path")); 90 LOG.debug("Operating system name: " + System.getProperty("os.name")); 91 LOG.debug("Operating system architecture: " + System.getProperty("os.arch")); 92 LOG.debug("Operating system version: " + System.getProperty("os.version")); 93 94 Provider[] providers = Security.getProviders(); 95 for (int i = 0; i < providers.length; i++) { 96 Provider provider = providers[i]; 97 LOG.debug(provider.getName() + " " + provider.getVersion() 98 + ": " + provider.getInfo()); 99 } 100 } catch (SecurityException ignore) { 101 } 102 } 103 } 104 // ----------------------------------------------------------- Constructors 105 106 /** 107 * Creates an instance of HttpClient using default {@link HttpClientParams parameter set}. 108 * 109 * @see HttpClientParams 110 */ 111 public HttpClient() { 112 this(new HttpClientParams()); 113 } 114 115 /** 116 * Creates an instance of HttpClient using the given 117 * {@link HttpClientParams parameter set}. 118 * 119 * @param params The {@link HttpClientParams parameters} to use. 120 * 121 * @see HttpClientParams 122 * 123 * @since 3.0 124 */ 125 public HttpClient(HttpClientParams params) { 126 super(); 127 if (params == null) { 128 throw new IllegalArgumentException("Params may not be null"); 129 } 130 this.params = params; 131 this.httpConnectionManager = null; 132 Class clazz = params.getConnectionManagerClass(); 133 if (clazz != null) { 134 try { 135 this.httpConnectionManager = (HttpConnectionManager) clazz.newInstance(); 136 } catch (Exception e) { 137 LOG.warn("Error instantiating connection manager class, defaulting to" 138 + " SimpleHttpConnectionManager", 139 e); 140 } 141 } 142 if (this.httpConnectionManager == null) { 143 this.httpConnectionManager = new SimpleHttpConnectionManager(); 144 } 145 if (this.httpConnectionManager != null) { 146 this.httpConnectionManager.getParams().setDefaults(this.params); 147 } 148 } 149 150 /** 151 * Creates an instance of HttpClient with a user specified 152 * {@link HttpClientParams parameter set} and 153 * {@link HttpConnectionManager HTTP connection manager}. 154 * 155 * @param params The {@link HttpClientParams parameters} to use. 156 * @param httpConnectionManager The {@link HttpConnectionManager connection manager} 157 * to use. 158 * 159 * @since 3.0 160 */ 161 public HttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager) { 162 super(); 163 if (httpConnectionManager == null) { 164 throw new IllegalArgumentException("httpConnectionManager cannot be null"); 165 } 166 if (params == null) { 167 throw new IllegalArgumentException("Params may not be null"); 168 } 169 this.params = params; 170 this.httpConnectionManager = httpConnectionManager; 171 this.httpConnectionManager.getParams().setDefaults(this.params); 172 } 173 174 /** 175 * Creates an instance of HttpClient with a user specified 176 * {@link HttpConnectionManager HTTP connection manager}. 177 * 178 * @param httpConnectionManager The {@link HttpConnectionManager connection manager} 179 * to use. 180 * 181 * @since 2.0 182 */ 183 public HttpClient(HttpConnectionManager httpConnectionManager) { 184 this(new HttpClientParams(), httpConnectionManager); 185 } 186 187 // ----------------------------------------------------- Instance Variables 188 189 /** 190 * The {@link HttpConnectionManager connection manager} being used to manage 191 * connections for this HttpClient 192 */ 193 private HttpConnectionManager httpConnectionManager; 194 195 /** 196 * The {@link HttpState HTTP state} associated with this HttpClient. 197 */ 198 private HttpStateleware/grouperClientExt/org/apache/commons/httpclient/HttpState.html#HttpState">HttpState state = new HttpState(); 199 200 /** 201 * The {@link HttpClientParams collection of parameters} associated with this HttpClient. 202 */ 203 private HttpClientParams params = null; 204 205 /** 206 * The {@link HostConfiguration host configuration} associated with 207 * the HttpClient 208 */ 209 private HostConfigurationExt/org/apache/commons/httpclient/HostConfiguration.html#HostConfiguration">HostConfiguration hostConfiguration = new HostConfiguration(); 210 211 // ------------------------------------------------------------- Properties 212 213 /** 214 * Returns {@link HttpState HTTP state} associated with the HttpClient. 215 * 216 * @see #setState(HttpState) 217 * @return the shared client state 218 */ 219 public synchronized HttpState getState() { 220 return state; 221 } 222 223 /** 224 * Assigns {@link HttpState HTTP state} for the HttpClient. 225 * 226 * @see #getState() 227 * @param state the new {@link HttpState HTTP state} for the client 228 */ 229 public synchronized void setState(HttpState state) { 230 this.state = state; 231 } 232 233 /** 234 * Defines how strictly the method follows the HTTP protocol specification 235 * (see RFC 2616 and other relevant RFCs). 236 * 237 * In the strict mode the method precisely 238 * implements the requirements of the specification, whereas in non-strict mode 239 * it attempts to mimic the exact behaviour of commonly used HTTP agents, 240 * which many HTTP servers expect. 241 * 242 * @param strictMode <tt>true</tt> for strict mode, <tt>false</tt> otherwise 243 * 244 * @see #isStrictMode() 245 * 246 * @deprecated Use {@link HttpClientParams#setParameter(String, Object)} 247 * to exercise a more granular control over HTTP protocol strictness. 248 */ 249 public synchronized void setStrictMode(boolean strictMode) { 250 if (strictMode) { 251 this.params.makeStrict(); 252 } else { 253 this.params.makeLenient(); 254 } 255 } 256 257 /** 258 * Returns the value of the strict mode flag. 259 * 260 * @return <tt>true</tt> if strict mode is enabled, <tt>false</tt> otherwise 261 * 262 * @see #setStrictMode(boolean) 263 * 264 * @deprecated Use 265 * {@link edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpClientParams#getParameter(String)} 266 * to exercise a more granular control over HTTP protocol strictness. 267 */ 268 public synchronized boolean isStrictMode() { 269 return false; 270 } 271 272 /** 273 * Sets the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the 274 * timeout for waiting for data. A timeout value of zero is interpreted as an 275 * infinite timeout. 276 * 277 * @param newTimeoutInMilliseconds Timeout in milliseconds 278 * 279 * @deprecated Use 280 * {@link edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)}, 281 * {@link HttpConnectionManager#getParams()}. 282 * 283 */ 284 public synchronized void setTimeout(int newTimeoutInMilliseconds) { 285 this.params.setSoTimeout(newTimeoutInMilliseconds); 286 } 287 288 /** 289 * Sets the timeout in milliseconds used when retrieving an 290 * {@link HttpConnection HTTP connection} from the 291 * {@link HttpConnectionManager HTTP connection manager}. 292 * 293 * @param timeout the timeout in milliseconds 294 * 295 * @see HttpConnectionManager#getConnection(HostConfiguration, long) 296 * 297 * @deprecated Use 298 * {@link edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpClientParams#setConnectionManagerTimeout(long)}, 299 * {@link HttpClient#getParams()} 300 */ 301 public synchronized void setHttpConnectionFactoryTimeout(long timeout) { 302 this.params.setConnectionManagerTimeout(timeout); 303 } 304 305 /** 306 * Sets the timeout until a connection is etablished. A value of zero 307 * means the timeout is not used. The default value is zero. 308 * 309 * @see HttpConnection#setConnectionTimeout(int) 310 * @param newTimeoutInMilliseconds Timeout in milliseconds. 311 * 312 * @deprecated Use 313 * {@link edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)}, 314 * {@link HttpConnectionManager#getParams()}. 315 */ 316 public synchronized void setConnectionTimeout(int newTimeoutInMilliseconds) { 317 this.httpConnectionManager.getParams().setConnectionTimeout(newTimeoutInMilliseconds); 318 } 319 320 // --------------------------------------------------------- Public Methods 321 322 /** 323 * Executes the given {@link HttpMethod HTTP method}. 324 * 325 * @param method the {@link HttpMethod HTTP method} to execute. 326 * @return the method's response code 327 * 328 * @throws IOException If an I/O (transport) error occurs. Some transport exceptions 329 * can be recovered from. 330 * @throws HttpException If a protocol exception occurs. Usually protocol exceptions 331 * cannot be recovered from. 332 */ 333 public int executeMethod(HttpMethod method) 334 throws IOException, HttpException { 335 336 LOG.trace("enter HttpClient.executeMethod(HttpMethod)"); 337 // execute this method and use its host configuration, if it has one 338 return executeMethod(null, method, null); 339 } 340 341 /** 342 * Executes the given {@link HttpMethod HTTP method} using custom 343 * {@link HostConfiguration host configuration}. 344 * 345 * @param hostConfiguration The {@link HostConfiguration host configuration} to use. 346 * If <code>null</code>, the host configuration returned by {@link #getHostConfiguration} will be used. 347 * @param method the {@link HttpMethod HTTP method} to execute. 348 * @return the method's response code 349 * 350 * @throws IOException If an I/O (transport) error occurs. Some transport exceptions 351 * can be recovered from. 352 * @throws HttpException If a protocol exception occurs. Usually protocol exceptions 353 * cannot be recovered from. 354 * @since 2.0 355 */ 356 public int executeMethod(final HostConfiguration hostConfiguration, final HttpMethod method) 357 throws IOException, HttpException { 358 359 LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod)"); 360 361 return executeMethod(hostConfiguration, method, null); 362 } 363 364 365 366 /** 367 * Executes the given {@link HttpMethod HTTP method} using the given custom 368 * {@link HostConfiguration host configuration} with the given custom 369 * {@link HttpState HTTP state}. 370 * 371 * @param hostconfig The {@link HostConfiguration host configuration} to use. 372 * If <code>null</code>, the host configuration returned by {@link #getHostConfiguration} will be used. 373 * @param method the {@link HttpMethod HTTP method} to execute. 374 * @param state the {@link HttpState HTTP state} to use when executing the method. 375 * If <code>null</code>, the state returned by {@link #getState} will be used. 376 * 377 * @return the method's response code 378 * 379 * @throws IOException If an I/O (transport) error occurs. Some transport exceptions 380 * can be recovered from. 381 * @throws HttpException If a protocol exception occurs. Usually protocol exceptions 382 * cannot be recovered from. 383 * @since 2.0 384 */ 385 public int executeMethod(HostConfiguration hostconfig, 386 final HttpMethod method, final HttpState state) 387 throws IOException, HttpException { 388 389 LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)"); 390 391 if (method == null) { 392 throw new IllegalArgumentException("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 HttpMethodDirectorntExt/org/apache/commons/httpclient/HttpMethodDirector.html#HttpMethodDirector">HttpMethodDirector methodDirector = new HttpMethodDirector( 408 getHttpConnectionManager(), 409 hostconfig, 410 this.params, 411 (state == null ? getState() : state)); 412 methodDirector.executeMethod(method); 413 return method.getStatusCode(); 414 } 415 416 /** 417 * Returns the default host. 418 * 419 * @return The default host. 420 * 421 * @deprecated use #getHostConfiguration() 422 */ 423 public String getHost() { 424 return hostConfiguration.getHost(); 425 } 426 427 /** 428 * Returns the default port. 429 * 430 * @return The default port. 431 * 432 * @deprecated use #getHostConfiguration() 433 */ 434 public int getPort() { 435 return hostConfiguration.getPort(); 436 } 437 438 /** 439 * Returns the {@link HostConfiguration host configuration} associated with the 440 * HttpClient. 441 * 442 * @return {@link HostConfiguration host configuration} 443 * 444 * @since 2.0 445 */ 446 public synchronized HostConfiguration getHostConfiguration() { 447 return hostConfiguration; 448 } 449 450 /** 451 * Assigns the {@link HostConfiguration host configuration} to use with the 452 * HttpClient. 453 * 454 * @param hostConfiguration The {@link HostConfiguration host configuration} to set 455 * 456 * @since 2.0 457 */ 458 public synchronized void setHostConfiguration(HostConfiguration hostConfiguration) { 459 this.hostConfiguration = hostConfiguration; 460 } 461 462 /** 463 * Returns the {@link HttpConnectionManager HTTP connection manager} associated 464 * with the HttpClient. 465 * 466 * @return {@link HttpConnectionManager HTTP connection manager} 467 * 468 * @since 2.0 469 */ 470 public synchronized HttpConnectionManager getHttpConnectionManager() { 471 return httpConnectionManager; 472 } 473 474 /** 475 * Assigns the {@link HttpConnectionManager HTTP connection manager} to use with 476 * the HttpClient. 477 * 478 * @param httpConnectionManager The {@link HttpConnectionManager HTTP connection manager} 479 * to set 480 * 481 * @since 2.0 482 */ 483 public synchronized void setHttpConnectionManager( 484 HttpConnectionManager httpConnectionManager 485 ) { 486 this.httpConnectionManager = httpConnectionManager; 487 if (this.httpConnectionManager != null) { 488 this.httpConnectionManager.getParams().setDefaults(this.params); 489 } 490 } 491 492 /** 493 * Returns {@link HttpClientParams HTTP protocol parameters} associated with this HttpClient. 494 * 495 * @since 3.0 496 * 497 * @see HttpClientParams 498 */ 499 public HttpClientParams getParams() { 500 return this.params; 501 } 502 503 /** 504 * Assigns {@link HttpClientParams HTTP protocol parameters} for this HttpClient. 505 * 506 * @since 3.0 507 * 508 * @see HttpClientParams 509 */ 510 public void setParams(final HttpClientParams params) { 511 if (params == null) { 512 throw new IllegalArgumentException("Parameters may not be null"); 513 } 514 this.params = params; 515 } 516 517 }