1 31 32 package org.apache.commons.httpclient; 33 34 import org.apache.commons.httpclient.protocol.Protocol; 35 36 import java.net.InetAddress ; 37 38 49 public class HostConfiguration implements Cloneable { 50 51 52 private String host; 53 54 55 private String virtualHost; 56 57 58 private int port; 59 60 61 private Protocol protocol; 62 63 64 private boolean hostSet; 65 66 67 private String proxyHost; 68 69 70 private int proxyPort; 71 72 73 private boolean proxySet; 74 75 76 private InetAddress localAddress; 77 78 81 public HostConfiguration() { 82 83 this.host = null; 84 this.virtualHost = null; 85 this.port = -1; 86 this.protocol = null; 87 this.hostSet = false; 88 89 this.proxyHost = null; 90 this.proxyPort = -1; 91 this.proxySet = false; 92 this.localAddress = null; 93 } 94 95 100 public HostConfiguration (HostConfiguration hostConfiguration) { 101 102 synchronized (hostConfiguration) { 105 this.host = hostConfiguration.getHost(); 106 this.virtualHost = hostConfiguration.getVirtualHost(); 107 this.port = hostConfiguration.getPort(); 108 this.protocol = hostConfiguration.getProtocol(); 109 this.hostSet = hostConfiguration.isHostSet(); 110 111 this.proxyHost = hostConfiguration.getProxyHost(); 112 this.proxyPort = hostConfiguration.getProxyPort(); 113 this.proxySet = hostConfiguration.isProxySet(); 114 this.localAddress = hostConfiguration.getLocalAddress(); 115 } 116 117 } 118 119 122 public Object clone() { 123 return new HostConfiguration(this); 124 } 125 126 129 public synchronized String toString() { 130 131 boolean appendComma = false; 132 133 StringBuffer b = new StringBuffer (50); 134 b.append("HostConfiguration["); 135 136 if (isHostSet()) { 137 appendComma = true; 138 b.append("host=").append(host); 139 b.append(", protocol=").append(protocol); 140 b.append(", port=").append(port); 141 if (virtualHost != null) { 142 b.append(", virtualHost=").append(virtualHost); 143 } 144 } 145 if (isProxySet()) { 146 if (appendComma) { 147 b.append(", "); 148 } else { 149 appendComma = true; 150 } 151 b.append("proxyHost=").append(proxyHost); 152 b.append(", proxyPort=").append(proxyPort); 153 } 154 if (localAddress != null) { 155 if (appendComma) { 156 b.append(", "); 157 } else { 158 appendComma = true; 159 } 160 b.append("localAddress=").append(localAddress); 161 } 162 163 b.append("]"); 164 return b.toString(); 165 } 166 167 179 public synchronized boolean hostEquals(HttpConnection connection) { 180 181 if (hostSet) { 182 if (!this.host.equalsIgnoreCase(connection.getHost())) { 183 return false; 184 } 185 if (this.virtualHost != null) { 186 if (!this.virtualHost.equalsIgnoreCase(connection.getVirtualHost())) { 187 return false; 188 } 189 } else { 190 if (connection.getVirtualHost() != null) { 191 return false; 192 } 193 } 194 if (this.port != connection.getPort()) { 195 return false; 196 } 197 if (!this.protocol.equals(connection.getProtocol())) { 198 return false; 199 } 200 if (this.localAddress != null) { 201 if (!this.localAddress.equals(connection.getLocalAddress())) { 202 return false; 203 } 204 } else { 205 if (connection.getLocalAddress() != null) { 206 return false; 207 } 208 } 209 return true; 210 } else { 211 return false; 212 } 213 214 } 215 216 226 public synchronized boolean proxyEquals(HttpConnection connection) { 227 228 if (proxyHost == null) { 229 return connection.getProxyHost() == null; 230 } else { 231 return ( 232 proxyHost.equalsIgnoreCase(connection.getProxyHost()) 233 && proxyPort == connection.getProxyPort() 234 ); 235 } 236 } 237 238 242 public synchronized boolean isHostSet() { 243 return hostSet; 244 } 245 246 253 public synchronized void setHost(String host, int port, String protocol) { 254 setHost(host, null, port, Protocol.getProtocol(protocol)); 255 } 256 257 265 public synchronized void setHost(String host, String virtualHost, int port, 266 Protocol protocol) { 267 268 if (host == null) { 269 throw new IllegalArgumentException ("host must not be null"); 270 } 271 if (protocol == null) { 272 throw new IllegalArgumentException ("protocol must not be null"); 273 } 274 275 this.host = host; 276 this.virtualHost = virtualHost; 277 this.port = port == -1 ? protocol.getDefaultPort() : port; 278 this.protocol = protocol; 279 280 this.hostSet = true; 281 } 282 283 290 public synchronized void setHost(String host, int port, Protocol protocol) { 291 setHost(host, null, port, protocol); 292 } 293 294 300 public synchronized void setHost(String host, int port) { 301 setHost(host, null, port, Protocol.getProtocol("http")); 302 } 303 304 309 public synchronized void setHost(String host) { 310 Protocol defaultProtocol = Protocol.getProtocol("http"); 311 setHost(host, null, defaultProtocol.getDefaultPort(), defaultProtocol); 312 } 313 314 318 public synchronized void setHost(URI uri) { 319 try { 320 setHost(uri.getHost(), uri.getPort(), uri.getScheme()); 321 } catch (URIException e) { 322 throw new IllegalArgumentException (e.toString()); 323 } 324 } 325 326 331 public synchronized String getHostURL() { 332 333 if (!hostSet) { 334 throw new IllegalStateException ("a default host must be set to " 335 + "create a host URL" 336 ); 337 } 338 339 String url = protocol.getScheme() + "://" + host; 340 341 if (port != -1 && port != protocol.getDefaultPort()) { 342 url += ":" + port; 343 } 344 345 return url; 346 } 347 348 355 public synchronized String getHost() { 356 return host; 357 } 358 359 364 public synchronized String getVirtualHost() { 365 return virtualHost; 366 } 367 368 375 public synchronized int getPort() { 376 return port; 377 } 378 379 383 public synchronized Protocol getProtocol() { 384 return protocol; 385 } 386 387 394 public synchronized boolean isProxySet() { 395 return proxySet; 396 } 397 398 403 public synchronized void setProxy(String proxyHost, int proxyPort) { 404 405 this.proxyHost = proxyHost; 406 this.proxyPort = proxyPort; 407 408 this.proxySet = true; 409 } 410 411 418 public synchronized String getProxyHost() { 419 return proxyHost; 420 } 421 422 429 public synchronized int getProxyPort() { 430 return proxyPort; 431 } 432 433 440 public synchronized void setLocalAddress(InetAddress localAddress) { 441 this.localAddress = localAddress; 442 } 443 444 450 public synchronized InetAddress getLocalAddress() { 451 return this.localAddress; 452 } 453 454 457 public synchronized boolean equals(Object o) { 458 459 if (o instanceof HostConfiguration) { 460 461 if (o == this) { 463 return true; 464 } 465 466 HostConfiguration config = (HostConfiguration) o; 467 468 if (hostSet) { 469 if (!host.equalsIgnoreCase(config.getHost())) { 470 return false; 471 } 472 if (virtualHost != null) { 473 if (!virtualHost.equalsIgnoreCase(config.getVirtualHost())) { 474 return false; 475 } 476 } else { 477 if (config.getVirtualHost() != null) { 478 return false; 479 } 480 } 481 if (port != config.getPort()) { 482 return false; 483 } 484 if (!protocol.equals(config.getProtocol())) { 485 return false; 486 } 487 } else if (config.isHostSet()) { 488 return false; 489 } 490 if (proxyHost != null) { 491 if (!proxyHost.equalsIgnoreCase (config.getProxyHost()) 492 || proxyPort != config.getProxyPort()) { 493 return false; 495 } 496 } else if (config.getProxyHost() != null) { 497 return false; 498 } 499 if (localAddress != null) { 500 if (!localAddress.equals(config.getLocalAddress())) { 501 return false; 502 } 503 } else { 504 if (config.getLocalAddress() != null) { 505 return false; 506 } 507 } 508 509 return true; 511 512 } else { 513 return false; 514 } 515 516 } 517 518 521 public int hashCode() { 522 523 if (host != null) { 524 return host.hashCode(); 525 } else if (proxyHost != null) { 526 return proxyHost.hashCode(); 527 } else { 528 return super.hashCode(); 529 } 530 } 531 } 532 | Popular Tags |