001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v 1.23 2005/01/14 21:16:40 olegk Exp $
003: * $Revision: 510585 $
004: * $Date: 2007-02-22 17:52:16 +0100 (Thu, 22 Feb 2007) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import org.apache.commons.httpclient.params.HostParams;
034: import org.apache.commons.httpclient.protocol.Protocol;
035: import org.apache.commons.httpclient.util.LangUtils;
036:
037: import java.net.InetAddress;
038:
039: /**
040: * Holds all of the variables needed to describe an HTTP connection to a host. This includes
041: * remote host, port and protocol, proxy host and port, local address, and virtual host.
042: *
043: * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
044: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
045: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
046: * @author Laura Werner
047: *
048: * @since 2.0
049: */
050: public class HostConfiguration implements Cloneable {
051:
052: /**
053: * A value to represent any host configuration, instead of using something like
054: * <code>null</code>. This value should be treated as immutable and only used in
055: * lookups and other such places to represent "any" host config.
056: */
057: public static final HostConfiguration ANY_HOST_CONFIGURATION = new HostConfiguration();
058:
059: /** The host to use. */
060: private HttpHost host = null;
061:
062: /** The host name of the proxy server */
063: private ProxyHost proxyHost = null;
064:
065: /** The local address to use when creating the socket, or null to use the default */
066: private InetAddress localAddress = null;
067:
068: /** Parameters specific to this host */
069: private HostParams params = new HostParams();
070:
071: /**
072: * Constructor for HostConfiguration.
073: */
074: public HostConfiguration() {
075: super ();
076: }
077:
078: /**
079: * Copy constructor for HostConfiguration
080: *
081: * @param hostConfiguration the hostConfiguration to copy
082: */
083: public HostConfiguration(final HostConfiguration hostConfiguration) {
084: init(hostConfiguration);
085: }
086:
087: private void init(final HostConfiguration hostConfiguration) {
088: // wrap all of the assignments in a synchronized block to avoid
089: // having to negotiate the monitor for each method call
090: synchronized (hostConfiguration) {
091: try {
092: if (hostConfiguration.host != null) {
093: this .host = (HttpHost) hostConfiguration.host
094: .clone();
095: } else {
096: this .host = null;
097: }
098: if (hostConfiguration.proxyHost != null) {
099: this .proxyHost = (ProxyHost) hostConfiguration.proxyHost
100: .clone();
101: } else {
102: this .proxyHost = null;
103: }
104: this .localAddress = hostConfiguration.getLocalAddress();
105: this .params = (HostParams) hostConfiguration
106: .getParams().clone();
107: } catch (CloneNotSupportedException e) {
108: throw new IllegalArgumentException(
109: "Host configuration could not be cloned");
110: }
111: }
112: }
113:
114: /**
115: * @see java.lang.Object#clone()
116: */
117: public Object clone() {
118: HostConfiguration copy;
119: try {
120: copy = (HostConfiguration) super .clone();
121: } catch (CloneNotSupportedException e) {
122: throw new IllegalArgumentException(
123: "Host configuration could not be cloned");
124: }
125: copy.init(this );
126: return copy;
127: }
128:
129: /**
130: * @see java.lang.Object#toString()
131: */
132: public synchronized String toString() {
133:
134: boolean appendComma = false;
135: StringBuffer b = new StringBuffer(50);
136: b.append("HostConfiguration[");
137:
138: if (this .host != null) {
139: appendComma = true;
140: b.append("host=").append(this .host);
141: }
142: if (this .proxyHost != null) {
143: if (appendComma) {
144: b.append(", ");
145: } else {
146: appendComma = true;
147: }
148: b.append("proxyHost=").append(this .proxyHost);
149: }
150: if (this .localAddress != null) {
151: if (appendComma) {
152: b.append(", ");
153: } else {
154: appendComma = true;
155: }
156: b.append("localAddress=").append(this .localAddress);
157: if (appendComma) {
158: b.append(", ");
159: } else {
160: appendComma = true;
161: }
162: b.append("params=").append(this .params);
163: }
164: b.append("]");
165: return b.toString();
166: }
167:
168: /**
169: * Tests if the host configuration equals the configuration set on the
170: * connection. True only if the host, port, protocol, local address and virtual address
171: * are equal. If no host configuration has been set false will be returned.
172: *
173: * @param connection the connection to test against
174: * @return <code>true</code> if the connection's host information equals that of this
175: * configuration
176: *
177: * @see #proxyEquals(HttpConnection)
178: */
179: public synchronized boolean hostEquals(
180: final HttpConnection connection) {
181: if (connection == null) {
182: throw new IllegalArgumentException(
183: "Connection may not be null");
184: }
185: if (this .host != null) {
186: if (!this .host.getHostName().equalsIgnoreCase(
187: connection.getHost())) {
188: return false;
189: }
190: if (this .host.getPort() != connection.getPort()) {
191: return false;
192: }
193: if (!this .host.getProtocol().equals(
194: connection.getProtocol())) {
195: return false;
196: }
197: if (this .localAddress != null) {
198: if (!this .localAddress.equals(connection
199: .getLocalAddress())) {
200: return false;
201: }
202: } else {
203: if (connection.getLocalAddress() != null) {
204: return false;
205: }
206: }
207: return true;
208: } else {
209: return false;
210: }
211: }
212:
213: /**
214: * Tests if the proxy configuration equals the configuration set on the
215: * connection. True only if the proxyHost and proxyPort are equal.
216: *
217: * @param connection the connection to test against
218: * @return <code>true</code> if the connection's proxy information equals that of this
219: * configuration
220: *
221: * @see #hostEquals(HttpConnection)
222: */
223: public synchronized boolean proxyEquals(
224: final HttpConnection connection) {
225: if (connection == null) {
226: throw new IllegalArgumentException(
227: "Connection may not be null");
228: }
229: if (this .proxyHost != null) {
230: return this .proxyHost.getHostName().equalsIgnoreCase(
231: connection.getProxyHost())
232: && this .proxyHost.getPort() == connection
233: .getProxyPort();
234: } else {
235: return connection.getProxyHost() == null;
236: }
237: }
238:
239: /**
240: * Returns true if the host is set.
241: * @return <code>true</code> if the host is set.
242: *
243: * @deprecated no longer used
244: */
245: public synchronized boolean isHostSet() {
246: return this .host != null;
247: }
248:
249: /**
250: * Sets the given host
251: *
252: * @param host the host
253: */
254: public synchronized void setHost(final HttpHost host) {
255: this .host = host;
256: }
257:
258: /**
259: * Sets the given host, port and protocol
260: *
261: * @param host the host(IP or DNS name)
262: * @param port The port
263: * @param protocol The protocol.
264: */
265: public synchronized void setHost(final String host, int port,
266: final String protocol) {
267: this .host = new HttpHost(host, port, Protocol
268: .getProtocol(protocol));
269: }
270:
271: /**
272: * Sets the given host, virtual host, port and protocol.
273: *
274: * @param host the host(IP or DNS name)
275: * @param virtualHost the virtual host name or <code>null</code>
276: * @param port the host port or -1 to use protocol default
277: * @param protocol the protocol
278: *
279: * @deprecated #setHost(String, int, Protocol)
280: */
281: public synchronized void setHost(final String host,
282: final String virtualHost, int port, final Protocol protocol) {
283: setHost(host, port, protocol);
284: this .params.setVirtualHost(virtualHost);
285: }
286:
287: /**
288: * Sets the given host, port and protocol.
289: *
290: * @param host the host(IP or DNS name)
291: * @param port The port
292: * @param protocol the protocol
293: */
294: public synchronized void setHost(final String host, int port,
295: final Protocol protocol) {
296: if (host == null) {
297: throw new IllegalArgumentException("host must not be null");
298: }
299: if (protocol == null) {
300: throw new IllegalArgumentException(
301: "protocol must not be null");
302: }
303: this .host = new HttpHost(host, port, protocol);
304: }
305:
306: /**
307: * Sets the given host and port. Uses the default protocol "http".
308: *
309: * @param host the host(IP or DNS name)
310: * @param port The port
311: */
312: public synchronized void setHost(final String host, int port) {
313: setHost(host, port, Protocol.getProtocol("http"));
314: }
315:
316: /**
317: * Set the given host. Uses the default protocol("http") and its port.
318: *
319: * @param host The host(IP or DNS name).
320: */
321: public synchronized void setHost(final String host) {
322: Protocol defaultProtocol = Protocol.getProtocol("http");
323: setHost(host, defaultProtocol.getDefaultPort(), defaultProtocol);
324: }
325:
326: /**
327: * Sets the protocol, host and port from the given URI.
328: * @param uri the URI.
329: */
330: public synchronized void setHost(final URI uri) {
331: try {
332: setHost(uri.getHost(), uri.getPort(), uri.getScheme());
333: } catch (URIException e) {
334: throw new IllegalArgumentException(e.toString());
335: }
336: }
337:
338: /**
339: * Return the host url.
340: *
341: * @return The host url.
342: */
343: public synchronized String getHostURL() {
344: if (this .host == null) {
345: throw new IllegalStateException(
346: "Host must be set to create a host URL");
347: } else {
348: return this .host.toURI();
349: }
350: }
351:
352: /**
353: * Returns the host.
354: *
355: * @return the host(IP or DNS name), or <code>null</code> if not set
356: *
357: * @see #isHostSet()
358: */
359: public synchronized String getHost() {
360: if (this .host != null) {
361: return this .host.getHostName();
362: } else {
363: return null;
364: }
365: }
366:
367: /**
368: * Returns the virtual host.
369: *
370: * @return the virtual host name, or <code>null</code> if not set
371: *
372: * @deprecated use HostParams
373: */
374: public synchronized String getVirtualHost() {
375: return this .params.getVirtualHost();
376: }
377:
378: /**
379: * Returns the port.
380: *
381: * @return the host port, or <code>-1</code> if not set
382: *
383: * @see #isHostSet()
384: */
385: public synchronized int getPort() {
386: if (this .host != null) {
387: return this .host.getPort();
388: } else {
389: return -1;
390: }
391: }
392:
393: /**
394: * Returns the protocol.
395: * @return The protocol.
396: */
397: public synchronized Protocol getProtocol() {
398: if (this .host != null) {
399: return this .host.getProtocol();
400: } else {
401: return null;
402: }
403: }
404:
405: /**
406: * Tests if the proxy host/port have been set.
407: *
408: * @return <code>true</code> if a proxy server has been set.
409: *
410: * @see #setProxy(String, int)
411: *
412: * @deprecated no longer used
413: */
414: public synchronized boolean isProxySet() {
415: return this .proxyHost != null;
416: }
417:
418: /**
419: * Sets the given proxy host
420: *
421: * @param proxyHost the proxy host
422: */
423: public synchronized void setProxyHost(final ProxyHost proxyHost) {
424: this .proxyHost = proxyHost;
425: }
426:
427: /**
428: * Set the proxy settings.
429: * @param proxyHost The proxy host
430: * @param proxyPort The proxy port
431: */
432: public synchronized void setProxy(final String proxyHost,
433: int proxyPort) {
434: this .proxyHost = new ProxyHost(proxyHost, proxyPort);
435: }
436:
437: /**
438: * Returns the proxyHost.
439: *
440: * @return the proxy host, or <code>null</code> if not set
441: *
442: * @see #isProxySet()
443: */
444: public synchronized String getProxyHost() {
445: if (this .proxyHost != null) {
446: return this .proxyHost.getHostName();
447: } else {
448: return null;
449: }
450: }
451:
452: /**
453: * Returns the proxyPort.
454: *
455: * @return the proxy port, or <code>-1</code> if not set
456: *
457: * @see #isProxySet()
458: */
459: public synchronized int getProxyPort() {
460: if (this .proxyHost != null) {
461: return this .proxyHost.getPort();
462: } else {
463: return -1;
464: }
465: }
466:
467: /**
468: * Set the local address to be used when creating connections.
469: * If this is unset, the default address will be used.
470: * This is useful for specifying the interface to use on multi-homed or clustered systems.
471: *
472: * @param localAddress the local address to use
473: */
474:
475: public synchronized void setLocalAddress(InetAddress localAddress) {
476: this .localAddress = localAddress;
477: }
478:
479: /**
480: * Return the local address to be used when creating connections.
481: * If this is unset, the default address should be used.
482: *
483: * @return the local address to be used when creating Sockets, or <code>null</code>
484: */
485:
486: public synchronized InetAddress getLocalAddress() {
487: return this .localAddress;
488: }
489:
490: /**
491: * Returns {@link HostParams HTTP protocol parameters} associated with this host.
492: *
493: * @return HTTP parameters.
494: *
495: * @since 3.0
496: */
497: public HostParams getParams() {
498: return this .params;
499: }
500:
501: /**
502: * Assigns {@link HostParams HTTP protocol parameters} specific to this host.
503: *
504: * @since 3.0
505: *
506: * @see HostParams
507: */
508: public void setParams(final HostParams params) {
509: if (params == null) {
510: throw new IllegalArgumentException(
511: "Parameters may not be null");
512: }
513: this .params = params;
514: }
515:
516: /**
517: * @see java.lang.Object#equals(java.lang.Object)
518: */
519: public synchronized boolean equals(final Object o) {
520: if (o instanceof HostConfiguration) {
521: // shortcut if we're comparing with ourselves
522: if (o == this ) {
523: return true;
524: }
525: HostConfiguration that = (HostConfiguration) o;
526: return LangUtils.equals(this .host, that.host)
527: && LangUtils.equals(this .proxyHost, that.proxyHost)
528: && LangUtils.equals(this .localAddress,
529: that.localAddress);
530: } else {
531: return false;
532: }
533:
534: }
535:
536: /**
537: * @see java.lang.Object#hashCode()
538: */
539: public synchronized int hashCode() {
540: int hash = LangUtils.HASH_SEED;
541: hash = LangUtils.hashCode(hash, this.host);
542: hash = LangUtils.hashCode(hash, this.proxyHost);
543: hash = LangUtils.hashCode(hash, this.localAddress);
544: return hash;
545: }
546:
547: }
|