Source code: org/apache/http/HttpHost.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/HttpHost.java $
3 * $Revision: 390703 $
4 * $Date: 2006-04-01 19:35:45 +0200 (Sat, 01 Apr 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 2002-2004 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http;
31
32 import org.apache.http.io.CharArrayBuffer;
33 import org.apache.http.util.LangUtils;
34
35 /**
36 * Holds all of the variables needed to describe an HTTP connection to a host. This includes
37 * remote host, port and scheme.
38 *
39 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
40 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
41 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
42 * @author Laura Werner
43 *
44 * @since 3.0
45 */
46 public class HttpHost {
47
48 /** The host to use. */
49 private String hostname = null;
50
51 /** The port to use. */
52 private int port = -1;
53
54 /** The scheme */
55 private Scheme scheme = null;
56
57 /**
58 * Constructor for HttpHost.
59 *
60 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
61 * @param port the port. Value <code>-1</code> can be used to set default scheme port
62 * @param scheme the scheme. Value <code>null</code> can be used to set default scheme
63 */
64 public HttpHost(final String hostname, int port, final Scheme scheme) {
65 super();
66 if (hostname == null) {
67 throw new IllegalArgumentException("Host name may not be null");
68 }
69 if (scheme == null) {
70 throw new IllegalArgumentException("Protocol may not be null");
71 }
72 this.hostname = hostname;
73 this.scheme = scheme;
74 if (port >= 0) {
75 this.port = port;
76 } else {
77 this.port = this.scheme.getDefaultPort();
78 }
79 }
80
81 /**
82 * Constructor for HttpHost.
83 *
84 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
85 * @param port the port. Value <code>-1</code> can be used to set default scheme port
86 */
87 public HttpHost(final String hostname, int port) {
88 this(hostname, port, Scheme.getScheme("http"));
89 }
90
91 /**
92 * Constructor for HttpHost.
93 *
94 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
95 */
96 public HttpHost(final String hostname) {
97 this(hostname, -1, Scheme.getScheme("http"));
98 }
99
100 /**
101 * Copy constructor for HttpHost
102 *
103 * @param httphost the HTTP host to copy details from
104 */
105 public HttpHost (final HttpHost httphost) {
106 super();
107 this.hostname = httphost.hostname;
108 this.port = httphost.port;
109 this.scheme = httphost.scheme;
110 }
111
112 /**
113 * Returns the host name (IP or DNS name).
114 *
115 * @return the host name (IP or DNS name), or <code>null</code> if not set
116 */
117 public String getHostName() {
118 return this.hostname;
119 }
120
121 /**
122 * Returns the port.
123 *
124 * @return the host port, or <code>-1</code> if not set
125 */
126 public int getPort() {
127 return this.port;
128 }
129
130 /**
131 * Returns the scheme.
132 * @return The scheme.
133 */
134 public Scheme getScheme() {
135 return this.scheme;
136 }
137
138 /**
139 * Return the host uri.
140 *
141 * @return The host uri.
142 */
143 public String toURI() {
144 CharArrayBuffer buffer = new CharArrayBuffer(32);
145 buffer.append(this.scheme.getName());
146 buffer.append("://");
147 buffer.append(this.hostname);
148 if (this.port != this.scheme.getDefaultPort()) {
149 buffer.append(':');
150 buffer.append(Integer.toString(this.port));
151 }
152 return buffer.toString();
153 }
154
155 public String toHostString() {
156 CharArrayBuffer buffer = new CharArrayBuffer(32);
157 buffer.append(this.hostname);
158 if (this.port != this.scheme.getDefaultPort()) {
159 buffer.append(':');
160 buffer.append(Integer.toString(this.port));
161 }
162 return buffer.toString();
163 }
164
165 /**
166 * @see java.lang.Object#toString()
167 */
168 public String toString() {
169 return toURI();
170 }
171
172 /**
173 * @see java.lang.Object#equals(java.lang.Object)
174 */
175 public boolean equals(final Object obj) {
176 if (obj == null) return false;
177 if (this == obj) return true;
178 if (obj instanceof HttpHost) {
179 HttpHost that = (HttpHost) obj;
180 return this.hostname.equalsIgnoreCase(that.hostname)
181 && this.port == that.port
182 && this.scheme.equals(that.scheme);
183 } else {
184 return false;
185 }
186 }
187
188 /**
189 * @see java.lang.Object#hashCode()
190 */
191 public int hashCode() {
192 int hash = LangUtils.HASH_SEED;
193 hash = LangUtils.hashCode(hash, this.hostname.toUpperCase());
194 hash = LangUtils.hashCode(hash, this.port);
195 hash = LangUtils.hashCode(hash, this.scheme);
196 return hash;
197 }
198
199 }