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