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