• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/AuthScope.java $
3  * $Revision: 652950 $
4  * $Date: 2008-05-02 16:49:48 -0700 (Fri, 02 May 2008) $
5  *
6  * ====================================================================
7  *
8  *  Licensed to the Apache Software Foundation (ASF) under one or more
9  *  contributor license agreements.  See the NOTICE file distributed with
10  *  this work for additional information regarding copyright ownership.
11  *  The ASF licenses this file to You under the Apache License, Version 2.0
12  *  (the "License"); you may not use this file except in compliance with
13  *  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, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation.  For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */
30 
31 package org.apache.http.auth;
32 
33 import java.util.Locale;
34 
35 import org.apache.http.util.LangUtils;
36 
37 /**
38  * The class represents an authentication scope consisting of a host name,
39  * a port number, a realm name and an authentication scheme name which
40  * {@link Credentials Credentials} apply to.
41  *
42  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
43  * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
44  *
45  * @since 4.0
46  *
47  * @deprecated Please use {@link java.net.URL#openConnection} instead.
48  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
49  *     for further details.
50  */
51 @Deprecated
52 public class AuthScope {
53 
54     /**
55      * The <tt>null</tt> value represents any host. In the future versions of
56      * HttpClient the use of this parameter will be discontinued.
57      */
58     public static final String ANY_HOST = null;
59 
60     /**
61      * The <tt>-1</tt> value represents any port.
62      */
63     public static final int ANY_PORT = -1;
64 
65     /**
66      * The <tt>null</tt> value represents any realm.
67      */
68     public static final String ANY_REALM = null;
69 
70     /**
71      * The <tt>null</tt> value represents any authentication scheme.
72      */
73     public static final String ANY_SCHEME = null;
74 
75     /**
76      * Default scope matching any host, port, realm and authentication scheme.
77      * In the future versions of HttpClient the use of this parameter will be
78      * discontinued.
79      */
80     public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
81 
82     /** The authentication scheme the credentials apply to. */
83     private final String scheme;
84 
85     /** The realm the credentials apply to. */
86     private final String realm;
87 
88     /** The host the credentials apply to. */
89     private final String host;
90 
91     /** The port the credentials apply to. */
92     private final int port;
93 
94     /** Creates a new credentials scope for the given
95      * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and
96      * <tt>authentication scheme</tt>.
97      *
98      * @param host the host the credentials apply to. May be set
99      *   to <tt>null</tt> if credenticals are applicable to
100      *   any host.
101      * @param port the port the credentials apply to. May be set
102      *   to negative value if credenticals are applicable to
103      *   any port.
104      * @param realm the realm the credentials apply to. May be set
105      *   to <tt>null</tt> if credenticals are applicable to
106      *   any realm.
107      * @param scheme the authentication scheme the credentials apply to.
108      *   May be set to <tt>null</tt> if credenticals are applicable to
109      *   any authentication scheme.
110      */
AuthScope(final String host, int port, final String realm, final String scheme)111     public AuthScope(final String host, int port,
112         final String realm, final String scheme)
113     {
114         this.host =   (host == null)   ? ANY_HOST: host.toLowerCase(Locale.ENGLISH);
115         this.port =   (port < 0)       ? ANY_PORT: port;
116         this.realm =  (realm == null)  ? ANY_REALM: realm;
117         this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase(Locale.ENGLISH);
118     }
119 
120     /** Creates a new credentials scope for the given
121      * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
122      * authentication scheme.
123      *
124      * @param host the host the credentials apply to. May be set
125      *   to <tt>null</tt> if credenticals are applicable to
126      *   any host.
127      * @param port the port the credentials apply to. May be set
128      *   to negative value if credenticals are applicable to
129      *   any port.
130      * @param realm the realm the credentials apply to. May be set
131      *   to <tt>null</tt> if credenticals are applicable to
132      *   any realm.
133      */
AuthScope(final String host, int port, final String realm)134     public AuthScope(final String host, int port, final String realm) {
135         this(host, port, realm, ANY_SCHEME);
136     }
137 
138     /** Creates a new credentials scope for the given
139      * <tt>host</tt>, <tt>port</tt>, any realm name, and any
140      * authentication scheme.
141      *
142      * @param host the host the credentials apply to. May be set
143      *   to <tt>null</tt> if credenticals are applicable to
144      *   any host.
145      * @param port the port the credentials apply to. May be set
146      *   to negative value if credenticals are applicable to
147      *   any port.
148      */
AuthScope(final String host, int port)149     public AuthScope(final String host, int port) {
150         this(host, port, ANY_REALM, ANY_SCHEME);
151     }
152 
153     /**
154      * Creates a copy of the given credentials scope.
155      */
AuthScope(final AuthScope authscope)156     public AuthScope(final AuthScope authscope) {
157         super();
158         if (authscope == null) {
159             throw new IllegalArgumentException("Scope may not be null");
160         }
161         this.host = authscope.getHost();
162         this.port = authscope.getPort();
163         this.realm = authscope.getRealm();
164         this.scheme = authscope.getScheme();
165     }
166 
167     /**
168      * @return the host
169      */
getHost()170     public String getHost() {
171         return this.host;
172     }
173 
174     /**
175      * @return the port
176      */
getPort()177     public int getPort() {
178         return this.port;
179     }
180 
181     /**
182      * @return the realm name
183      */
getRealm()184     public String getRealm() {
185         return this.realm;
186     }
187 
188     /**
189      * @return the scheme type
190      */
getScheme()191     public String getScheme() {
192         return this.scheme;
193     }
194 
195     /**
196      * Tests if the authentication scopes match.
197      *
198      * @return the match factor. Negative value signifies no match.
199      *    Non-negative signifies a match. The greater the returned value
200      *    the closer the match.
201      */
match(final AuthScope that)202     public int match(final AuthScope that) {
203         int factor = 0;
204         if (LangUtils.equals(this.scheme, that.scheme)) {
205             factor += 1;
206         } else {
207             if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
208                 return -1;
209             }
210         }
211         if (LangUtils.equals(this.realm, that.realm)) {
212             factor += 2;
213         } else {
214             if (this.realm != ANY_REALM && that.realm != ANY_REALM) {
215                 return -1;
216             }
217         }
218         if (this.port == that.port) {
219             factor += 4;
220         } else {
221             if (this.port != ANY_PORT && that.port != ANY_PORT) {
222                 return -1;
223             }
224         }
225         if (LangUtils.equals(this.host, that.host)) {
226             factor += 8;
227         } else {
228             if (this.host != ANY_HOST && that.host != ANY_HOST) {
229                 return -1;
230             }
231         }
232         return factor;
233     }
234 
235     /**
236      * @see java.lang.Object#equals(Object)
237      */
238     @Override
equals(Object o)239     public boolean equals(Object o) {
240         if (o == null) {
241             return false;
242         }
243         if (o == this) {
244             return true;
245         }
246         if (!(o instanceof AuthScope)) {
247             return super.equals(o);
248         }
249         AuthScope that = (AuthScope) o;
250         return
251         LangUtils.equals(this.host, that.host)
252           && this.port == that.port
253           && LangUtils.equals(this.realm, that.realm)
254           && LangUtils.equals(this.scheme, that.scheme);
255     }
256 
257     /**
258      * @see java.lang.Object#toString()
259      */
260     @Override
toString()261     public String toString() {
262         StringBuffer buffer = new StringBuffer();
263         if (this.scheme != null) {
264             buffer.append(this.scheme.toUpperCase(Locale.ENGLISH));
265             buffer.append(' ');
266         }
267         if (this.realm != null) {
268             buffer.append('\'');
269             buffer.append(this.realm);
270             buffer.append('\'');
271         } else {
272             buffer.append("<any realm>");
273         }
274         if (this.host != null) {
275             buffer.append('@');
276             buffer.append(this.host);
277             if (this.port >= 0) {
278                 buffer.append(':');
279                 buffer.append(this.port);
280             }
281         }
282         return buffer.toString();
283     }
284 
285     /**
286      * @see java.lang.Object#hashCode()
287      */
288     @Override
hashCode()289     public int hashCode() {
290         int hash = LangUtils.HASH_SEED;
291         hash = LangUtils.hashCode(hash, this.host);
292         hash = LangUtils.hashCode(hash, this.port);
293         hash = LangUtils.hashCode(hash, this.realm);
294         hash = LangUtils.hashCode(hash, this.scheme);
295         return hash;
296     }
297 }
298