Home | History | Annotate | Download | only in auth
      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 (at) intencha.com">Adrian Sutton</a>
     44  *
     45  * @since 4.0
     46  */
     47 public class AuthScope {
     48 
     49     /**
     50      * The <tt>null</tt> value represents any host. In the future versions of
     51      * HttpClient the use of this parameter will be discontinued.
     52      */
     53     public static final String ANY_HOST = null;
     54 
     55     /**
     56      * The <tt>-1</tt> value represents any port.
     57      */
     58     public static final int ANY_PORT = -1;
     59 
     60     /**
     61      * The <tt>null</tt> value represents any realm.
     62      */
     63     public static final String ANY_REALM = null;
     64 
     65     /**
     66      * The <tt>null</tt> value represents any authentication scheme.
     67      */
     68     public static final String ANY_SCHEME = null;
     69 
     70     /**
     71      * Default scope matching any host, port, realm and authentication scheme.
     72      * In the future versions of HttpClient the use of this parameter will be
     73      * discontinued.
     74      */
     75     public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
     76 
     77     /** The authentication scheme the credentials apply to. */
     78     private final String scheme;
     79 
     80     /** The realm the credentials apply to. */
     81     private final String realm;
     82 
     83     /** The host the credentials apply to. */
     84     private final String host;
     85 
     86     /** The port the credentials apply to. */
     87     private final int port;
     88 
     89     /** Creates a new credentials scope for the given
     90      * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and
     91      * <tt>authentication scheme</tt>.
     92      *
     93      * @param host the host the credentials apply to. May be set
     94      *   to <tt>null</tt> if credenticals are applicable to
     95      *   any host.
     96      * @param port the port the credentials apply to. May be set
     97      *   to negative value if credenticals are applicable to
     98      *   any port.
     99      * @param realm the realm the credentials apply to. May be set
    100      *   to <tt>null</tt> if credenticals are applicable to
    101      *   any realm.
    102      * @param scheme the authentication scheme the credentials apply to.
    103      *   May be set to <tt>null</tt> if credenticals are applicable to
    104      *   any authentication scheme.
    105      */
    106     public AuthScope(final String host, int port,
    107         final String realm, final String scheme)
    108     {
    109         this.host =   (host == null)   ? ANY_HOST: host.toLowerCase(Locale.ENGLISH);
    110         this.port =   (port < 0)       ? ANY_PORT: port;
    111         this.realm =  (realm == null)  ? ANY_REALM: realm;
    112         this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase(Locale.ENGLISH);
    113     }
    114 
    115     /** Creates a new credentials scope for the given
    116      * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
    117      * authentication scheme.
    118      *
    119      * @param host the host the credentials apply to. May be set
    120      *   to <tt>null</tt> if credenticals are applicable to
    121      *   any host.
    122      * @param port the port the credentials apply to. May be set
    123      *   to negative value if credenticals are applicable to
    124      *   any port.
    125      * @param realm the realm the credentials apply to. May be set
    126      *   to <tt>null</tt> if credenticals are applicable to
    127      *   any realm.
    128      */
    129     public AuthScope(final String host, int port, final String realm) {
    130         this(host, port, realm, ANY_SCHEME);
    131     }
    132 
    133     /** Creates a new credentials scope for the given
    134      * <tt>host</tt>, <tt>port</tt>, any realm name, and any
    135      * authentication scheme.
    136      *
    137      * @param host the host the credentials apply to. May be set
    138      *   to <tt>null</tt> if credenticals are applicable to
    139      *   any host.
    140      * @param port the port the credentials apply to. May be set
    141      *   to negative value if credenticals are applicable to
    142      *   any port.
    143      */
    144     public AuthScope(final String host, int port) {
    145         this(host, port, ANY_REALM, ANY_SCHEME);
    146     }
    147 
    148     /**
    149      * Creates a copy of the given credentials scope.
    150      */
    151     public AuthScope(final AuthScope authscope) {
    152         super();
    153         if (authscope == null) {
    154             throw new IllegalArgumentException("Scope may not be null");
    155         }
    156         this.host = authscope.getHost();
    157         this.port = authscope.getPort();
    158         this.realm = authscope.getRealm();
    159         this.scheme = authscope.getScheme();
    160     }
    161 
    162     /**
    163      * @return the host
    164      */
    165     public String getHost() {
    166         return this.host;
    167     }
    168 
    169     /**
    170      * @return the port
    171      */
    172     public int getPort() {
    173         return this.port;
    174     }
    175 
    176     /**
    177      * @return the realm name
    178      */
    179     public String getRealm() {
    180         return this.realm;
    181     }
    182 
    183     /**
    184      * @return the scheme type
    185      */
    186     public String getScheme() {
    187         return this.scheme;
    188     }
    189 
    190     /**
    191      * Tests if the authentication scopes match.
    192      *
    193      * @return the match factor. Negative value signifies no match.
    194      *    Non-negative signifies a match. The greater the returned value
    195      *    the closer the match.
    196      */
    197     public int match(final AuthScope that) {
    198         int factor = 0;
    199         if (LangUtils.equals(this.scheme, that.scheme)) {
    200             factor += 1;
    201         } else {
    202             if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
    203                 return -1;
    204             }
    205         }
    206         if (LangUtils.equals(this.realm, that.realm)) {
    207             factor += 2;
    208         } else {
    209             if (this.realm != ANY_REALM && that.realm != ANY_REALM) {
    210                 return -1;
    211             }
    212         }
    213         if (this.port == that.port) {
    214             factor += 4;
    215         } else {
    216             if (this.port != ANY_PORT && that.port != ANY_PORT) {
    217                 return -1;
    218             }
    219         }
    220         if (LangUtils.equals(this.host, that.host)) {
    221             factor += 8;
    222         } else {
    223             if (this.host != ANY_HOST && that.host != ANY_HOST) {
    224                 return -1;
    225             }
    226         }
    227         return factor;
    228     }
    229 
    230     /**
    231      * @see java.lang.Object#equals(Object)
    232      */
    233     @Override
    234     public boolean equals(Object o) {
    235         if (o == null) {
    236             return false;
    237         }
    238         if (o == this) {
    239             return true;
    240         }
    241         if (!(o instanceof AuthScope)) {
    242             return super.equals(o);
    243         }
    244         AuthScope that = (AuthScope) o;
    245         return
    246         LangUtils.equals(this.host, that.host)
    247           && this.port == that.port
    248           && LangUtils.equals(this.realm, that.realm)
    249           && LangUtils.equals(this.scheme, that.scheme);
    250     }
    251 
    252     /**
    253      * @see java.lang.Object#toString()
    254      */
    255     @Override
    256     public String toString() {
    257         StringBuffer buffer = new StringBuffer();
    258         if (this.scheme != null) {
    259             buffer.append(this.scheme.toUpperCase(Locale.ENGLISH));
    260             buffer.append(' ');
    261         }
    262         if (this.realm != null) {
    263             buffer.append('\'');
    264             buffer.append(this.realm);
    265             buffer.append('\'');
    266         } else {
    267             buffer.append("<any realm>");
    268         }
    269         if (this.host != null) {
    270             buffer.append('@');
    271             buffer.append(this.host);
    272             if (this.port >= 0) {
    273                 buffer.append(':');
    274                 buffer.append(this.port);
    275             }
    276         }
    277         return buffer.toString();
    278     }
    279 
    280     /**
    281      * @see java.lang.Object#hashCode()
    282      */
    283     @Override
    284     public int hashCode() {
    285         int hash = LangUtils.HASH_SEED;
    286         hash = LangUtils.hashCode(hash, this.host);
    287         hash = LangUtils.hashCode(hash, this.port);
    288         hash = LangUtils.hashCode(hash, this.realm);
    289         hash = LangUtils.hashCode(hash, this.scheme);
    290         return hash;
    291     }
    292 }
    293