1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpHost.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 653058 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-03 05:01:10 -0700 (Sat, 03 May 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements.  See the NOTICE file
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership.  The ASF licenses this file
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *   http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing,
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied.  See the License for the
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License.
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http;
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Locale;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.util.CharArrayBuffer;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.util.LangUtils;
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Holds all of the variables needed to describe an HTTP connection to a host.
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This includes remote host name, port and scheme.
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Laura Werner
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0
49d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *
50d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead.
51d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
52d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     for further details.
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
54d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath@Deprecated
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic final class HttpHost implements Cloneable {
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The default scheme is "http". */
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static final String DEFAULT_SCHEME_NAME = "http";
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The host to use. */
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final String hostname;
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final String lcHostname;
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The port to use. */
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final int port;
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The scheme */
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final String schemeName;
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new {@link HttpHost HttpHost}, specifying all values.
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Constructor for HttpHost.
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param hostname  the hostname (IP or DNS name)
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param port      the port number.
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  <code>-1</code> indicates the scheme default port.
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param scheme    the name of the scheme.
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  <code>null</code> indicates the
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  {@link #DEFAULT_SCHEME_NAME default scheme}
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public HttpHost(final String hostname, int port, final String scheme) {
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        super();
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (hostname == null) {
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("Host name may not be null");
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.hostname   = hostname;
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.lcHostname = hostname.toLowerCase(Locale.ENGLISH);
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (scheme != null) {
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            this.schemeName = DEFAULT_SCHEME_NAME;
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.port = port;
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new {@link HttpHost HttpHost}, with default scheme.
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param hostname  the hostname (IP or DNS name)
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param port      the port number.
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  <code>-1</code> indicates the scheme default port.
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public HttpHost(final String hostname, int port) {
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this(hostname, port, null);
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param hostname  the hostname (IP or DNS name)
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public HttpHost(final String hostname) {
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this(hostname, -1, null);
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Copy constructor for {@link HttpHost HttpHost}.
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param httphost the HTTP host to copy details from
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public HttpHost (final HttpHost httphost) {
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this(httphost.hostname, httphost.port, httphost.schemeName);
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the host name.
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the host name (IP or DNS name)
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getHostName() {
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.hostname;
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the port.
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the host port, or <code>-1</code> if not set
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int getPort() {
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.port;
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the scheme name.
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the scheme name
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getSchemeName() {
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.schemeName;
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Return the host URI, as a string.
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the host URI
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String toURI() {
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        CharArrayBuffer buffer = new CharArrayBuffer(32);
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.schemeName);
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append("://");
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.hostname);
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.port != -1) {
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(':');
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(Integer.toString(this.port));
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return buffer.toString();
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Obtains the host string, without scheme prefix.
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  the host string, for example <code>localhost:8080</code>
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String toHostString() {
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        CharArrayBuffer buffer = new CharArrayBuffer(32);
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        buffer.append(this.hostname);
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.port != -1) {
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(':');
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(Integer.toString(this.port));
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return buffer.toString();
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String toString() {
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return toURI();
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean equals(final Object obj) {
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (obj == null) return false;
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this == obj) return true;
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (obj instanceof HttpHost) {
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            HttpHost that = (HttpHost) obj;
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return this.lcHostname.equals(that.lcHostname)
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                && this.port == that.port
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                && this.schemeName.equals(that.schemeName);
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return false;
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see java.lang.Object#hashCode()
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int hashCode() {
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int hash = LangUtils.HASH_SEED;
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        hash = LangUtils.hashCode(hash, this.lcHostname);
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        hash = LangUtils.hashCode(hash, this.port);
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        hash = LangUtils.hashCode(hash, this.schemeName);
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return hash;
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Object clone() throws CloneNotSupportedException {
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return super.clone();
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
224