View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v 1.11.2.2 2003/09/05 02:09:41 mbecke Exp $ 3 * $Revision: 1.11.2.2 $ 4 * $Date: 2003/09/05 02:09:41 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient; 65 66 import org.apache.commons.httpclient.protocol.Protocol; 67 68 import java.net.InetAddress; 69 70 /*** 71 * Holds all of the variables needed to describe an HTTP connection to a host. This includes 72 * remote host, port and protocol, proxy host and port, local address, and virtual host. 73 * 74 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a> 75 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 76 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 77 * @author Laura Werner 78 * 79 * @since 2.0 80 */ 81 public class HostConfiguration implements Cloneable { 82 83 /*** The host to use. */ 84 private String host; 85 86 /*** The virtual host to use. */ 87 private String virtualHost; 88 89 /*** The port to use. */ 90 private int port; 91 92 /*** The protocol */ 93 private Protocol protocol; 94 95 /*** True if a host has been set */ 96 private boolean hostSet; 97 98 /*** The host name of the proxy server */ 99 private String proxyHost; 100 101 /*** The port number of the proxy server */ 102 private int proxyPort; 103 104 /*** True if a proxy server has been set */ 105 private boolean proxySet; 106 107 /*** The local address to use when creating the socket, or null to use the default */ 108 private InetAddress localAddress; 109 110 /*** 111 * Constructor for HostConfiguration. 112 */ 113 public HostConfiguration() { 114 115 this.host = null; 116 this.virtualHost = null; 117 this.port = -1; 118 this.protocol = null; 119 this.hostSet = false; 120 121 this.proxyHost = null; 122 this.proxyPort = -1; 123 this.proxySet = false; 124 this.localAddress = null; 125 } 126 127 /*** 128 * Copy constructor for HostConfiguration 129 * 130 * @param hostConfiguration the hostConfiguration to copy 131 */ 132 public HostConfiguration (HostConfiguration hostConfiguration) { 133 134 // wrap all of the assignments in a synchronized block to avoid 135 // having to negotiate the monitor for each method call 136 synchronized (hostConfiguration) { 137 this.host = hostConfiguration.getHost(); 138 this.virtualHost = hostConfiguration.getVirtualHost(); 139 this.port = hostConfiguration.getPort(); 140 this.protocol = hostConfiguration.getProtocol(); 141 this.hostSet = hostConfiguration.isHostSet(); 142 143 this.proxyHost = hostConfiguration.getProxyHost(); 144 this.proxyPort = hostConfiguration.getProxyPort(); 145 this.proxySet = hostConfiguration.isProxySet(); 146 this.localAddress = hostConfiguration.getLocalAddress(); 147 } 148 149 } 150 151 /*** 152 * @see java.lang.Object#clone() 153 */ 154 public Object clone() { 155 return new HostConfiguration(this); 156 } 157 158 /*** 159 * @see java.lang.Object#toString() 160 */ 161 public synchronized String toString() { 162 163 boolean appendComma = false; 164 165 StringBuffer b = new StringBuffer(50); 166 b.append("HostConfiguration["); 167 168 if (isHostSet()) { 169 appendComma = true; 170 b.append("host=").append(host); 171 b.append(", protocol=").append(protocol); 172 b.append(", port=").append(port); 173 if (virtualHost != null) { 174 b.append(", virtualHost=").append(virtualHost); 175 } 176 } 177 if (isProxySet()) { 178 if (appendComma) { 179 b.append(", "); 180 } else { 181 appendComma = true; 182 } 183 b.append("proxyHost=").append(proxyHost); 184 b.append(", proxyPort=").append(proxyPort); 185 } 186 if (localAddress != null) { 187 if (appendComma) { 188 b.append(", "); 189 } else { 190 appendComma = true; 191 } 192 b.append("localAddress=").append(localAddress); 193 } 194 195 b.append("]"); 196 return b.toString(); 197 } 198 199 /*** 200 * Tests if the host configuration equals the configuration set on the 201 * connection. True only if the host, port, protocol, local address and virtual address 202 * are equal. If no host configuration has been set false will be returned. 203 * 204 * @param connection the connection to test against 205 * @return <code>true</code> if the connection's host information equals that of this 206 * configuration 207 * 208 * @see #proxyEquals(HttpConnection) 209 * @see #isHostSet() 210 */ 211 public synchronized boolean hostEquals(HttpConnection connection) { 212 213 if (hostSet) { 214 if (!this.host.equalsIgnoreCase(connection.getHost())) { 215 return false; 216 } 217 if (this.virtualHost != null) { 218 if (!this.virtualHost.equalsIgnoreCase(connection.getVirtualHost())) { 219 return false; 220 } 221 } else { 222 if (connection.getVirtualHost() != null) { 223 return false; 224 } 225 } 226 if (this.port != connection.getPort()) { 227 return false; 228 } 229 if (!this.protocol.equals(connection.getProtocol())) { 230 return false; 231 } 232 if (this.localAddress != null) { 233 if (!this.localAddress.equals(connection.getLocalAddress())) { 234 return false; 235 } 236 } else { 237 if (connection.getLocalAddress() != null) { 238 return false; 239 } 240 } 241 return true; 242 } else { 243 return false; 244 } 245 246 } 247 248 /*** 249 * Tests if the proxy configuration equals the configuration set on the 250 * connection. True only if the proxyHost and proxyPort are equal. 251 * 252 * @param connection the connection to test against 253 * @return <code>true</code> if the connection's proxy information equals that of this 254 * configuration 255 * 256 * @see #hostEquals(HttpConnection) 257 */ 258 public synchronized boolean proxyEquals(HttpConnection connection) { 259 260 if (proxyHost == null) { 261 return connection.getProxyHost() == null; 262 } else { 263 return ( 264 proxyHost.equalsIgnoreCase(connection.getProxyHost()) 265 && proxyPort == connection.getProxyPort() 266 ); 267 } 268 } 269 270 /*** 271 * Returns true if the host is set. 272 * @return <code>true</code> if the host is set. 273 */ 274 public synchronized boolean isHostSet() { 275 return hostSet; 276 } 277 278 /*** 279 * Sets the given host, port and protocol 280 * 281 * @param host the host(IP or DNS name) 282 * @param port The port 283 * @param protocol The protocol. 284 */ 285 public synchronized void setHost(String host, int port, String protocol) { 286 setHost(host, null, port, Protocol.getProtocol(protocol)); 287 } 288 289 /*** 290 * Sets the given host, virtual host, port and protocol. 291 * 292 * @param host the host(IP or DNS name) 293 * @param virtualHost the virtual host name or <code>null</code> 294 * @param port the host port or -1 to use protocol default 295 * @param protocol the protocol 296 */ 297 public synchronized void setHost(String host, String virtualHost, int port, 298 Protocol protocol) { 299 300 if (host == null) { 301 throw new IllegalArgumentException("host must not be null"); 302 } 303 if (protocol == null) { 304 throw new IllegalArgumentException("protocol must not be null"); 305 } 306 307 this.host = host; 308 this.virtualHost = virtualHost; 309 this.port = port == -1 ? protocol.getDefaultPort() : port; 310 this.protocol = protocol; 311 312 this.hostSet = true; 313 } 314 315 /*** 316 * Sets the given host, port and protocol. 317 * 318 * @param host the host(IP or DNS name) 319 * @param port The port 320 * @param protocol the protocol 321 */ 322 public synchronized void setHost(String host, int port, Protocol protocol) { 323 setHost(host, null, port, protocol); 324 } 325 326 /*** 327 * Sets the given host and port. Uses the default protocol "http". 328 * 329 * @param host the host(IP or DNS name) 330 * @param port The port 331 */ 332 public synchronized void setHost(String host, int port) { 333 setHost(host, null, port, Protocol.getProtocol("http")); 334 } 335 336 /*** 337 * Set the given host. Uses the default protocol("http") and its port. 338 * 339 * @param host The host(IP or DNS name). 340 */ 341 public synchronized void setHost(String host) { 342 Protocol defaultProtocol = Protocol.getProtocol("http"); 343 setHost(host, null, defaultProtocol.getDefaultPort(), defaultProtocol); 344 } 345 346 /*** 347 * Sets the protocol, host and port from the given URI. 348 * @param uri the URI. 349 */ 350 public synchronized void setHost(URI uri) { 351 try { 352 setHost(uri.getHost(), uri.getPort(), uri.getScheme()); 353 } catch (URIException e) { 354 throw new IllegalArgumentException(e.toString()); 355 } 356 } 357 358 /*** 359 * Return the host url. 360 * 361 * @return The host url. 362 */ 363 public synchronized String getHostURL() { 364 365 if (!hostSet) { 366 throw new IllegalStateException("a default host must be set to " 367 + "create a host URL" 368 ); 369 } 370 371 String url = protocol.getScheme() + "://" + host; 372 373 if (port != -1 && port != protocol.getDefaultPort()) { 374 url += ":" + port; 375 } 376 377 return url; 378 } 379 380 /*** 381 * Returns the host. 382 * 383 * @return the host(IP or DNS name), or <code>null</code> if not set 384 * 385 * @see #isHostSet() 386 */ 387 public synchronized String getHost() { 388 return host; 389 } 390 391 /*** 392 * Returns the virtual host. 393 * 394 * @return the virtual host name, or <code>null</code> if not set 395 */ 396 public synchronized String getVirtualHost() { 397 return virtualHost; 398 } 399 400 /*** 401 * Returns the port. 402 * 403 * @return the host port, or <code>-1</code> if not set 404 * 405 * @see #isHostSet() 406 */ 407 public synchronized int getPort() { 408 return port; 409 } 410 411 /*** 412 * Returns the protocol. 413 * @return The protocol. 414 */ 415 public synchronized Protocol getProtocol() { 416 return protocol; 417 } 418 419 /*** 420 * Tests if the proxy host/port have been set. 421 * 422 * @return <code>true</code> if a proxy server has been set. 423 * 424 * @see #setProxy(String, int) 425 */ 426 public synchronized boolean isProxySet() { 427 return proxySet; 428 } 429 430 /*** 431 * Set the proxy settings. 432 * @param proxyHost The proxy host 433 * @param proxyPort The proxy port 434 */ 435 public synchronized void setProxy(String proxyHost, int proxyPort) { 436 437 this.proxyHost = proxyHost; 438 this.proxyPort = proxyPort; 439 440 this.proxySet = true; 441 } 442 443 /*** 444 * Returns the proxyHost. 445 * 446 * @return the proxy host, or <code>null</code> if not set 447 * 448 * @see #isProxySet() 449 */ 450 public synchronized String getProxyHost() { 451 return proxyHost; 452 } 453 454 /*** 455 * Returns the proxyPort. 456 * 457 * @return the proxy port, or <code>-1</code> if not set 458 * 459 * @see #isProxySet() 460 */ 461 public synchronized int getProxyPort() { 462 return proxyPort; 463 } 464 465 /*** 466 * Set the local address to be used when creating connections. 467 * If this is unset, the default address will be used. 468 * This is useful for specifying the interface to use on multi-homed or clustered systems. 469 * 470 * @param localAddress the local address to use 471 */ 472 public synchronized void setLocalAddress(InetAddress localAddress) { 473 this.localAddress = localAddress; 474 } 475 476 /*** 477 * Return the local address to be used when creating connections. 478 * If this is unset, the default address should be used. 479 * 480 * @return the local address to be used when creating Sockets, or <code>null</code> 481 */ 482 public synchronized InetAddress getLocalAddress() { 483 return this.localAddress; 484 } 485 486 /*** 487 * @see java.lang.Object#equals(java.lang.Object) 488 */ 489 public synchronized boolean equals(Object o) { 490 491 if (o instanceof HostConfiguration) { 492 493 // shortcut if we're comparing with ourselves 494 if (o == this) { 495 return true; 496 } 497 498 HostConfiguration config = (HostConfiguration) o; 499 500 if (hostSet) { 501 if (!host.equalsIgnoreCase(config.getHost())) { 502 return false; 503 } 504 if (virtualHost != null) { 505 if (!virtualHost.equalsIgnoreCase(config.getVirtualHost())) { 506 return false; 507 } 508 } else { 509 if (config.getVirtualHost() != null) { 510 return false; 511 } 512 } 513 if (port != config.getPort()) { 514 return false; 515 } 516 if (!protocol.equals(config.getProtocol())) { 517 return false; 518 } 519 } else if (config.isHostSet()) { 520 return false; 521 } 522 if (proxyHost != null) { 523 if (!proxyHost.equalsIgnoreCase (config.getProxyHost()) 524 || proxyPort != config.getProxyPort()) { 525 // either proxyHost or proxyPort don't match 526 return false; 527 } 528 } else if (config.getProxyHost() != null) { 529 return false; 530 } 531 if (localAddress != null) { 532 if (!localAddress.equals(config.getLocalAddress())) { 533 return false; 534 } 535 } else { 536 if (config.getLocalAddress() != null) { 537 return false; 538 } 539 } 540 541 // everything matches 542 return true; 543 544 } else { 545 return false; 546 } 547 548 } 549 550 /*** 551 * @see java.lang.Object#hashCode() 552 */ 553 public int hashCode() { 554 555 if (host != null) { 556 return host.hashCode(); 557 } else if (proxyHost != null) { 558 return proxyHost.hashCode(); 559 } else { 560 return super.hashCode(); 561 } 562 } 563 }

This page was automatically generated by Maven