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.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   * Holds all of the variables needed to describe an HTTP connection to a host.
41   * This includes remote host name, port and scheme.
42   *
43   *
44   * @since 4.0
45   */
46  @Contract(threading = ThreadingBehavior.IMMUTABLE)
47  public final class HttpHost implements Cloneable, Serializable {
48  
49      private static final long serialVersionUID = -7529410654042457626L;
50  
51      /** The default scheme is "http". */
52      public static final String DEFAULT_SCHEME_NAME = "http";
53  
54      /** The host to use. */
55      protected final String hostname;
56  
57      /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
58      protected final String lcHostname;
59  
60  
61      /** The port to use, defaults to -1 if not set. */
62      protected final int port;
63  
64      /** The scheme (lowercased) */
65      protected final String schemeName;
66  
67      protected final InetAddress address;
68  
69      /**
70       * Creates {@code HttpHost} instance with the given scheme, hostname and port.
71       *
72       * @param hostname  the hostname (IP or DNS name)
73       * @param port      the port number.
74       *                  {@code -1} indicates the scheme default port.
75       * @param scheme    the name of the scheme.
76       *                  {@code null} indicates the
77       *                  {@link #DEFAULT_SCHEME_NAME default scheme}
78       */
79      public HttpHost(final String hostname, final int port, final String scheme) {
80          super();
81          this.hostname   = Args.containsNoBlanks(hostname, "Host name");
82          this.lcHostname = hostname.toLowerCase(Locale.ROOT);
83          if (scheme != null) {
84              this.schemeName = scheme.toLowerCase(Locale.ROOT);
85          } else {
86              this.schemeName = DEFAULT_SCHEME_NAME;
87          }
88          this.port = port;
89          this.address = null;
90      }
91  
92      /**
93       * Creates {@code HttpHost} instance with the default scheme and the given hostname and port.
94       *
95       * @param hostname  the hostname (IP or DNS name)
96       * @param port      the port number.
97       *                  {@code -1} indicates the scheme default port.
98       */
99      public HttpHost(final String hostname, final int port) {
100         this(hostname, port, null);
101     }
102 
103     /**
104      * Creates {@code HttpHost} instance from string. Text may not contain any blanks.
105      *
106      * @since 4.4
107      */
108     public static HttpHost create(final String s) {
109         Args.containsNoBlanks(s, "HTTP Host");
110         String text = s;
111         String scheme = null;
112         final int schemeIdx = text.indexOf("://");
113         if (schemeIdx > 0) {
114             scheme = text.substring(0, schemeIdx);
115             text = text.substring(schemeIdx + 3);
116         }
117         int port = -1;
118         final int portIdx = text.lastIndexOf(":");
119         if (portIdx > 0) {
120             try {
121                 port = Integer.parseInt(text.substring(portIdx + 1));
122             } catch (NumberFormatException ex) {
123                 throw new IllegalArgumentException("Invalid HTTP host: " + text);
124             }
125             text = text.substring(0, portIdx);
126         }
127         return new HttpHost(text, port, scheme);
128     }
129 
130     /**
131      * Creates {@code HttpHost} instance with the default scheme and port and the given hostname.
132      *
133      * @param hostname  the hostname (IP or DNS name)
134      */
135     public HttpHost(final String hostname) {
136         this(hostname, -1, null);
137     }
138 
139     /**
140      * Creates {@code HttpHost} instance with the given scheme, inet address and port.
141      *
142      * @param address   the inet address.
143      * @param port      the port number.
144      *                  {@code -1} indicates the scheme default port.
145      * @param scheme    the name of the scheme.
146      *                  {@code null} indicates the
147      *                  {@link #DEFAULT_SCHEME_NAME default scheme}
148      *
149      * @since 4.3
150      */
151     public HttpHost(final InetAddress address, final int port, final String scheme) {
152         this(Args.notNull(address,"Inet address"), address.getHostName(), port, scheme);
153     }
154     /**
155      * Creates a new {@link HttpHost HttpHost}, specifying all values.
156      * Constructor for HttpHost.
157      *
158      * @param address   the inet address.
159      * @param hostname   the hostname (IP or DNS name)
160      * @param port      the port number.
161      *                  {@code -1} indicates the scheme default port.
162      * @param scheme    the name of the scheme.
163      *                  {@code null} indicates the
164      *                  {@link #DEFAULT_SCHEME_NAME default scheme}
165      *
166      * @since 4.4
167      */
168     public HttpHost(final InetAddress address, final String hostname, final int port, final String scheme) {
169         super();
170         this.address = Args.notNull(address, "Inet address");
171         this.hostname = Args.notNull(hostname, "Hostname");
172         this.lcHostname = this.hostname.toLowerCase(Locale.ROOT);
173         if (scheme != null) {
174             this.schemeName = scheme.toLowerCase(Locale.ROOT);
175         } else {
176             this.schemeName = DEFAULT_SCHEME_NAME;
177         }
178         this.port = port;
179     }
180 
181     /**
182      * Creates {@code HttpHost} instance with the default scheme and the given inet address
183      * and port.
184      *
185      * @param address   the inet address.
186      * @param port      the port number.
187      *                  {@code -1} indicates the scheme default port.
188      *
189      * @since 4.3
190      */
191     public HttpHost(final InetAddress address, final int port) {
192         this(address, port, null);
193     }
194 
195     /**
196      * Creates {@code HttpHost} instance with the default scheme and port and the given inet
197      * address.
198      *
199      * @param address   the inet address.
200      *
201      * @since 4.3
202      */
203     public HttpHost(final InetAddress address) {
204         this(address, -1, null);
205     }
206 
207     /**
208      * Copy constructor for {@link HttpHost HttpHost}.
209      *
210      * @param httphost the HTTP host to copy details from
211      */
212     public HttpHost (final HttpHost httphost) {
213         super();
214         Args.notNull(httphost, "HTTP host");
215         this.hostname   = httphost.hostname;
216         this.lcHostname = httphost.lcHostname;
217         this.schemeName = httphost.schemeName;
218         this.port = httphost.port;
219         this.address = httphost.address;
220     }
221 
222     /**
223      * Returns the host name.
224      *
225      * @return the host name (IP or DNS name)
226      */
227     public String getHostName() {
228         return this.hostname;
229     }
230 
231     /**
232      * Returns the port.
233      *
234      * @return the host port, or {@code -1} if not set
235      */
236     public int getPort() {
237         return this.port;
238     }
239 
240     /**
241      * Returns the scheme name.
242      *
243      * @return the scheme name
244      */
245     public String getSchemeName() {
246         return this.schemeName;
247     }
248 
249     /**
250      * Returns the inet address if explicitly set by a constructor,
251      *   {@code null} otherwise.
252      * @return the inet address
253      *
254      * @since 4.3
255      */
256     public InetAddress getAddress() {
257         return this.address;
258     }
259 
260     /**
261      * Return the host URI, as a string.
262      *
263      * @return the host URI
264      */
265     public String toURI() {
266         final StringBuilder buffer = new StringBuilder();
267         buffer.append(this.schemeName);
268         buffer.append("://");
269         buffer.append(this.hostname);
270         if (this.port != -1) {
271             buffer.append(':');
272             buffer.append(Integer.toString(this.port));
273         }
274         return buffer.toString();
275     }
276 
277 
278     /**
279      * Obtains the host string, without scheme prefix.
280      *
281      * @return  the host string, for example {@code localhost:8080}
282      */
283     public String toHostString() {
284         if (this.port != -1) {
285             //the highest port number is 65535, which is length 6 with the addition of the colon
286             final StringBuilder buffer = new StringBuilder(this.hostname.length() + 6);
287             buffer.append(this.hostname);
288             buffer.append(":");
289             buffer.append(Integer.toString(this.port));
290             return buffer.toString();
291         } else {
292             return this.hostname;
293         }
294     }
295 
296 
297     @Override
298     public String toString() {
299         return toURI();
300     }
301 
302 
303     @Override
304     public boolean equals(final Object obj) {
305         if (this == obj) {
306             return true;
307         }
308         if (obj instanceof HttpHost) {
309             final HttpHost that = (HttpHost) obj;
310             return this.lcHostname.equals(that.lcHostname)
311                 && this.port == that.port
312                 && this.schemeName.equals(that.schemeName)
313                 && (this.address==null ? that.address== null : this.address.equals(that.address));
314         } else {
315             return false;
316         }
317     }
318 
319     /**
320      * @see java.lang.Object#hashCode()
321      */
322     @Override
323     public int hashCode() {
324         int hash = LangUtils.HASH_SEED;
325         hash = LangUtils.hashCode(hash, this.lcHostname);
326         hash = LangUtils.hashCode(hash, this.port);
327         hash = LangUtils.hashCode(hash, this.schemeName);
328         if (address!=null) {
329             hash = LangUtils.hashCode(hash, address);
330         }
331         return hash;
332     }
333 
334     @Override
335     public Object clone() throws CloneNotSupportedException {
336         return super.clone();
337     }
338 
339 }