1 30 31 package org.apache.commons.httpclient; 32 33 import org.apache.commons.httpclient.protocol.Protocol; 34 import org.apache.commons.httpclient.util.LangUtils; 35 36 47 public class HttpHost implements Cloneable { 48 49 50 private String hostname = null; 51 52 53 private int port = -1; 54 55 56 private Protocol protocol = null; 57 58 65 public HttpHost(final String hostname, int port, final Protocol protocol) { 66 super(); 67 if (hostname == null) { 68 throw new IllegalArgumentException ("Host name may not be null"); 69 } 70 if (protocol == null) { 71 throw new IllegalArgumentException ("Protocol may not be null"); 72 } 73 this.hostname = hostname; 74 this.protocol = protocol; 75 if (port >= 0) { 76 this.port = port; 77 } else { 78 this.port = this.protocol.getDefaultPort(); 79 } 80 } 81 82 88 public HttpHost(final String hostname, int port) { 89 this(hostname, port, Protocol.getProtocol("http")); 90 } 91 92 97 public HttpHost(final String hostname) { 98 this(hostname, -1, Protocol.getProtocol("http")); 99 } 100 101 106 public HttpHost(final URI uri) throws URIException { 107 this(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getScheme())); 108 } 109 110 115 public HttpHost (final HttpHost httphost) { 116 super(); 117 init(httphost); 118 } 119 120 private void init(final HttpHost httphost) { 121 this.hostname = httphost.hostname; 122 this.port = httphost.port; 123 this.protocol = httphost.protocol; 124 } 125 126 130 public Object clone() throws CloneNotSupportedException { 131 HttpHost copy = (HttpHost) super.clone(); 132 copy.init(this); 133 return copy; 134 } 135 136 141 public String getHostName() { 142 return this.hostname; 143 } 144 145 150 public int getPort() { 151 return this.port; 152 } 153 154 158 public Protocol getProtocol() { 159 return this.protocol; 160 } 161 162 167 public String toURI() { 168 StringBuffer buffer = new StringBuffer (50); 169 buffer.append(this.protocol.getScheme()); 170 buffer.append("://"); 171 buffer.append(this.hostname); 172 if (this.port != this.protocol.getDefaultPort()) { 173 buffer.append(':'); 174 buffer.append(this.port); 175 } 176 return buffer.toString(); 177 } 178 179 182 public String toString() { 183 StringBuffer buffer = new StringBuffer (50); 184 buffer.append(toURI()); 185 return buffer.toString(); 186 } 187 188 191 public boolean equals(final Object o) { 192 193 if (o instanceof HttpHost) { 194 if (o == this) { 196 return true; 197 } 198 HttpHost that = (HttpHost) o; 199 if (!this.hostname.equalsIgnoreCase(that.hostname)) { 200 return false; 201 } 202 if (this.port != that.port) { 203 return false; 204 } 205 if (!this.protocol.equals(that.protocol)) { 206 return false; 207 } 208 return true; 210 } else { 211 return false; 212 } 213 } 214 215 218 public int hashCode() { 219 int hash = LangUtils.HASH_SEED; 220 hash = LangUtils.hashCode(hash, this.hostname); 221 hash = LangUtils.hashCode(hash, this.port); 222 hash = LangUtils.hashCode(hash, this.protocol); 223 return hash; 224 } 225 226 } 227 | Popular Tags |