1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v 1.11.2.1 2003/07/31 02:31:09 mbecke Exp $
3 * $Revision: 1.11.2.1 $
4 * $Date: 2003/07/31 02:31:09 $
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 *
72 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
73 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
74 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
75 * @author Laura Werner
76 *
77 * @since 2.0
78 */
79 public class HostConfiguration implements Cloneable {
80
81 /*** The host to use. */
82 private String host;
83
84 /*** The virtual host to use. */
85 private String virtualHost;
86
87 /*** The port to use. */
88 private int port;
89
90 /*** The protocol */
91 private Protocol protocol;
92
93 /*** True if a host has been set */
94 private boolean hostSet;
95
96 /*** The host name of the proxy server */
97 private String proxyHost;
98
99 /*** The port number of the proxy server */
100 private int proxyPort;
101
102 /*** True if a proxy server has been set */
103 private boolean proxySet;
104
105 /*** The local address to use when creating the socket, or null to use the default */
106 private InetAddress localAddress;
107
108 /***
109 * Constructor for HostConfiguration.
110 */
111 public HostConfiguration() {
112
113 this.host = null;
114 this.virtualHost = null;
115 this.port = -1;
116 this.protocol = null;
117 this.hostSet = false;
118
119 this.proxyHost = null;
120 this.proxyPort = -1;
121 this.proxySet = false;
122 this.localAddress = null;
123 }
124
125 /***
126 * Copy constructor for HostConfiguration
127 *
128 * @param hostConfiguration the hostConfiguration to copy
129 */
130 public HostConfiguration (HostConfiguration hostConfiguration) {
131
132 // wrap all of the assignments in a synchronized block to avoid
133 // having to negotiate the monitor for each method call
134 synchronized (hostConfiguration) {
135 this.host = hostConfiguration.getHost();
136 this.virtualHost = hostConfiguration.getVirtualHost();
137 this.port = hostConfiguration.getPort();
138 this.protocol = hostConfiguration.getProtocol();
139 this.hostSet = hostConfiguration.isHostSet();
140
141 this.proxyHost = hostConfiguration.getProxyHost();
142 this.proxyPort = hostConfiguration.getProxyPort();
143 this.proxySet = hostConfiguration.isProxySet();
144 this.localAddress = hostConfiguration.getLocalAddress();
145 }
146
147 }
148
149 /***
150 * @see java.lang.Object#clone()
151 */
152 public Object clone() {
153 return new HostConfiguration(this);
154 }
155
156
157 /***
158 * @see java.lang.Object#toString()
159 */
160 public synchronized String toString() {
161
162 boolean appendComma = false;
163
164 StringBuffer b = new StringBuffer(50);
165 b.append("HostConfiguration[");
166
167 if (isHostSet()) {
168 appendComma = true;
169 b.append("host=").append(host);
170 b.append(", protocol=").append(protocol);
171 b.append(", port=").append(port);
172 if (virtualHost != null) {
173 b.append(", virtualHost=").append(virtualHost);
174 }
175 }
176 if (isProxySet()) {
177 if (appendComma) {
178 b.append(", ");
179 } else {
180 appendComma = true;
181 }
182 b.append("proxyHost=").append(proxyHost);
183 b.append(", proxyPort=").append(proxyPort);
184 }
185 if (localAddress != null) {
186 if (appendComma) {
187 b.append(", ");
188 } else {
189 appendComma = true;
190 }
191 b.append("localAddress=").append(localAddress);
192 }
193
194 b.append("]");
195 return b.toString();
196 }
197
198 /***
199 * Tests if the host configuration equals the configuraiton set on the
200 * connection. True only if the host, port and protocol are equal. If no
201 * host configuration has been set false will be returned.
202 *
203 * @param connection the connection to test against
204 * @return true if the connection's host information equals that of this
205 * configuration
206 *
207 * @see #proxyEquals(HttpConnection)
208 */
209 public synchronized boolean hostEquals(HttpConnection connection) {
210
211 if (hostSet) {
212 if (!this.host.equalsIgnoreCase(connection.getHost())) {
213 return false;
214 }
215 if (this.virtualHost != null) {
216 if (!this.virtualHost.equalsIgnoreCase(connection.getVirtualHost())) {
217 return false;
218 }
219 } else {
220 if (connection.getVirtualHost() != null) {
221 return false;
222 }
223 }
224 if (this.port != connection.getPort()) {
225 return false;
226 }
227 if (!this.protocol.equals(connection.getProtocol())) {
228 return false;
229 }
230 if (this.localAddress != null) {
231 if (!this.localAddress.equals(connection.getLocalAddress())) {
232 return false;
233 }
234 } else {
235 if (connection.getLocalAddress() != null) {
236 return false;
237 }
238 }
239 return true;
240 } else {
241 return false;
242 }
243
244 }
245
246 /***
247 * Tests if the proxy configuration equals the configuraiton set on the
248 * connection. True only if the proxyHost and proxyPort are equal.
249 *
250 * @param connection the connection to test against
251 * @return true if the connection's proxy information equals that of this
252 * configuration
253 *
254 * @see #hostEquals(HttpConnection)
255 */
256 public synchronized boolean proxyEquals(HttpConnection connection) {
257
258 if (proxyHost == null) {
259 return connection.getProxyHost() == null;
260 } else {
261 return (
262 proxyHost.equalsIgnoreCase(connection.getProxyHost())
263 && proxyPort == connection.getProxyPort()
264 );
265 }
266
267 }
268
269 /***
270 * Return true if the host is set.
271 * @return boolean True if the host is set.
272 */
273 public synchronized boolean isHostSet() {
274 return hostSet;
275 }
276
277 /***
278 * Set the given host, port and protocol
279 *
280 * @param host the host, IP or DNS name
281 * @param port The port
282 * @param protocol The protocol.
283 */
284 public synchronized void setHost(String host, int port, String protocol) {
285 setHost(host, null, port, Protocol.getProtocol(protocol));
286 }
287
288 /***
289 * Set the given host, virtual host, port and protocol.
290 *
291 * @param host the host, IP or DNS name
292 * @param virtualHost the virtual host name
293 * @param port the host port or -1 to use protocol default
294 * @param protocol the protocol
295 */
296 public synchronized void setHost(String host, String virtualHost, int port,
297 Protocol protocol) {
298
299 if (host == null) {
300 throw new IllegalArgumentException("host must not be null");
301 }
302 if (protocol == null) {
303 throw new IllegalArgumentException("protocol must not be null");
304 }
305
306 this.host = host;
307 this.virtualHost = virtualHost;
308 this.port = port == -1 ? protocol.getDefaultPort() : port;
309 this.protocol = protocol;
310
311 this.hostSet = true;
312
313 }
314
315 /***
316 * Set 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 /***
328 * Set the given host and port. Select default protocol.
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 /***
338 * Set the given host. Select default protocol and port.
339 * @param host The host.
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 /***
348 * Sets the protocol, host and port from the given URI.
349 * @param uri the URI.
350 */
351 public synchronized void setHost(URI uri) {
352 try {
353 setHost(uri.getHost(), uri.getPort(), uri.getScheme());
354 } catch (URIException e) {
355 throw new IllegalArgumentException(e.toString());
356 }
357 }
358
359 /***
360 * Return the host url.
361 *
362 * @return String The host url.
363 */
364 public synchronized String getHostURL() {
365
366 if (!hostSet) {
367 throw new IllegalStateException("a default host must be set to "
368 + "create a host URL"
369 );
370 }
371
372 String url = protocol.getScheme() + "://" + host;
373
374 if (port != -1 && port != protocol.getDefaultPort()) {
375 url += ":" + port;
376 }
377
378 return url;
379
380 }
381
382 /***
383 * Returns the host.
384 * @return String
385 */
386 public synchronized String getHost() {
387 return host;
388 }
389
390 /***
391 * Returns the virtual host.
392 * @return String
393 */
394 public synchronized String getVirtualHost() {
395 return virtualHost;
396 }
397
398 /***
399 * Returns the port.
400 * @return int
401 */
402 public synchronized int getPort() {
403 return port;
404 }
405
406 /***
407 * Returns the protocol.
408 * @return String The protocol.
409 */
410 public synchronized Protocol getProtocol() {
411 return protocol;
412 }
413
414 /***
415 * @return boolean True if a proxy server has been set.
416 */
417 public synchronized boolean isProxySet() {
418 return proxySet;
419 }
420
421 /***
422 * Set the proxy settings.
423 * @param proxyHost The proxy host
424 * @param proxyPort The proxy port
425 */
426 public synchronized void setProxy(String proxyHost, int proxyPort) {
427
428 this.proxyHost = proxyHost;
429 this.proxyPort = proxyPort;
430
431 this.proxySet = true;
432 }
433
434 /***
435 * Returns the proxyHost.
436 * @return String
437 */
438 public synchronized String getProxyHost() {
439 return proxyHost;
440 }
441
442 /***
443 * Returns the proxyPort.
444 * @return int
445 */
446 public synchronized int getProxyPort() {
447 return proxyPort;
448 }
449
450 /***
451 * Set the local address to be used when creating connections.
452 * If this is unset, the default address will be used.
453 * This is useful for specifying the interface to use on multi-homed or clustered systems.
454 *
455 * @param localAddress the local address to use
456 */
457 public synchronized void setLocalAddress(InetAddress localAddress) {
458 this.localAddress = localAddress;
459 }
460
461 /***
462 * Return the local address to be used when creating connections.
463 * If this is unset, the default address should be used.
464 *
465 * @return InetAddress the local address to be used when creating Sockets
466 */
467 public synchronized InetAddress getLocalAddress() {
468 return this.localAddress;
469 }
470
471 /***
472 * @see java.lang.Object#equals(java.lang.Object)
473 */
474 public synchronized boolean equals(Object o) {
475
476 if (o instanceof HostConfiguration) {
477
478 // shortcut if we're comparing with ourselves
479 if (o == this) {
480 return true;
481 }
482
483 HostConfiguration config = (HostConfiguration) o;
484
485 if (hostSet) {
486 if (!host.equalsIgnoreCase(config.getHost())) {
487 return false;
488 }
489 if (virtualHost != null) {
490 if (!virtualHost.equalsIgnoreCase(config.getVirtualHost())) {
491 return false;
492 }
493 } else {
494 if (config.getVirtualHost() != null) {
495 return false;
496 }
497 }
498 if (port != config.getPort()) {
499 return false;
500 }
501 if (!protocol.equals(config.getProtocol())) {
502 return false;
503 }
504 } else if (config.isHostSet()) {
505 return false;
506 }
507 if (proxyHost != null) {
508 if (!proxyHost.equalsIgnoreCase (config.getProxyHost())
509 || proxyPort != config.getProxyPort()) {
510 // either proxyHost or proxyPort don't match
511 return false;
512 }
513 } else if (config.getProxyHost() != null) {
514 return false;
515 }
516 if (localAddress != null) {
517 if (!localAddress.equals(config.getLocalAddress())) {
518 return false;
519 }
520 } else {
521 if (config.getLocalAddress() != null) {
522 return false;
523 }
524 }
525
526 // everything matches
527 return true;
528
529 } else {
530 return false;
531 }
532
533 }
534
535 /***
536 * @see java.lang.Object#hashCode()
537 */
538 public int hashCode() {
539
540 if (host != null) {
541 return host.hashCode();
542 } else if (proxyHost != null) {
543 return proxyHost.hashCode();
544 } else {
545 return super.hashCode();
546 }
547 }
548 }
This page was automatically generated by Maven