001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpHost.java,v 1.3 2005/01/14 21:16:40 olegk Exp $
003: * $Revision: 510587 $
004: * $Date: 2007-02-22 17:56:08 +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.protocol.Protocol;
034: import org.apache.commons.httpclient.util.LangUtils;
035:
036: /**
037: * Holds all of the variables needed to describe an HTTP connection to a host. This includes
038: * remote host, port and protocol.
039: *
040: * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
041: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
042: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
043: * @author Laura Werner
044: *
045: * @since 3.0
046: */
047: public class HttpHost implements Cloneable {
048:
049: /** The host to use. */
050: private String hostname = null;
051:
052: /** The port to use. */
053: private int port = -1;
054:
055: /** The protocol */
056: private Protocol protocol = null;
057:
058: /**
059: * Constructor for HttpHost.
060: *
061: * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
062: * @param port the port. Value <code>-1</code> can be used to set default protocol port
063: * @param protocol the protocol. Value <code>null</code> can be used to set default protocol
064: */
065: public HttpHost(final String hostname, int port,
066: final Protocol protocol) {
067: super ();
068: if (hostname == null) {
069: throw new IllegalArgumentException(
070: "Host name may not be null");
071: }
072: if (protocol == null) {
073: throw new IllegalArgumentException(
074: "Protocol may not be null");
075: }
076: this .hostname = hostname;
077: this .protocol = protocol;
078: if (port >= 0) {
079: this .port = port;
080: } else {
081: this .port = this .protocol.getDefaultPort();
082: }
083: }
084:
085: /**
086: * Constructor for HttpHost.
087: *
088: * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
089: * @param port the port. Value <code>-1</code> can be used to set default protocol port
090: */
091: public HttpHost(final String hostname, int port) {
092: this (hostname, port, Protocol.getProtocol("http"));
093: }
094:
095: /**
096: * Constructor for HttpHost.
097: *
098: * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
099: */
100: public HttpHost(final String hostname) {
101: this (hostname, -1, Protocol.getProtocol("http"));
102: }
103:
104: /**
105: * URI constructor for HttpHost.
106: *
107: * @param uri the URI.
108: */
109: public HttpHost(final URI uri) throws URIException {
110: this (uri.getHost(), uri.getPort(), Protocol.getProtocol(uri
111: .getScheme()));
112: }
113:
114: /**
115: * Copy constructor for HttpHost
116: *
117: * @param httphost the HTTP host to copy details from
118: */
119: public HttpHost(final HttpHost httphost) {
120: super ();
121: init(httphost);
122: }
123:
124: private void init(final HttpHost httphost) {
125: this .hostname = httphost.hostname;
126: this .port = httphost.port;
127: this .protocol = httphost.protocol;
128: }
129:
130: /**
131: * @throws CloneNotSupportedException
132: * @see java.lang.Object#clone()
133: */
134: public Object clone() throws CloneNotSupportedException {
135: HttpHost copy = (HttpHost) super .clone();
136: copy.init(this );
137: return copy;
138: }
139:
140: /**
141: * Returns the host name (IP or DNS name).
142: *
143: * @return the host name (IP or DNS name), or <code>null</code> if not set
144: */
145: public String getHostName() {
146: return this .hostname;
147: }
148:
149: /**
150: * Returns the port.
151: *
152: * @return the host port, or <code>-1</code> if not set
153: */
154: public int getPort() {
155: return this .port;
156: }
157:
158: /**
159: * Returns the protocol.
160: * @return The protocol.
161: */
162: public Protocol getProtocol() {
163: return this .protocol;
164: }
165:
166: /**
167: * Return the host uri.
168: *
169: * @return The host uri.
170: */
171: public String toURI() {
172: StringBuffer buffer = new StringBuffer(50);
173: buffer.append(this .protocol.getScheme());
174: buffer.append("://");
175: buffer.append(this .hostname);
176: if (this .port != this .protocol.getDefaultPort()) {
177: buffer.append(':');
178: buffer.append(this .port);
179: }
180: return buffer.toString();
181: }
182:
183: /**
184: * @see java.lang.Object#toString()
185: */
186: public String toString() {
187: StringBuffer buffer = new StringBuffer(50);
188: buffer.append(toURI());
189: return buffer.toString();
190: }
191:
192: /**
193: * @see java.lang.Object#equals(java.lang.Object)
194: */
195: public boolean equals(final Object o) {
196:
197: if (o instanceof HttpHost) {
198: // shortcut if we're comparing with ourselves
199: if (o == this ) {
200: return true;
201: }
202: HttpHost that = (HttpHost) o;
203: if (!this .hostname.equalsIgnoreCase(that.hostname)) {
204: return false;
205: }
206: if (this .port != that.port) {
207: return false;
208: }
209: if (!this .protocol.equals(that.protocol)) {
210: return false;
211: }
212: // everything matches
213: return true;
214: } else {
215: return false;
216: }
217: }
218:
219: /**
220: * @see java.lang.Object#hashCode()
221: */
222: public int hashCode() {
223: int hash = LangUtils.HASH_SEED;
224: hash = LangUtils.hashCode(hash, this.hostname);
225: hash = LangUtils.hashCode(hash, this.port);
226: hash = LangUtils.hashCode(hash, this.protocol);
227: return hash;
228: }
229:
230: }
|