1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient;
31
32 import org.apache.commons.httpclient.params.HostParams;
33 import org.apache.commons.httpclient.protocol.Protocol;
34 import org.apache.commons.httpclient.util.LangUtils;
35
36 import java.net.InetAddress;
37
38 /***
39 * Holds all of the variables needed to describe an HTTP connection to a host. This includes
40 * remote host, port and protocol, proxy host and port, local address, and virtual host.
41 *
42 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
43 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
44 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
45 * @author Laura Werner
46 *
47 * @since 2.0
48 */
49 public class HostConfiguration implements Cloneable {
50
51 /***
52 * A value to represent any host configuration, instead of using something like
53 * <code>null</code>. This value should be treated as immutable and only used in
54 * lookups and other such places to represent "any" host config.
55 */
56 public static final HostConfiguration ANY_HOST_CONFIGURATION = new HostConfiguration();
57
58 /*** The host to use. */
59 private HttpHost host = null;
60
61 /*** The host name of the proxy server */
62 private ProxyHost proxyHost = null;
63
64 /*** The local address to use when creating the socket, or null to use the default */
65 private InetAddress localAddress = null;
66
67 /*** Parameters specific to this host */
68 private HostParams params = new HostParams();
69
70 /***
71 * Constructor for HostConfiguration.
72 */
73 public HostConfiguration() {
74 super();
75 }
76
77 /***
78 * Copy constructor for HostConfiguration
79 *
80 * @param hostConfiguration the hostConfiguration to copy
81 */
82 public HostConfiguration (final HostConfiguration hostConfiguration) {
83
84
85 synchronized (hostConfiguration) {
86 try {
87 if (hostConfiguration.host != null) {
88 this.host = (HttpHost) hostConfiguration.host.clone();
89 } else {
90 this.host = null;
91 }
92 if (hostConfiguration.proxyHost != null) {
93 this.proxyHost = (ProxyHost) hostConfiguration.proxyHost.clone();
94 } else {
95 this.proxyHost = null;
96 }
97 this.localAddress = hostConfiguration.getLocalAddress();
98 this.params = (HostParams)hostConfiguration.getParams().clone();
99 } catch (CloneNotSupportedException e) {
100 throw new IllegalArgumentException("Host configuration could not be cloned");
101 }
102 }
103 }
104
105 /***
106 * @see java.lang.Object#clone()
107 */
108 public Object clone() {
109 return new HostConfiguration(this);
110 }
111
112 /***
113 * @see java.lang.Object#toString()
114 */
115 public synchronized String toString() {
116
117 boolean appendComma = false;
118 StringBuffer b = new StringBuffer(50);
119 b.append("HostConfiguration[");
120
121 if (this.host != null) {
122 appendComma = true;
123 b.append("host=").append(this.host);
124 }
125 if (this.proxyHost != null) {
126 if (appendComma) {
127 b.append(", ");
128 } else {
129 appendComma = true;
130 }
131 b.append("proxyHost=").append(this.proxyHost);
132 }
133 if (this.localAddress != null) {
134 if (appendComma) {
135 b.append(", ");
136 } else {
137 appendComma = true;
138 }
139 b.append("localAddress=").append(this.localAddress);
140 if (appendComma) {
141 b.append(", ");
142 } else {
143 appendComma = true;
144 }
145 b.append("params=").append(this.params);
146 }
147 b.append("]");
148 return b.toString();
149 }
150
151 /***
152 * Tests if the host configuration equals the configuration set on the
153 * connection. True only if the host, port, protocol, local address and virtual address
154 * are equal. If no host configuration has been set false will be returned.
155 *
156 * @param connection the connection to test against
157 * @return <code>true</code> if the connection's host information equals that of this
158 * configuration
159 *
160 * @see #proxyEquals(HttpConnection)
161 */
162 public synchronized boolean hostEquals(final HttpConnection connection) {
163 if (connection == null) {
164 throw new IllegalArgumentException("Connection may not be null");
165 }
166 if (this.host != null) {
167 if (!this.host.getHostName().equalsIgnoreCase(connection.getHost())) {
168 return false;
169 }
170 if (this.host.getPort() != connection.getPort()) {
171 return false;
172 }
173 if (!this.host.getProtocol().equals(connection.getProtocol())) {
174 return false;
175 }
176 if (this.localAddress != null) {
177 if (!this.localAddress.equals(connection.getLocalAddress())) {
178 return false;
179 }
180 } else {
181 if (connection.getLocalAddress() != null) {
182 return false;
183 }
184 }
185 return true;
186 } else {
187 return false;
188 }
189 }
190
191 /***
192 * Tests if the proxy configuration equals the configuration set on the
193 * connection. True only if the proxyHost and proxyPort are equal.
194 *
195 * @param connection the connection to test against
196 * @return <code>true</code> if the connection's proxy information equals that of this
197 * configuration
198 *
199 * @see #hostEquals(HttpConnection)
200 */
201 public synchronized boolean proxyEquals(final HttpConnection connection) {
202 if (connection == null) {
203 throw new IllegalArgumentException("Connection may not be null");
204 }
205 if (this.proxyHost != null) {
206 return
207 this.proxyHost.getHostName().equalsIgnoreCase(connection.getProxyHost())
208 && this.proxyHost.getPort() == connection.getProxyPort();
209 } else {
210 return connection.getProxyHost() == null;
211 }
212 }
213
214 /***
215 * Returns true if the host is set.
216 * @return <code>true</code> if the host is set.
217 *
218 * @deprecated no longer used
219 */
220 public synchronized boolean isHostSet() {
221 return this.host != null;
222 }
223
224 /***
225 * Sets the given host
226 *
227 * @param host the host
228 */
229 public synchronized void setHost(final HttpHost host) {
230 this.host = host;
231 }
232
233 /***
234 * Sets the given host, port and protocol
235 *
236 * @param host the host(IP or DNS name)
237 * @param port The port
238 * @param protocol The protocol.
239 */
240 public synchronized void setHost(final String host, int port, final String protocol) {
241 this.host = new HttpHost(host, port, Protocol.getProtocol(protocol));
242 }
243
244 /***
245 * Sets the given host, virtual host, port and protocol.
246 *
247 * @param host the host(IP or DNS name)
248 * @param virtualHost the virtual host name or <code>null</code>
249 * @param port the host port or -1 to use protocol default
250 * @param protocol the protocol
251 *
252 * @deprecated #setHost(String, int, Protocol)
253 */
254 public synchronized void setHost(final String host, final String virtualHost, int port,
255 final Protocol protocol) {
256 setHost(host, port, protocol);
257 this.params.setVirtualHost(virtualHost);
258 }
259
260 /***
261 * Sets the given host, port and protocol.
262 *
263 * @param host the host(IP or DNS name)
264 * @param port The port
265 * @param protocol the protocol
266 */
267 public synchronized void setHost(final String host, int port, final Protocol protocol) {
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 this.host = new HttpHost(host, port, protocol);
275 }
276
277 /***
278 * Sets the given host and port. Uses the default protocol "http".
279 *
280 * @param host the host(IP or DNS name)
281 * @param port The port
282 */
283 public synchronized void setHost(final String host, int port) {
284 setHost(host, port, Protocol.getProtocol("http"));
285 }
286
287 /***
288 * Set the given host. Uses the default protocol("http") and its port.
289 *
290 * @param host The host(IP or DNS name).
291 */
292 public synchronized void setHost(final String host) {
293 Protocol defaultProtocol = Protocol.getProtocol("http");
294 setHost(host, defaultProtocol.getDefaultPort(), defaultProtocol);
295 }
296
297 /***
298 * Sets the protocol, host and port from the given URI.
299 * @param uri the URI.
300 */
301 public synchronized void setHost(final URI uri) {
302 try {
303 setHost(uri.getHost(), uri.getPort(), uri.getScheme());
304 } catch (URIException e) {
305 throw new IllegalArgumentException(e.toString());
306 }
307 }
308
309 /***
310 * Return the host url.
311 *
312 * @return The host url.
313 */
314 public synchronized String getHostURL() {
315 if (this.host == null) {
316 throw new IllegalStateException("Host must be set to create a host URL");
317 } else {
318 return this.host.toURI();
319 }
320 }
321
322 /***
323 * Returns the host.
324 *
325 * @return the host(IP or DNS name), or <code>null</code> if not set
326 *
327 * @see #isHostSet()
328 */
329 public synchronized String getHost() {
330 if (this.host != null) {
331 return this.host.getHostName();
332 } else {
333 return null;
334 }
335 }
336
337 /***
338 * Returns the virtual host.
339 *
340 * @return the virtual host name, or <code>null</code> if not set
341 *
342 * @deprecated use HostParams
343 */
344 public synchronized String getVirtualHost() {
345 return this.params.getVirtualHost();
346 }
347
348 /***
349 * Returns the port.
350 *
351 * @return the host port, or <code>-1</code> if not set
352 *
353 * @see #isHostSet()
354 */
355 public synchronized int getPort() {
356 if (this.host != null) {
357 return this.host.getPort();
358 } else {
359 return -1;
360 }
361 }
362
363 /***
364 * Returns the protocol.
365 * @return The protocol.
366 */
367 public synchronized Protocol getProtocol() {
368 if (this.host != null) {
369 return this.host.getProtocol();
370 } else {
371 return null;
372 }
373 }
374
375 /***
376 * Tests if the proxy host/port have been set.
377 *
378 * @return <code>true</code> if a proxy server has been set.
379 *
380 * @see #setProxy(String, int)
381 *
382 * @deprecated no longer used
383 */
384 public synchronized boolean isProxySet() {
385 return this.proxyHost != null;
386 }
387
388 /***
389 * Sets the given proxy host
390 *
391 * @param proxyHost the proxy host
392 */
393 public synchronized void setProxyHost(final ProxyHost proxyHost) {
394 this.proxyHost = proxyHost;
395 }
396
397 /***
398 * Set the proxy settings.
399 * @param proxyHost The proxy host
400 * @param proxyPort The proxy port
401 */
402 public synchronized void setProxy(final String proxyHost, int proxyPort) {
403 this.proxyHost = new ProxyHost(proxyHost, proxyPort);
404 }
405
406 /***
407 * Returns the proxyHost.
408 *
409 * @return the proxy host, or <code>null</code> if not set
410 *
411 * @see #isProxySet()
412 */
413 public synchronized String getProxyHost() {
414 if (this.proxyHost != null) {
415 return this.proxyHost.getHostName();
416 } else {
417 return null;
418 }
419 }
420
421 /***
422 * Returns the proxyPort.
423 *
424 * @return the proxy port, or <code>-1</code> if not set
425 *
426 * @see #isProxySet()
427 */
428 public synchronized int getProxyPort() {
429 if (this.proxyHost != null) {
430 return this.proxyHost.getPort();
431 } else {
432 return -1;
433 }
434 }
435
436 /***
437 * Set the local address to be used when creating connections.
438 * If this is unset, the default address will be used.
439 * This is useful for specifying the interface to use on multi-homed or clustered systems.
440 *
441 * @param localAddress the local address to use
442 */
443
444 public synchronized void setLocalAddress(InetAddress localAddress) {
445 this.localAddress = localAddress;
446 }
447
448 /***
449 * Return the local address to be used when creating connections.
450 * If this is unset, the default address should be used.
451 *
452 * @return the local address to be used when creating Sockets, or <code>null</code>
453 */
454
455 public synchronized InetAddress getLocalAddress() {
456 return this.localAddress;
457 }
458
459 /***
460 * Returns {@link HostParams HTTP protocol parameters} associated with this host.
461 *
462 * @return HTTP parameters.
463 *
464 * @since 3.0
465 */
466 public HostParams getParams() {
467 return this.params;
468 }
469
470 /***
471 * Assigns {@link HostParams HTTP protocol parameters} specific to this host.
472 *
473 * @since 3.0
474 *
475 * @see HostParams
476 */
477 public void setParams(final HostParams params) {
478 if (params == null) {
479 throw new IllegalArgumentException("Parameters may not be null");
480 }
481 this.params = params;
482 }
483
484 /***
485 * @see java.lang.Object#equals(java.lang.Object)
486 */
487 public synchronized boolean equals(final Object o) {
488 if (o instanceof HostConfiguration) {
489
490 if (o == this) {
491 return true;
492 }
493 HostConfiguration that = (HostConfiguration) o;
494 return LangUtils.equals(this.host, that.host)
495 && LangUtils.equals(this.proxyHost, that.proxyHost)
496 && LangUtils.equals(this.localAddress, that.localAddress);
497 } else {
498 return false;
499 }
500
501 }
502
503 /***
504 * @see java.lang.Object#hashCode()
505 */
506 public synchronized int hashCode() {
507 int hash = LangUtils.HASH_SEED;
508 hash = LangUtils.hashCode(hash, this.host);
509 hash = LangUtils.hashCode(hash, this.proxyHost);
510 hash = LangUtils.hashCode(hash, this.localAddress);
511 return hash;
512 }
513
514 }