• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpHost.java $
3  * $Revision: 653058 $
4  * $Date: 2008-05-03 05:01:10 -0700 (Sat, 03 May 2008) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http;
33 
34 import java.util.Locale;
35 
36 import org.apache.http.util.CharArrayBuffer;
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  * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
44  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
45  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46  * @author Laura Werner
47  *
48  * @since 4.0
49  *
50  * @deprecated Please use {@link java.net.URL#openConnection} instead.
51  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
52  *     for further details.
53  */
54 @Deprecated
55 public final class HttpHost implements Cloneable {
56 
57     /** The default scheme is "http". */
58     public static final String DEFAULT_SCHEME_NAME = "http";
59 
60     /** The host to use. */
61     protected final String hostname;
62 
63     /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
64     protected final String lcHostname;
65 
66 
67     /** The port to use. */
68     protected final int port;
69 
70     /** The scheme */
71     protected final String schemeName;
72 
73 
74     /**
75      * Creates a new {@link HttpHost HttpHost}, specifying all values.
76      * Constructor for HttpHost.
77      *
78      * @param hostname  the hostname (IP or DNS name)
79      * @param port      the port number.
80      *                  <code>-1</code> indicates the scheme default port.
81      * @param scheme    the name of the scheme.
82      *                  <code>null</code> indicates the
83      *                  {@link #DEFAULT_SCHEME_NAME default scheme}
84      */
HttpHost(final String hostname, int port, final String scheme)85     public HttpHost(final String hostname, int port, final String scheme) {
86         super();
87         if (hostname == null) {
88             throw new IllegalArgumentException("Host name may not be null");
89         }
90         this.hostname   = hostname;
91         this.lcHostname = hostname.toLowerCase(Locale.ENGLISH);
92         if (scheme != null) {
93             this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
94         } else {
95             this.schemeName = DEFAULT_SCHEME_NAME;
96         }
97         this.port = port;
98     }
99 
100     /**
101      * Creates a new {@link HttpHost HttpHost}, with default scheme.
102      *
103      * @param hostname  the hostname (IP or DNS name)
104      * @param port      the port number.
105      *                  <code>-1</code> indicates the scheme default port.
106      */
HttpHost(final String hostname, int port)107     public HttpHost(final String hostname, int port) {
108         this(hostname, port, null);
109     }
110 
111     /**
112      * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
113      *
114      * @param hostname  the hostname (IP or DNS name)
115      */
HttpHost(final String hostname)116     public HttpHost(final String hostname) {
117         this(hostname, -1, null);
118     }
119 
120     /**
121      * Copy constructor for {@link HttpHost HttpHost}.
122      *
123      * @param httphost the HTTP host to copy details from
124      */
HttpHost(final HttpHost httphost)125     public HttpHost (final HttpHost httphost) {
126         this(httphost.hostname, httphost.port, httphost.schemeName);
127     }
128 
129     /**
130      * Returns the host name.
131      *
132      * @return the host name (IP or DNS name)
133      */
getHostName()134     public String getHostName() {
135         return this.hostname;
136     }
137 
138     /**
139      * Returns the port.
140      *
141      * @return the host port, or <code>-1</code> if not set
142      */
getPort()143     public int getPort() {
144         return this.port;
145     }
146 
147     /**
148      * Returns the scheme name.
149      *
150      * @return the scheme name
151      */
getSchemeName()152     public String getSchemeName() {
153         return this.schemeName;
154     }
155 
156     /**
157      * Return the host URI, as a string.
158      *
159      * @return the host URI
160      */
toURI()161     public String toURI() {
162         CharArrayBuffer buffer = new CharArrayBuffer(32);
163         buffer.append(this.schemeName);
164         buffer.append("://");
165         buffer.append(this.hostname);
166         if (this.port != -1) {
167             buffer.append(':');
168             buffer.append(Integer.toString(this.port));
169         }
170         return buffer.toString();
171     }
172 
173 
174     /**
175      * Obtains the host string, without scheme prefix.
176      *
177      * @return  the host string, for example <code>localhost:8080</code>
178      */
toHostString()179     public String toHostString() {
180         CharArrayBuffer buffer = new CharArrayBuffer(32);
181         buffer.append(this.hostname);
182         if (this.port != -1) {
183             buffer.append(':');
184             buffer.append(Integer.toString(this.port));
185         }
186         return buffer.toString();
187     }
188 
189 
toString()190     public String toString() {
191         return toURI();
192     }
193 
194 
equals(final Object obj)195     public boolean equals(final Object obj) {
196         if (obj == null) return false;
197         if (this == obj) return true;
198         if (obj instanceof HttpHost) {
199             HttpHost that = (HttpHost) obj;
200             return this.lcHostname.equals(that.lcHostname)
201                 && this.port == that.port
202                 && this.schemeName.equals(that.schemeName);
203         } else {
204             return false;
205         }
206     }
207 
208     /**
209      * @see java.lang.Object#hashCode()
210      */
hashCode()211     public int hashCode() {
212         int hash = LangUtils.HASH_SEED;
213         hash = LangUtils.hashCode(hash, this.lcHostname);
214         hash = LangUtils.hashCode(hash, this.port);
215         hash = LangUtils.hashCode(hash, this.schemeName);
216         return hash;
217     }
218 
clone()219     public Object clone() throws CloneNotSupportedException {
220         return super.clone();
221     }
222 
223 }
224