1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/AuthScope.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 652950 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-02 16:49:48 -0700 (Fri, 02 May 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  this work for additional information regarding copyright ownership.
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  (the "License"); you may not use this file except in compliance with
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  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, software
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  See the License for the specific language governing permissions and
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  limitations under the License.
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.auth;
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Locale;
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.util.LangUtils;
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The class represents an authentication scope consisting of a host name,
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * a port number, a realm name and an authentication scheme name which
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link Credentials Credentials} apply to.
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class AuthScope {
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The <tt>null</tt> value represents any host. In the future versions of
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * HttpClient the use of this parameter will be discontinued.
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static final String ANY_HOST = null;
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The <tt>-1</tt> value represents any port.
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static final int ANY_PORT = -1;
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The <tt>null</tt> value represents any realm.
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static final String ANY_REALM = null;
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The <tt>null</tt> value represents any authentication scheme.
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static final String ANY_SCHEME = null;
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Default scope matching any host, port, realm and authentication scheme.
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * In the future versions of HttpClient the use of this parameter will be
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * discontinued.
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The authentication scheme the credentials apply to. */
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final String scheme;
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The realm the credentials apply to. */
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final String realm;
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The host the credentials apply to. */
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final String host;
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The port the credentials apply to. */
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final int port;
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Creates a new credentials scope for the given
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <tt>authentication scheme</tt>.
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param host the host the credentials apply to. May be set
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to <tt>null</tt> if credenticals are applicable to
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any host.
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param port the port the credentials apply to. May be set
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to negative value if credenticals are applicable to
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any port.
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param realm the realm the credentials apply to. May be set
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to <tt>null</tt> if credenticals are applicable to
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any realm.
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param scheme the authentication scheme the credentials apply to.
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   May be set to <tt>null</tt> if credenticals are applicable to
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any authentication scheme.
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public AuthScope(final String host, int port,
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final String realm, final String scheme)
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    {
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.host =   (host == null)   ? ANY_HOST: host.toLowerCase(Locale.ENGLISH);
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.port =   (port < 0)       ? ANY_PORT: port;
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.realm =  (realm == null)  ? ANY_REALM: realm;
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase(Locale.ENGLISH);
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Creates a new credentials scope for the given
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * authentication scheme.
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param host the host the credentials apply to. May be set
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to <tt>null</tt> if credenticals are applicable to
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any host.
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param port the port the credentials apply to. May be set
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to negative value if credenticals are applicable to
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any port.
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param realm the realm the credentials apply to. May be set
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to <tt>null</tt> if credenticals are applicable to
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any realm.
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public AuthScope(final String host, int port, final String realm) {
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this(host, port, realm, ANY_SCHEME);
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Creates a new credentials scope for the given
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <tt>host</tt>, <tt>port</tt>, any realm name, and any
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * authentication scheme.
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param host the host the credentials apply to. May be set
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to <tt>null</tt> if credenticals are applicable to
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any host.
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param port the port the credentials apply to. May be set
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   to negative value if credenticals are applicable to
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *   any port.
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public AuthScope(final String host, int port) {
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this(host, port, ANY_REALM, ANY_SCHEME);
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a copy of the given credentials scope.
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public AuthScope(final AuthScope authscope) {
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        super();
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (authscope == null) {
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("Scope may not be null");
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.host = authscope.getHost();
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.port = authscope.getPort();
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.realm = authscope.getRealm();
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.scheme = authscope.getScheme();
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the host
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getHost() {
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.host;
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the port
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int getPort() {
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.port;
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the realm name
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getRealm() {
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.realm;
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the scheme type
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getScheme() {
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.scheme;
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Tests if the authentication scopes match.
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the match factor. Negative value signifies no match.
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *    Non-negative signifies a match. The greater the returned value
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *    the closer the match.
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int match(final AuthScope that) {
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int factor = 0;
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (LangUtils.equals(this.scheme, that.scheme)) {
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            factor += 1;
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                return -1;
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (LangUtils.equals(this.realm, that.realm)) {
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            factor += 2;
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (this.realm != ANY_REALM && that.realm != ANY_REALM) {
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                return -1;
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.port == that.port) {
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            factor += 4;
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (this.port != ANY_PORT && that.port != ANY_PORT) {
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                return -1;
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (LangUtils.equals(this.host, that.host)) {
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            factor += 8;
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (this.host != ANY_HOST && that.host != ANY_HOST) {
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                return -1;
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return factor;
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see java.lang.Object#equals(Object)
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean equals(Object o) {
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (o == null) {
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return false;
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (o == this) {
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return true;
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!(o instanceof AuthScope)) {
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return super.equals(o);
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        AuthScope that = (AuthScope) o;
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        LangUtils.equals(this.host, that.host)
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project          && this.port == that.port
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project          && LangUtils.equals(this.realm, that.realm)
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project          && LangUtils.equals(this.scheme, that.scheme);
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see java.lang.Object#toString()
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String toString() {
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        StringBuffer buffer = new StringBuffer();
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.scheme != null) {
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(this.scheme.toUpperCase(Locale.ENGLISH));
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(' ');
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.realm != null) {
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append('\'');
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(this.realm);
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append('\'');
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append("<any realm>");
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.host != null) {
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append('@');
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            buffer.append(this.host);
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (this.port >= 0) {
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                buffer.append(':');
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                buffer.append(this.port);
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return buffer.toString();
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see java.lang.Object#hashCode()
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int hashCode() {
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int hash = LangUtils.HASH_SEED;
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        hash = LangUtils.hashCode(hash, this.host);
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        hash = LangUtils.hashCode(hash, this.port);
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        hash = LangUtils.hashCode(hash, this.realm);
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        hash = LangUtils.hashCode(hash, this.scheme);
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return hash;
291069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
292069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
293