View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.Immutable;
35  import org.apache.http.util.Args;
36  import org.apache.http.util.LangUtils;
37  
38  /**
39   * Holds all of the variables needed to describe an HTTP connection to a host.
40   * This includes remote host name, port and scheme.
41   *
42   *
43   * @since 4.0
44   */
45  @Immutable
46  public final class HttpHost implements Cloneable, Serializable {
47  
48      private static final long serialVersionUID = -7529410654042457626L;
49  
50      /** The default scheme is "http". */
51      public static final String DEFAULT_SCHEME_NAME = "http";
52  
53      /** The host to use. */
54      protected final String hostname;
55  
56      /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
57      protected final String lcHostname;
58  
59  
60      /** The port to use, defaults to -1 if not set. */
61      protected final int port;
62  
63      /** The scheme (lowercased) */
64      protected final String schemeName;
65  
66      protected final InetAddress address;
67  
68      /**
69       * Creates <tt>HttpHost<tt/> instance with the given scheme, hostname and port.
70       *
71       * @param hostname  the hostname (IP or DNS name)
72       * @param port      the port number.
73       *                  <code>-1</code> indicates the scheme default port.
74       * @param scheme    the name of the scheme.
75       *                  <code>null</code> indicates the
76       *                  {@link #DEFAULT_SCHEME_NAME default scheme}
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       * Creates <tt>HttpHost<tt/> instance with the default scheme and the given hostname and port.
93       *
94       * @param hostname  the hostname (IP or DNS name)
95       * @param port      the port number.
96       *                  <code>-1</code> indicates the scheme default port.
97       */
98      public HttpHost(final String hostname, final int port) {
99          this(hostname, port, null);
100     }
101 
102     /**
103      * Creates <tt>HttpHost<tt/> instance from string. Text may not contain any blanks.
104      *
105      * @since 4.4
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 (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      * Creates <tt>HttpHost<tt/> instance with the default scheme and port and the given hostname.
131      *
132      * @param hostname  the hostname (IP or DNS name)
133      */
134     public HttpHost(final String hostname) {
135         this(hostname, -1, null);
136     }
137 
138     /**
139      * Creates <tt>HttpHost<tt/> instance with the given scheme, inet address and port.
140      *
141      * @param address   the inet address.
142      * @param port      the port number.
143      *                  <code>-1</code> indicates the scheme default port.
144      * @param scheme    the name of the scheme.
145      *                  <code>null</code> indicates the
146      *                  {@link #DEFAULT_SCHEME_NAME default scheme}
147      *
148      * @since 4.3
149      */
150     public HttpHost(final InetAddress address, final int port, final String scheme) {
151         super();
152         this.address = Args.notNull(address, "Inet address");
153         this.hostname = address.getHostAddress();
154         this.lcHostname = this.hostname.toLowerCase(Locale.ROOT);
155         if (scheme != null) {
156             this.schemeName = scheme.toLowerCase(Locale.ROOT);
157         } else {
158             this.schemeName = DEFAULT_SCHEME_NAME;
159         }
160         this.port = port;
161     }
162 
163     /**
164      * Creates <tt>HttpHost<tt/> instance with the default scheme and the given inet address
165      * and port.
166      *
167      * @param address   the inet address.
168      * @param port      the port number.
169      *                  <code>-1</code> indicates the scheme default port.
170      *
171      * @since 4.3
172      */
173     public HttpHost(final InetAddress address, final int port) {
174         this(address, port, null);
175     }
176 
177     /**
178      * Creates <tt>HttpHost<tt/> instance with the default scheme and port and the given inet
179      * address.
180      *
181      * @param address   the inet address.
182      *
183      * @since 4.3
184      */
185     public HttpHost(final InetAddress address) {
186         this(address, -1, null);
187     }
188 
189     /**
190      * Copy constructor for {@link HttpHost HttpHost}.
191      *
192      * @param httphost the HTTP host to copy details from
193      */
194     public HttpHost (final HttpHost httphost) {
195         super();
196         Args.notNull(httphost, "HTTP host");
197         this.hostname   = httphost.hostname;
198         this.lcHostname = httphost.lcHostname;
199         this.schemeName = httphost.schemeName;
200         this.port = httphost.port;
201         this.address = httphost.address;
202     }
203 
204     /**
205      * Returns the host name.
206      *
207      * @return the host name (IP or DNS name)
208      */
209     public String getHostName() {
210         return this.hostname;
211     }
212 
213     /**
214      * Returns the port.
215      *
216      * @return the host port, or <code>-1</code> if not set
217      */
218     public int getPort() {
219         return this.port;
220     }
221 
222     /**
223      * Returns the scheme name.
224      *
225      * @return the scheme name
226      */
227     public String getSchemeName() {
228         return this.schemeName;
229     }
230 
231     /**
232      * Returns the inet address if explicitly set by a constructor,
233      *   <code>null</code> otherwise.
234      * @return the inet address
235      *
236      * @since 4.3
237      */
238     public InetAddress getAddress() {
239         return this.address;
240     }
241 
242     /**
243      * Return the host URI, as a string.
244      *
245      * @return the host URI
246      */
247     public String toURI() {
248         final StringBuilder buffer = new StringBuilder();
249         buffer.append(this.schemeName);
250         buffer.append("://");
251         buffer.append(this.hostname);
252         if (this.port != -1) {
253             buffer.append(':');
254             buffer.append(Integer.toString(this.port));
255         }
256         return buffer.toString();
257     }
258 
259 
260     /**
261      * Obtains the host string, without scheme prefix.
262      *
263      * @return  the host string, for example <code>localhost:8080</code>
264      */
265     public String toHostString() {
266         if (this.port != -1) {
267             //the highest port number is 65535, which is length 6 with the addition of the colon
268             final StringBuilder buffer = new StringBuilder(this.hostname.length() + 6);
269             buffer.append(this.hostname);
270             buffer.append(":");
271             buffer.append(Integer.toString(this.port));
272             return buffer.toString();
273         } else {
274             return this.hostname;
275         }
276     }
277 
278 
279     @Override
280     public String toString() {
281         return toURI();
282     }
283 
284 
285     @Override
286     public boolean equals(final Object obj) {
287         if (this == obj) {
288             return true;
289         }
290         if (obj instanceof HttpHost) {
291             final HttpHost that = (HttpHost) obj;
292             return this.lcHostname.equals(that.lcHostname)
293                 && this.port == that.port
294                 && this.schemeName.equals(that.schemeName);
295         } else {
296             return false;
297         }
298     }
299 
300     /**
301      * @see java.lang.Object#hashCode()
302      */
303     @Override
304     public int hashCode() {
305         int hash = LangUtils.HASH_SEED;
306         hash = LangUtils.hashCode(hash, this.lcHostname);
307         hash = LangUtils.hashCode(hash, this.port);
308         hash = LangUtils.hashCode(hash, this.schemeName);
309         return hash;
310     }
311 
312     @Override
313     public Object clone() throws CloneNotSupportedException {
314         return super.clone();
315     }
316 
317 }