1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v 1.11 2003/05/12 02:42:42 mbecke Exp $
3 * $Revision: 1.11 $
4 * $Date: 2003/05/12 02:42:42 $
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 * Tests if the host configuration equals the configuraiton set on the
158 * connection. True only if the host, port and protocol are equal. If no
159 * host configuration has been set false will be returned.
160 *
161 * @param connection the connection to test against
162 * @return true if the connection's host information equals that of this
163 * configuration
164 *
165 * @see #proxyEquals(HttpConnection)
166 */
167 public synchronized boolean hostEquals(HttpConnection connection) {
168
169 if (hostSet) {
170 if (!this.host.equalsIgnoreCase(connection.getHost())) {
171 return false;
172 }
173 if (this.virtualHost != null) {
174 if (!this.virtualHost.equalsIgnoreCase(connection.getVirtualHost())) {
175 return false;
176 }
177 } else {
178 if (connection.getVirtualHost() != null) {
179 return false;
180 }
181 }
182 if (this.port != connection.getPort()) {
183 return false;
184 }
185 if (!this.protocol.equals(connection.getProtocol())) {
186 return false;
187 }
188 if (this.localAddress != null) {
189 if (!this.localAddress.equals(connection.getLocalAddress())) {
190 return false;
191 }
192 } else {
193 if (connection.getLocalAddress() != null) {
194 return false;
195 }
196 }
197 return true;
198 } else {
199 return false;
200 }
201
202 }
203
204 /***
205 * Tests if the proxy configuration equals the configuraiton set on the
206 * connection. True only if the proxyHost and proxyPort are equal.
207 *
208 * @param connection the connection to test against
209 * @return true if the connection's proxy information equals that of this
210 * configuration
211 *
212 * @see #hostEquals(HttpConnection)
213 */
214 public synchronized boolean proxyEquals(HttpConnection connection) {
215
216 if (proxyHost == null) {
217 return connection.getProxyHost() == null;
218 } else {
219 return (
220 proxyHost.equalsIgnoreCase(connection.getProxyHost())
221 && proxyPort == connection.getProxyPort()
222 );
223 }
224
225 }
226
227 /***
228 * Return true if the host is set.
229 * @return boolean True if the host is set.
230 */
231 public synchronized boolean isHostSet() {
232 return hostSet;
233 }
234
235 /***
236 * Set the given host, port and protocol
237 *
238 * @param host the host, IP or DNS name
239 * @param port The port
240 * @param protocol The protocol.
241 */
242 public synchronized void setHost(String host, int port, String protocol) {
243 setHost(host, null, port, Protocol.getProtocol(protocol));
244 }
245
246 /***
247 * Set the given host, virtual host, port and protocol.
248 *
249 * @param host the host, IP or DNS name
250 * @param virtualHost the virtual host name
251 * @param port the host port or -1 to use protocol default
252 * @param protocol the protocol
253 */
254 public synchronized void setHost(String host, String virtualHost, int port,
255 Protocol protocol) {
256
257 if (host == null) {
258 throw new IllegalArgumentException("host must not be null");
259 }
260 if (protocol == null) {
261 throw new IllegalArgumentException("protocol must not be null");
262 }
263
264 this.host = host;
265 this.virtualHost = virtualHost;
266 this.port = port == -1 ? protocol.getDefaultPort() : port;
267 this.protocol = protocol;
268
269 this.hostSet = true;
270
271 }
272
273 /***
274 * Set the given host, port and protocol.
275 *
276 * @param host the host, IP or DNS name
277 * @param port The port
278 * @param protocol the protocol
279 */
280 public synchronized void setHost(String host, int port, Protocol protocol) {
281 setHost(host, null, port, protocol);
282 }
283
284
285 /***
286 * Set the given host and port. Select default protocol.
287 * @param host the host, IP or DNS name
288 * @param port The port
289 */
290 public synchronized void setHost(String host, int port) {
291 setHost(host, null, port, Protocol.getProtocol("http"));
292 }
293
294
295 /***
296 * Set the given host. Select default protocol and port.
297 * @param host The host.
298 */
299 public synchronized void setHost(String host) {
300 Protocol defaultProtocol = Protocol.getProtocol("http");
301 setHost(host, null, defaultProtocol.getDefaultPort(), defaultProtocol);
302 }
303
304
305 /***
306 * Sets the protocol, host and port from the given URI.
307 * @param uri the URI.
308 */
309 public synchronized void setHost(URI uri) {
310 try {
311 setHost(uri.getHost(), uri.getPort(), uri.getScheme());
312 } catch (URIException e) {
313 throw new IllegalArgumentException(e.toString());
314 }
315 }
316
317 /***
318 * Return the host url.
319 *
320 * @return String The host url.
321 */
322 public synchronized String getHostURL() {
323
324 if (!hostSet) {
325 throw new IllegalStateException("a default host must be set to "
326 + "create a host URL"
327 );
328 }
329
330 String url = protocol.getScheme() + "://" + host;
331
332 if (port != -1 && port != protocol.getDefaultPort()) {
333 url += ":" + port;
334 }
335
336 return url;
337
338 }
339
340 /***
341 * Returns the host.
342 * @return String
343 */
344 public synchronized String getHost() {
345 return host;
346 }
347
348 /***
349 * Returns the virtual host.
350 * @return String
351 */
352 public synchronized String getVirtualHost() {
353 return virtualHost;
354 }
355
356 /***
357 * Returns the port.
358 * @return int
359 */
360 public synchronized int getPort() {
361 return port;
362 }
363
364 /***
365 * Returns the protocol.
366 * @return String The protocol.
367 */
368 public synchronized Protocol getProtocol() {
369 return protocol;
370 }
371
372 /***
373 * @return boolean True if a proxy server has been set.
374 */
375 public synchronized boolean isProxySet() {
376 return proxySet;
377 }
378
379 /***
380 * Set the proxy settings.
381 * @param proxyHost The proxy host
382 * @param proxyPort The proxy port
383 */
384 public synchronized void setProxy(String proxyHost, int proxyPort) {
385
386 this.proxyHost = proxyHost;
387 this.proxyPort = proxyPort;
388
389 this.proxySet = true;
390 }
391
392 /***
393 * Returns the proxyHost.
394 * @return String
395 */
396 public synchronized String getProxyHost() {
397 return proxyHost;
398 }
399
400 /***
401 * Returns the proxyPort.
402 * @return int
403 */
404 public synchronized int getProxyPort() {
405 return proxyPort;
406 }
407
408 /***
409 * Set the local address to be used when creating connections.
410 * If this is unset, the default address will be used.
411 * This is useful for specifying the interface to use on multi-homed or clustered systems.
412 *
413 * @param localAddress the local address to use
414 */
415 public synchronized void setLocalAddress(InetAddress localAddress) {
416 this.localAddress = localAddress;
417 }
418
419 /***
420 * Return the local address to be used when creating connections.
421 * If this is unset, the default address should be used.
422 *
423 * @return InetAddress the local address to be used when creating Sockets
424 */
425 public synchronized InetAddress getLocalAddress() {
426 return this.localAddress;
427 }
428
429 /***
430 * @see java.lang.Object#equals(java.lang.Object)
431 */
432 public synchronized boolean equals(Object o) {
433
434 if (o instanceof HostConfiguration) {
435
436 // shortcut if we're comparing with ourselves
437 if (o == this) {
438 return true;
439 }
440
441 HostConfiguration config = (HostConfiguration) o;
442
443 if (hostSet) {
444 if (!host.equalsIgnoreCase(config.getHost())) {
445 return false;
446 }
447 if (virtualHost != null) {
448 if (!virtualHost.equalsIgnoreCase(config.getVirtualHost())) {
449 return false;
450 }
451 } else {
452 if (config.getVirtualHost() != null) {
453 return false;
454 }
455 }
456 if (port != config.getPort()) {
457 return false;
458 }
459 if (!protocol.equals(config.getProtocol())) {
460 return false;
461 }
462 } else if (config.isHostSet()) {
463 return false;
464 }
465 if (proxyHost != null) {
466 if (!proxyHost.equalsIgnoreCase (config.getProxyHost())
467 || proxyPort != config.getProxyPort()) {
468 // either proxyHost or proxyPort don't match
469 return false;
470 }
471 } else if (config.getProxyHost() != null) {
472 return false;
473 }
474 if (localAddress != null) {
475 if (!localAddress.equals(config.getLocalAddress())) {
476 return false;
477 }
478 } else {
479 if (config.getLocalAddress() != null) {
480 return false;
481 }
482 }
483
484 // everything matches
485 return true;
486
487 } else {
488 return false;
489 }
490
491 }
492
493 /***
494 * @see java.lang.Object#hashCode()
495 */
496 public int hashCode() {
497
498 if (host != null) {
499 return host.hashCode();
500 } else if (proxyHost != null) {
501 return proxyHost.hashCode();
502 } else {
503 return super.hashCode();
504 }
505 }
506 }
This page was automatically generated by Maven