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.util.Locale;
32  
33  import org.apache.http.annotation.Immutable;
34  import org.apache.http.util.LangUtils;
35  
36  /**
37   * Holds all of the variables needed to describe an HTTP connection to a host.
38   * This includes remote host name, port and scheme.
39   *
40   *
41   * @since 4.0
42   */
43  @Immutable
44  public final class HttpHost implements Cloneable, Serializable {
45  
46      private static final long serialVersionUID = -7529410654042457626L;
47  
48      /** The default scheme is "http". */
49      public static final String DEFAULT_SCHEME_NAME = "http";
50  
51      /** The host to use. */
52      protected final String hostname;
53  
54      /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
55      protected final String lcHostname;
56  
57  
58      /** The port to use, defaults to -1 if not set. */
59      protected final int port;
60  
61      /** The scheme (lowercased) */
62      protected final String schemeName;
63  
64  
65      /**
66       * Creates a new {@link HttpHost HttpHost}, specifying all values.
67       * Constructor for HttpHost.
68       *
69       * @param hostname  the hostname (IP or DNS name)
70       * @param port      the port number.
71       *                  <code>-1</code> indicates the scheme default port.
72       * @param scheme    the name of the scheme.
73       *                  <code>null</code> indicates the
74       *                  {@link #DEFAULT_SCHEME_NAME default scheme}
75       */
76      public HttpHost(final String hostname, int port, final String scheme) {
77          super();
78          if (hostname == null) {
79              throw new IllegalArgumentException("Host name may not be null");
80          }
81          this.hostname   = hostname;
82          this.lcHostname = hostname.toLowerCase(Locale.ENGLISH);
83          if (scheme != null) {
84              this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
85          } else {
86              this.schemeName = DEFAULT_SCHEME_NAME;
87          }
88          this.port = port;
89      }
90  
91      /**
92       * Creates a new {@link HttpHost HttpHost}, with default scheme.
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, int port) {
99          this(hostname, port, null);
100     }
101 
102     /**
103      * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
104      *
105      * @param hostname  the hostname (IP or DNS name)
106      */
107     public HttpHost(final String hostname) {
108         this(hostname, -1, null);
109     }
110 
111     /**
112      * Copy constructor for {@link HttpHost HttpHost}.
113      *
114      * @param httphost the HTTP host to copy details from
115      */
116     public HttpHost (final HttpHost httphost) {
117         this(httphost.hostname, httphost.port, httphost.schemeName);
118     }
119 
120     /**
121      * Returns the host name.
122      *
123      * @return the host name (IP or DNS name)
124      */
125     public String getHostName() {
126         return this.hostname;
127     }
128 
129     /**
130      * Returns the port.
131      *
132      * @return the host port, or <code>-1</code> if not set
133      */
134     public int getPort() {
135         return this.port;
136     }
137 
138     /**
139      * Returns the scheme name.
140      *
141      * @return the scheme name
142      */
143     public String getSchemeName() {
144         return this.schemeName;
145     }
146 
147     /**
148      * Return the host URI, as a string.
149      *
150      * @return the host URI
151      */
152     public String toURI() {
153         StringBuilder buffer = new StringBuilder();
154         buffer.append(this.schemeName);
155         buffer.append("://");
156         buffer.append(this.hostname);
157         if (this.port != -1) {
158             buffer.append(':');
159             buffer.append(Integer.toString(this.port));
160         }
161         return buffer.toString();
162     }
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         if (this.port != -1) {
172             //the highest port number is 65535, which is length 6 with the addition of the colon
173             StringBuilder buffer = new StringBuilder(this.hostname.length() + 6);
174             buffer.append(this.hostname);
175             buffer.append(":");
176             buffer.append(Integer.toString(this.port));
177             return buffer.toString();
178         } else {
179             return this.hostname;
180         }
181     }
182 
183 
184     @Override
185     public String toString() {
186         return toURI();
187     }
188 
189 
190     @Override
191     public boolean equals(final Object obj) {
192         if (this == obj) return true;
193         if (obj instanceof HttpHost) {
194             HttpHost that = (HttpHost) obj;
195             return this.lcHostname.equals(that.lcHostname)
196                 && this.port == that.port
197                 && this.schemeName.equals(that.schemeName);
198         } else {
199             return false;
200         }
201     }
202 
203     /**
204      * @see java.lang.Object#hashCode()
205      */
206     @Override
207     public int hashCode() {
208         int hash = LangUtils.HASH_SEED;
209         hash = LangUtils.hashCode(hash, this.lcHostname);
210         hash = LangUtils.hashCode(hash, this.port);
211         hash = LangUtils.hashCode(hash, this.schemeName);
212         return hash;
213     }
214 
215     @Override
216     public Object clone() throws CloneNotSupportedException {
217         return super.clone();
218     }
219 
220 }