michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.auth; michael@0: michael@0: import java.util.Locale; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.util.LangUtils; michael@0: michael@0: /** michael@0: * The class represents an authentication scope consisting of a host name, michael@0: * a port number, a realm name and an authentication scheme name which michael@0: * {@link Credentials Credentials} apply to. michael@0: * michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class AuthScope { michael@0: michael@0: /** michael@0: * The null value represents any host. In the future versions of michael@0: * HttpClient the use of this parameter will be discontinued. michael@0: */ michael@0: public static final String ANY_HOST = null; michael@0: michael@0: /** michael@0: * The -1 value represents any port. michael@0: */ michael@0: public static final int ANY_PORT = -1; michael@0: michael@0: /** michael@0: * The null value represents any realm. michael@0: */ michael@0: public static final String ANY_REALM = null; michael@0: michael@0: /** michael@0: * The null value represents any authentication scheme. michael@0: */ michael@0: public static final String ANY_SCHEME = null; michael@0: michael@0: /** michael@0: * Default scope matching any host, port, realm and authentication scheme. michael@0: * In the future versions of HttpClient the use of this parameter will be michael@0: * discontinued. michael@0: */ michael@0: public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME); michael@0: michael@0: /** The authentication scheme the credentials apply to. */ michael@0: private final String scheme; michael@0: michael@0: /** The realm the credentials apply to. */ michael@0: private final String realm; michael@0: michael@0: /** The host the credentials apply to. */ michael@0: private final String host; michael@0: michael@0: /** The port the credentials apply to. */ michael@0: private final int port; michael@0: michael@0: /** Creates a new credentials scope for the given michael@0: * host, port, realm, and michael@0: * authentication scheme. michael@0: * michael@0: * @param host the host the credentials apply to. May be set michael@0: * to null if credentials are applicable to michael@0: * any host. michael@0: * @param port the port the credentials apply to. May be set michael@0: * to negative value if credentials are applicable to michael@0: * any port. michael@0: * @param realm the realm the credentials apply to. May be set michael@0: * to null if credentials are applicable to michael@0: * any realm. michael@0: * @param scheme the authentication scheme the credentials apply to. michael@0: * May be set to null if credentials are applicable to michael@0: * any authentication scheme. michael@0: */ michael@0: public AuthScope(final String host, int port, michael@0: final String realm, final String scheme) michael@0: { michael@0: this.host = (host == null) ? ANY_HOST: host.toLowerCase(Locale.ENGLISH); michael@0: this.port = (port < 0) ? ANY_PORT: port; michael@0: this.realm = (realm == null) ? ANY_REALM: realm; michael@0: this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase(Locale.ENGLISH); michael@0: } michael@0: michael@0: /** Creates a new credentials scope for the given michael@0: * host, port, realm, and any michael@0: * authentication scheme. michael@0: * michael@0: * @param host the host the credentials apply to. May be set michael@0: * to null if credentials are applicable to michael@0: * any host. michael@0: * @param port the port the credentials apply to. May be set michael@0: * to negative value if credentials are applicable to michael@0: * any port. michael@0: * @param realm the realm the credentials apply to. May be set michael@0: * to null if credentials are applicable to michael@0: * any realm. michael@0: */ michael@0: public AuthScope(final String host, int port, final String realm) { michael@0: this(host, port, realm, ANY_SCHEME); michael@0: } michael@0: michael@0: /** Creates a new credentials scope for the given michael@0: * host, port, any realm name, and any michael@0: * authentication scheme. michael@0: * michael@0: * @param host the host the credentials apply to. May be set michael@0: * to null if credentials are applicable to michael@0: * any host. michael@0: * @param port the port the credentials apply to. May be set michael@0: * to negative value if credentials are applicable to michael@0: * any port. michael@0: */ michael@0: public AuthScope(final String host, int port) { michael@0: this(host, port, ANY_REALM, ANY_SCHEME); michael@0: } michael@0: michael@0: /** michael@0: * Creates a copy of the given credentials scope. michael@0: */ michael@0: public AuthScope(final AuthScope authscope) { michael@0: super(); michael@0: if (authscope == null) { michael@0: throw new IllegalArgumentException("Scope may not be null"); michael@0: } michael@0: this.host = authscope.getHost(); michael@0: this.port = authscope.getPort(); michael@0: this.realm = authscope.getRealm(); michael@0: this.scheme = authscope.getScheme(); michael@0: } michael@0: michael@0: /** michael@0: * @return the host michael@0: */ michael@0: public String getHost() { michael@0: return this.host; michael@0: } michael@0: michael@0: /** michael@0: * @return the port michael@0: */ michael@0: public int getPort() { michael@0: return this.port; michael@0: } michael@0: michael@0: /** michael@0: * @return the realm name michael@0: */ michael@0: public String getRealm() { michael@0: return this.realm; michael@0: } michael@0: michael@0: /** michael@0: * @return the scheme type michael@0: */ michael@0: public String getScheme() { michael@0: return this.scheme; michael@0: } michael@0: michael@0: /** michael@0: * Tests if the authentication scopes match. michael@0: * michael@0: * @return the match factor. Negative value signifies no match. michael@0: * Non-negative signifies a match. The greater the returned value michael@0: * the closer the match. michael@0: */ michael@0: public int match(final AuthScope that) { michael@0: int factor = 0; michael@0: if (LangUtils.equals(this.scheme, that.scheme)) { michael@0: factor += 1; michael@0: } else { michael@0: if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) { michael@0: return -1; michael@0: } michael@0: } michael@0: if (LangUtils.equals(this.realm, that.realm)) { michael@0: factor += 2; michael@0: } else { michael@0: if (this.realm != ANY_REALM && that.realm != ANY_REALM) { michael@0: return -1; michael@0: } michael@0: } michael@0: if (this.port == that.port) { michael@0: factor += 4; michael@0: } else { michael@0: if (this.port != ANY_PORT && that.port != ANY_PORT) { michael@0: return -1; michael@0: } michael@0: } michael@0: if (LangUtils.equals(this.host, that.host)) { michael@0: factor += 8; michael@0: } else { michael@0: if (this.host != ANY_HOST && that.host != ANY_HOST) { michael@0: return -1; michael@0: } michael@0: } michael@0: return factor; michael@0: } michael@0: michael@0: /** michael@0: * @see java.lang.Object#equals(Object) michael@0: */ michael@0: @Override michael@0: public boolean equals(Object o) { michael@0: if (o == null) { michael@0: return false; michael@0: } michael@0: if (o == this) { michael@0: return true; michael@0: } michael@0: if (!(o instanceof AuthScope)) { michael@0: return super.equals(o); michael@0: } michael@0: AuthScope that = (AuthScope) o; michael@0: return michael@0: LangUtils.equals(this.host, that.host) michael@0: && this.port == that.port michael@0: && LangUtils.equals(this.realm, that.realm) michael@0: && LangUtils.equals(this.scheme, that.scheme); michael@0: } michael@0: michael@0: /** michael@0: * @see java.lang.Object#toString() michael@0: */ michael@0: @Override michael@0: public String toString() { michael@0: StringBuilder buffer = new StringBuilder(); michael@0: if (this.scheme != null) { michael@0: buffer.append(this.scheme.toUpperCase(Locale.ENGLISH)); michael@0: buffer.append(' '); michael@0: } michael@0: if (this.realm != null) { michael@0: buffer.append('\''); michael@0: buffer.append(this.realm); michael@0: buffer.append('\''); michael@0: } else { michael@0: buffer.append(""); michael@0: } michael@0: if (this.host != null) { michael@0: buffer.append('@'); michael@0: buffer.append(this.host); michael@0: if (this.port >= 0) { michael@0: buffer.append(':'); michael@0: buffer.append(this.port); michael@0: } michael@0: } michael@0: return buffer.toString(); michael@0: } michael@0: michael@0: /** michael@0: * @see java.lang.Object#hashCode() michael@0: */ michael@0: @Override michael@0: public int hashCode() { michael@0: int hash = LangUtils.HASH_SEED; michael@0: hash = LangUtils.hashCode(hash, this.host); michael@0: hash = LangUtils.hashCode(hash, this.port); michael@0: hash = LangUtils.hashCode(hash, this.realm); michael@0: hash = LangUtils.hashCode(hash, this.scheme); michael@0: return hash; michael@0: } michael@0: }