001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/HttpHost.java $
003: * $Revision: 604514 $
004: * $Date: 2007-12-15 21:49:40 +0100 (Sat, 15 Dec 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with 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,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http;
033:
034: import org.apache.http.util.CharArrayBuffer;
035: import org.apache.http.util.LangUtils;
036:
037: /**
038: * Holds all of the variables needed to describe an HTTP connection to a host.
039: * This includes remote host name, port and scheme.
040: *
041: * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
042: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
043: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
044: * @author Laura Werner
045: *
046: * @since 4.0
047: */
048: public final class HttpHost implements Cloneable {
049:
050: /** The default scheme is "http". */
051: public static final String DEFAULT_SCHEME_NAME = "http";
052:
053: /** The host to use. */
054: protected final String hostname;
055:
056: /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
057: protected final String lcHostname;
058:
059: /** The port to use. */
060: protected final int port;
061:
062: /** The scheme */
063: protected final String schemeName;
064:
065: /**
066: * Creates a new {@link HttpHost HttpHost}, specifying all values.
067: * Constructor for HttpHost.
068: *
069: * @param hostname the hostname (IP or DNS name)
070: * @param port the port number.
071: * <code>-1</code> indicates the scheme default port.
072: * @param scheme the name of the scheme.
073: * <code>null</code> indicates the
074: * {@link #DEFAULT_SCHEME_NAME default scheme}
075: */
076: public HttpHost(final String hostname, int port, final String scheme) {
077: super ();
078: if (hostname == null) {
079: throw new IllegalArgumentException(
080: "Host name may not be null");
081: }
082: this .hostname = hostname;
083: this .lcHostname = hostname.toLowerCase();
084: if (scheme != null) {
085: this .schemeName = scheme.toLowerCase();
086: } else {
087: this .schemeName = DEFAULT_SCHEME_NAME;
088: }
089: this .port = port;
090: }
091:
092: /**
093: * Creates a new {@link HttpHost HttpHost}, with default scheme.
094: *
095: * @param hostname the hostname (IP or DNS name)
096: * @param port the port number.
097: * <code>-1</code> indicates the scheme default port.
098: */
099: public HttpHost(final String hostname, int port) {
100: this (hostname, port, null);
101: }
102:
103: /**
104: * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
105: *
106: * @param hostname the hostname (IP or DNS name)
107: */
108: public HttpHost(final String hostname) {
109: this (hostname, -1, null);
110: }
111:
112: /**
113: * Copy constructor for {@link HttpHost HttpHost}.
114: *
115: * @param httphost the HTTP host to copy details from
116: */
117: public HttpHost(final HttpHost httphost) {
118: this (httphost.hostname, httphost.port, httphost.schemeName);
119: }
120:
121: /**
122: * Returns the host name.
123: *
124: * @return the host name (IP or DNS name)
125: */
126: public String getHostName() {
127: return this .hostname;
128: }
129:
130: /**
131: * Returns the port.
132: *
133: * @return the host port, or <code>-1</code> if not set
134: */
135: public int getPort() {
136: return this .port;
137: }
138:
139: /**
140: * Returns the scheme name.
141: *
142: * @return the scheme name
143: */
144: public String getSchemeName() {
145: return this .schemeName;
146: }
147:
148: /**
149: * Return the host URI, as a string.
150: *
151: * @return the host URI
152: */
153: public String toURI() {
154: CharArrayBuffer buffer = new CharArrayBuffer(32);
155: buffer.append(this .schemeName);
156: buffer.append("://");
157: buffer.append(this .hostname);
158: if (this .port != -1) {
159: buffer.append(':');
160: buffer.append(Integer.toString(this .port));
161: }
162: return buffer.toString();
163: }
164:
165: /**
166: * Obtains the host string, without scheme prefix.
167: *
168: * @return the host string, for example <code>localhost:8080</code>
169: */
170: public String toHostString() {
171: CharArrayBuffer buffer = new CharArrayBuffer(32);
172: buffer.append(this .hostname);
173: if (this .port != -1) {
174: buffer.append(':');
175: buffer.append(Integer.toString(this .port));
176: }
177: return buffer.toString();
178: }
179:
180: public String toString() {
181: return toURI();
182: }
183:
184: public boolean equals(final Object obj) {
185: if (obj == null)
186: return false;
187: if (this == obj)
188: return true;
189: if (obj instanceof HttpHost) {
190: HttpHost that = (HttpHost) obj;
191: return this .lcHostname.equals(that.lcHostname)
192: && this .port == that.port
193: && this .schemeName.equals(that.schemeName);
194: } else {
195: return false;
196: }
197: }
198:
199: /**
200: * @see java.lang.Object#hashCode()
201: */
202: public int hashCode() {
203: int hash = LangUtils.HASH_SEED;
204: hash = LangUtils.hashCode(hash, this .lcHostname);
205: hash = LangUtils.hashCode(hash, this .port);
206: hash = LangUtils.hashCode(hash, this .schemeName);
207: return hash;
208: }
209:
210: public Object clone() throws CloneNotSupportedException {
211: return super.clone();
212: }
213:
214: }
|