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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/auth/AuthScope.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,289 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.auth;
    1.31 +
    1.32 +import java.util.Locale;
    1.33 +
    1.34 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.util.LangUtils;
    1.37 +
    1.38 +/**
    1.39 + * The class represents an authentication scope consisting of a host name,
    1.40 + * a port number, a realm name and an authentication scheme name which
    1.41 + * {@link Credentials Credentials} apply to.
    1.42 + *
    1.43 + *
    1.44 + * @since 4.0
    1.45 + */
    1.46 +@Immutable
    1.47 +public class AuthScope {
    1.48 +
    1.49 +    /**
    1.50 +     * The <tt>null</tt> value represents any host. In the future versions of
    1.51 +     * HttpClient the use of this parameter will be discontinued.
    1.52 +     */
    1.53 +    public static final String ANY_HOST = null;
    1.54 +
    1.55 +    /**
    1.56 +     * The <tt>-1</tt> value represents any port.
    1.57 +     */
    1.58 +    public static final int ANY_PORT = -1;
    1.59 +
    1.60 +    /**
    1.61 +     * The <tt>null</tt> value represents any realm.
    1.62 +     */
    1.63 +    public static final String ANY_REALM = null;
    1.64 +
    1.65 +    /**
    1.66 +     * The <tt>null</tt> value represents any authentication scheme.
    1.67 +     */
    1.68 +    public static final String ANY_SCHEME = null;
    1.69 +
    1.70 +    /**
    1.71 +     * Default scope matching any host, port, realm and authentication scheme.
    1.72 +     * In the future versions of HttpClient the use of this parameter will be
    1.73 +     * discontinued.
    1.74 +     */
    1.75 +    public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
    1.76 +
    1.77 +    /** The authentication scheme the credentials apply to. */
    1.78 +    private final String scheme;
    1.79 +
    1.80 +    /** The realm the credentials apply to. */
    1.81 +    private final String realm;
    1.82 +
    1.83 +    /** The host the credentials apply to. */
    1.84 +    private final String host;
    1.85 +
    1.86 +    /** The port the credentials apply to. */
    1.87 +    private final int port;
    1.88 +
    1.89 +    /** Creates a new credentials scope for the given
    1.90 +     * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and
    1.91 +     * <tt>authentication scheme</tt>.
    1.92 +     *
    1.93 +     * @param host the host the credentials apply to. May be set
    1.94 +     *   to <tt>null</tt> if credentials are applicable to
    1.95 +     *   any host.
    1.96 +     * @param port the port the credentials apply to. May be set
    1.97 +     *   to negative value if credentials are applicable to
    1.98 +     *   any port.
    1.99 +     * @param realm the realm the credentials apply to. May be set
   1.100 +     *   to <tt>null</tt> if credentials are applicable to
   1.101 +     *   any realm.
   1.102 +     * @param scheme the authentication scheme the credentials apply to.
   1.103 +     *   May be set to <tt>null</tt> if credentials are applicable to
   1.104 +     *   any authentication scheme.
   1.105 +     */
   1.106 +    public AuthScope(final String host, int port,
   1.107 +        final String realm, final String scheme)
   1.108 +    {
   1.109 +        this.host =   (host == null)   ? ANY_HOST: host.toLowerCase(Locale.ENGLISH);
   1.110 +        this.port =   (port < 0)       ? ANY_PORT: port;
   1.111 +        this.realm =  (realm == null)  ? ANY_REALM: realm;
   1.112 +        this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase(Locale.ENGLISH);
   1.113 +    }
   1.114 +
   1.115 +    /** Creates a new credentials scope for the given
   1.116 +     * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
   1.117 +     * authentication scheme.
   1.118 +     *
   1.119 +     * @param host the host the credentials apply to. May be set
   1.120 +     *   to <tt>null</tt> if credentials are applicable to
   1.121 +     *   any host.
   1.122 +     * @param port the port the credentials apply to. May be set
   1.123 +     *   to negative value if credentials are applicable to
   1.124 +     *   any port.
   1.125 +     * @param realm the realm the credentials apply to. May be set
   1.126 +     *   to <tt>null</tt> if credentials are applicable to
   1.127 +     *   any realm.
   1.128 +     */
   1.129 +    public AuthScope(final String host, int port, final String realm) {
   1.130 +        this(host, port, realm, ANY_SCHEME);
   1.131 +    }
   1.132 +
   1.133 +    /** Creates a new credentials scope for the given
   1.134 +     * <tt>host</tt>, <tt>port</tt>, any realm name, and any
   1.135 +     * authentication scheme.
   1.136 +     *
   1.137 +     * @param host the host the credentials apply to. May be set
   1.138 +     *   to <tt>null</tt> if credentials are applicable to
   1.139 +     *   any host.
   1.140 +     * @param port the port the credentials apply to. May be set
   1.141 +     *   to negative value if credentials are applicable to
   1.142 +     *   any port.
   1.143 +     */
   1.144 +    public AuthScope(final String host, int port) {
   1.145 +        this(host, port, ANY_REALM, ANY_SCHEME);
   1.146 +    }
   1.147 +
   1.148 +    /**
   1.149 +     * Creates a copy of the given credentials scope.
   1.150 +     */
   1.151 +    public AuthScope(final AuthScope authscope) {
   1.152 +        super();
   1.153 +        if (authscope == null) {
   1.154 +            throw new IllegalArgumentException("Scope may not be null");
   1.155 +        }
   1.156 +        this.host = authscope.getHost();
   1.157 +        this.port = authscope.getPort();
   1.158 +        this.realm = authscope.getRealm();
   1.159 +        this.scheme = authscope.getScheme();
   1.160 +    }
   1.161 +
   1.162 +    /**
   1.163 +     * @return the host
   1.164 +     */
   1.165 +    public String getHost() {
   1.166 +        return this.host;
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * @return the port
   1.171 +     */
   1.172 +    public int getPort() {
   1.173 +        return this.port;
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +     * @return the realm name
   1.178 +     */
   1.179 +    public String getRealm() {
   1.180 +        return this.realm;
   1.181 +    }
   1.182 +
   1.183 +    /**
   1.184 +     * @return the scheme type
   1.185 +     */
   1.186 +    public String getScheme() {
   1.187 +        return this.scheme;
   1.188 +    }
   1.189 +
   1.190 +    /**
   1.191 +     * Tests if the authentication scopes match.
   1.192 +     *
   1.193 +     * @return the match factor. Negative value signifies no match.
   1.194 +     *    Non-negative signifies a match. The greater the returned value
   1.195 +     *    the closer the match.
   1.196 +     */
   1.197 +    public int match(final AuthScope that) {
   1.198 +        int factor = 0;
   1.199 +        if (LangUtils.equals(this.scheme, that.scheme)) {
   1.200 +            factor += 1;
   1.201 +        } else {
   1.202 +            if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
   1.203 +                return -1;
   1.204 +            }
   1.205 +        }
   1.206 +        if (LangUtils.equals(this.realm, that.realm)) {
   1.207 +            factor += 2;
   1.208 +        } else {
   1.209 +            if (this.realm != ANY_REALM && that.realm != ANY_REALM) {
   1.210 +                return -1;
   1.211 +            }
   1.212 +        }
   1.213 +        if (this.port == that.port) {
   1.214 +            factor += 4;
   1.215 +        } else {
   1.216 +            if (this.port != ANY_PORT && that.port != ANY_PORT) {
   1.217 +                return -1;
   1.218 +            }
   1.219 +        }
   1.220 +        if (LangUtils.equals(this.host, that.host)) {
   1.221 +            factor += 8;
   1.222 +        } else {
   1.223 +            if (this.host != ANY_HOST && that.host != ANY_HOST) {
   1.224 +                return -1;
   1.225 +            }
   1.226 +        }
   1.227 +        return factor;
   1.228 +    }
   1.229 +
   1.230 +    /**
   1.231 +     * @see java.lang.Object#equals(Object)
   1.232 +     */
   1.233 +    @Override
   1.234 +    public boolean equals(Object o) {
   1.235 +        if (o == null) {
   1.236 +            return false;
   1.237 +        }
   1.238 +        if (o == this) {
   1.239 +            return true;
   1.240 +        }
   1.241 +        if (!(o instanceof AuthScope)) {
   1.242 +            return super.equals(o);
   1.243 +        }
   1.244 +        AuthScope that = (AuthScope) o;
   1.245 +        return
   1.246 +        LangUtils.equals(this.host, that.host)
   1.247 +          && this.port == that.port
   1.248 +          && LangUtils.equals(this.realm, that.realm)
   1.249 +          && LangUtils.equals(this.scheme, that.scheme);
   1.250 +    }
   1.251 +
   1.252 +    /**
   1.253 +     * @see java.lang.Object#toString()
   1.254 +     */
   1.255 +    @Override
   1.256 +    public String toString() {
   1.257 +        StringBuilder buffer = new StringBuilder();
   1.258 +        if (this.scheme != null) {
   1.259 +            buffer.append(this.scheme.toUpperCase(Locale.ENGLISH));
   1.260 +            buffer.append(' ');
   1.261 +        }
   1.262 +        if (this.realm != null) {
   1.263 +            buffer.append('\'');
   1.264 +            buffer.append(this.realm);
   1.265 +            buffer.append('\'');
   1.266 +        } else {
   1.267 +            buffer.append("<any realm>");
   1.268 +        }
   1.269 +        if (this.host != null) {
   1.270 +            buffer.append('@');
   1.271 +            buffer.append(this.host);
   1.272 +            if (this.port >= 0) {
   1.273 +                buffer.append(':');
   1.274 +                buffer.append(this.port);
   1.275 +            }
   1.276 +        }
   1.277 +        return buffer.toString();
   1.278 +    }
   1.279 +
   1.280 +    /**
   1.281 +     * @see java.lang.Object#hashCode()
   1.282 +     */
   1.283 +    @Override
   1.284 +    public int hashCode() {
   1.285 +        int hash = LangUtils.HASH_SEED;
   1.286 +        hash = LangUtils.hashCode(hash, this.host);
   1.287 +        hash = LangUtils.hashCode(hash, this.port);
   1.288 +        hash = LangUtils.hashCode(hash, this.realm);
   1.289 +        hash = LangUtils.hashCode(hash, this.scheme);
   1.290 +        return hash;
   1.291 +    }
   1.292 +}

mercurial