mobile/android/thirdparty/ch/boye/httpclientandroidlib/auth/AuthScope.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial