1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http;
29
30 import java.io.Serializable;
31 import java.net.InetAddress;
32 import java.util.Locale;
33
34 import org.apache.http.annotation.ThreadingBehavior;
35 import org.apache.http.annotation.Contract;
36 import org.apache.http.util.Args;
37 import org.apache.http.util.LangUtils;
38
39
40
41
42
43
44
45 @Contract(threading = ThreadingBehavior.IMMUTABLE)
46 public final class HttpHost implements Cloneable, Serializable {
47
48 private static final long serialVersionUID = -7529410654042457626L;
49
50
51 public static final String DEFAULT_SCHEME_NAME = "http";
52
53
54 protected final String hostname;
55
56
57 protected final String lcHostname;
58
59
60
61 protected final int port;
62
63
64 protected final String schemeName;
65
66 protected final InetAddress address;
67
68
69
70
71
72
73
74
75
76
77
78 public HttpHost(final String hostname, final int port, final String scheme) {
79 super();
80 this.hostname = Args.containsNoBlanks(hostname, "Host name");
81 this.lcHostname = hostname.toLowerCase(Locale.ROOT);
82 if (scheme != null) {
83 this.schemeName = scheme.toLowerCase(Locale.ROOT);
84 } else {
85 this.schemeName = DEFAULT_SCHEME_NAME;
86 }
87 this.port = port;
88 this.address = null;
89 }
90
91
92
93
94
95
96
97
98 public HttpHost(final String hostname, final int port) {
99 this(hostname, port, null);
100 }
101
102
103
104
105
106
107 public static HttpHost create(final String s) {
108 Args.containsNoBlanks(s, "HTTP Host");
109 String text = s;
110 String scheme = null;
111 final int schemeIdx = text.indexOf("://");
112 if (schemeIdx > 0) {
113 scheme = text.substring(0, schemeIdx);
114 text = text.substring(schemeIdx + 3);
115 }
116 int port = -1;
117 final int portIdx = text.lastIndexOf(":");
118 if (portIdx > 0) {
119 try {
120 port = Integer.parseInt(text.substring(portIdx + 1));
121 } catch (final NumberFormatException ex) {
122 throw new IllegalArgumentException("Invalid HTTP host: " + text);
123 }
124 text = text.substring(0, portIdx);
125 }
126 return new HttpHost(text, port, scheme);
127 }
128
129
130
131
132
133
134 public HttpHost(final String hostname) {
135 this(hostname, -1, null);
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public HttpHost(final InetAddress address, final int port, final String scheme) {
151 this(Args.notNull(address,"Inet address"), address.getHostName(), port, scheme);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public HttpHost(final InetAddress address, final String hostname, final int port, final String scheme) {
168 super();
169 this.address = Args.notNull(address, "Inet address");
170 this.hostname = Args.notNull(hostname, "Hostname");
171 this.lcHostname = this.hostname.toLowerCase(Locale.ROOT);
172 if (scheme != null) {
173 this.schemeName = scheme.toLowerCase(Locale.ROOT);
174 } else {
175 this.schemeName = DEFAULT_SCHEME_NAME;
176 }
177 this.port = port;
178 }
179
180
181
182
183
184
185
186
187
188
189
190 public HttpHost(final InetAddress address, final int port) {
191 this(address, port, null);
192 }
193
194
195
196
197
198
199
200
201
202 public HttpHost(final InetAddress address) {
203 this(address, -1, null);
204 }
205
206
207
208
209
210
211 public HttpHostml#HttpHost">HttpHost (final HttpHost httphost) {
212 super();
213 Args.notNull(httphost, "HTTP host");
214 this.hostname = httphost.hostname;
215 this.lcHostname = httphost.lcHostname;
216 this.schemeName = httphost.schemeName;
217 this.port = httphost.port;
218 this.address = httphost.address;
219 }
220
221
222
223
224
225
226 public String getHostName() {
227 return this.hostname;
228 }
229
230
231
232
233
234
235 public int getPort() {
236 return this.port;
237 }
238
239
240
241
242
243
244 public String getSchemeName() {
245 return this.schemeName;
246 }
247
248
249
250
251
252
253
254
255 public InetAddress getAddress() {
256 return this.address;
257 }
258
259
260
261
262
263
264 public String toURI() {
265 final StringBuilder buffer = new StringBuilder();
266 buffer.append(this.schemeName);
267 buffer.append("://");
268 buffer.append(this.hostname);
269 if (this.port != -1) {
270 buffer.append(':');
271 buffer.append(Integer.toString(this.port));
272 }
273 return buffer.toString();
274 }
275
276
277
278
279
280
281
282 public String toHostString() {
283 if (this.port != -1) {
284
285 final StringBuilder buffer = new StringBuilder(this.hostname.length() + 6);
286 buffer.append(this.hostname);
287 buffer.append(":");
288 buffer.append(Integer.toString(this.port));
289 return buffer.toString();
290 }
291 return this.hostname;
292 }
293
294
295 @Override
296 public String toString() {
297 return toURI();
298 }
299
300
301 @Override
302 public boolean equals(final Object obj) {
303 if (this == obj) {
304 return true;
305 }
306 if (obj instanceof HttpHost) {
307 final HttpHost../../../org/apache/http/HttpHost.html#HttpHost">HttpHost that = (HttpHost) obj;
308 return this.lcHostname.equals(that.lcHostname)
309 && this.port == that.port
310 && this.schemeName.equals(that.schemeName)
311 && (this.address==null ? that.address== null : this.address.equals(that.address));
312 }
313 return false;
314 }
315
316
317
318
319 @Override
320 public int hashCode() {
321 int hash = LangUtils.HASH_SEED;
322 hash = LangUtils.hashCode(hash, this.lcHostname);
323 hash = LangUtils.hashCode(hash, this.port);
324 hash = LangUtils.hashCode(hash, this.schemeName);
325 if (address!=null) {
326 hash = LangUtils.hashCode(hash, address);
327 }
328 return hash;
329 }
330
331 @Override
332 public Object clone() throws CloneNotSupportedException {
333 return super.clone();
334 }
335
336 }