001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/AuthScope.java,v 1.2 2004/06/23 06:50:25 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.auth;
032:
033: import org.apache.commons.httpclient.util.LangUtils;
034:
035: /**
036: * The class represents an authentication scope consisting of a host name,
037: * a port number, a realm name and an authentication scheme name which
038: * {@link org.apache.commons.httpclient.Credentials} apply to.
039: *
040: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
041: * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
042: *
043: * @since 3.0
044: */
045: public class AuthScope {
046:
047: /**
048: * The <tt>null</tt> value represents any host. In the future versions of
049: * HttpClient the use of this parameter will be discontinued.
050: */
051: public static final String ANY_HOST = null;
052:
053: /**
054: * The <tt>-1</tt> value represents any port.
055: */
056: public static final int ANY_PORT = -1;
057:
058: /**
059: * The <tt>null</tt> value represents any realm.
060: */
061: public static final String ANY_REALM = null;
062:
063: /**
064: * The <tt>null</tt> value represents any authentication scheme.
065: */
066: public static final String ANY_SCHEME = null;
067:
068: /**
069: * Default scope matching any host, port, realm and authentication scheme.
070: * In the future versions of HttpClient the use of this parameter will be
071: * discontinued.
072: */
073: public static final AuthScope ANY = new AuthScope(ANY_HOST,
074: ANY_PORT, ANY_REALM, ANY_SCHEME);
075:
076: /** The authentication scheme the credentials apply to. */
077: private String scheme = null;
078:
079: /** The realm the credentials apply to. */
080: private String realm = null;
081:
082: /** The host the credentials apply to. */
083: private String host = null;
084:
085: /** The port the credentials apply to. */
086: private int port = -1;
087:
088: /** Creates a new credentials scope for the given
089: * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and
090: * <tt>authentication scheme</tt>.
091: *
092: * @param host the host the credentials apply to. May be set
093: * to <tt>null</tt> if credenticals are applicable to
094: * any host.
095: * @param port the port the credentials apply to. May be set
096: * to negative value if credenticals are applicable to
097: * any port.
098: * @param realm the realm the credentials apply to. May be set
099: * to <tt>null</tt> if credenticals are applicable to
100: * any realm.
101: * @param scheme the authentication scheme the credentials apply to.
102: * May be set to <tt>null</tt> if credenticals are applicable to
103: * any authentication scheme.
104: *
105: * @since 3.0
106: */
107: public AuthScope(final String host, int port, final String realm,
108: final String scheme) {
109: this .host = (host == null) ? ANY_HOST : host.toLowerCase();
110: this .port = (port < 0) ? ANY_PORT : port;
111: this .realm = (realm == null) ? ANY_REALM : realm;
112: this .scheme = (scheme == null) ? ANY_SCHEME : scheme
113: .toUpperCase();
114: ;
115: }
116:
117: /** Creates a new credentials scope for the given
118: * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
119: * authentication scheme.
120: *
121: * @param host the host the credentials apply to. May be set
122: * to <tt>null</tt> if credenticals are applicable to
123: * any host.
124: * @param port the port the credentials apply to. May be set
125: * to negative value if credenticals are applicable to
126: * any port.
127: * @param realm the realm the credentials apply to. May be set
128: * to <tt>null</tt> if credenticals are applicable to
129: * any realm.
130: *
131: * @since 3.0
132: */
133: public AuthScope(final String host, int port, final String realm) {
134: this (host, port, realm, ANY_SCHEME);
135: }
136:
137: /** Creates a new credentials scope for the given
138: * <tt>host</tt>, <tt>port</tt>, any realm name, and any
139: * authentication scheme.
140: *
141: * @param host the host the credentials apply to. May be set
142: * to <tt>null</tt> if credenticals are applicable to
143: * any host.
144: * @param port the port the credentials apply to. May be set
145: * to negative value if credenticals are applicable to
146: * any port.
147: *
148: * @since 3.0
149: */
150: public AuthScope(final String host, int port) {
151: this (host, port, ANY_REALM, ANY_SCHEME);
152: }
153:
154: /**
155: * Creates a copy of the given credentials scope.
156: *
157: * @since 3.0
158: */
159: public AuthScope(final AuthScope authscope) {
160: super ();
161: if (authscope == null) {
162: throw new IllegalArgumentException("Scope may not be null");
163: }
164: this .host = authscope.getHost();
165: this .port = authscope.getPort();
166: this .realm = authscope.getRealm();
167: this .scheme = authscope.getScheme();
168: }
169:
170: /**
171: * @return the host
172: *
173: * @since 3.0
174: */
175: public String getHost() {
176: return this .host;
177: }
178:
179: /**
180: * @return the port
181: *
182: * @since 3.0
183: */
184: public int getPort() {
185: return this .port;
186: }
187:
188: /**
189: * @return the realm name
190: *
191: * @since 3.0
192: */
193: public String getRealm() {
194: return this .realm;
195: }
196:
197: /**
198: * @return the scheme type
199: *
200: * @since 3.0
201: */
202: public String getScheme() {
203: return this .scheme;
204: }
205:
206: /** Determines if the given parameters are equal.
207: *
208: * @param p1 the parameter
209: * @param p2 the other parameter
210: * @return boolean true if the parameters are equal, otherwise false.
211: */
212: private static boolean paramsEqual(final String p1, final String p2) {
213: if (p1 == null) {
214: return p1 == p2;
215: } else {
216: return p1.equals(p2);
217: }
218: }
219:
220: /** Determines if the given parameters are equal.
221: *
222: * @param p1 the parameter
223: * @param p2 the other parameter
224: * @return boolean true if the parameters are equal, otherwise false.
225: */
226: private static boolean paramsEqual(int p1, int p2) {
227: return p1 == p2;
228: }
229:
230: /**
231: * Tests if the authentication scopes match.
232: *
233: * @return the match factor. Negative value signifies no match.
234: * Non-negative signifies a match. The greater the returned value
235: * the closer the match.
236: *
237: * @since 3.0
238: */
239: public int match(final AuthScope that) {
240: int factor = 0;
241: if (paramsEqual(this .scheme, that.scheme)) {
242: factor += 1;
243: } else {
244: if (this .scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
245: return -1;
246: }
247: }
248: if (paramsEqual(this .realm, that.realm)) {
249: factor += 2;
250: } else {
251: if (this .realm != ANY_REALM && that.realm != ANY_REALM) {
252: return -1;
253: }
254: }
255: if (paramsEqual(this .port, that.port)) {
256: factor += 4;
257: } else {
258: if (this .port != ANY_PORT && that.port != ANY_PORT) {
259: return -1;
260: }
261: }
262: if (paramsEqual(this .host, that.host)) {
263: factor += 8;
264: } else {
265: if (this .host != ANY_HOST && that.host != ANY_HOST) {
266: return -1;
267: }
268: }
269: return factor;
270: }
271:
272: /**
273: * @see java.lang.Object#equals(Object)
274: */
275: public boolean equals(Object o) {
276: if (o == null) {
277: return false;
278: }
279: if (o == this ) {
280: return true;
281: }
282: if (!(o instanceof AuthScope)) {
283: return super .equals(o);
284: }
285: AuthScope that = (AuthScope) o;
286: return paramsEqual(this .host, that.host)
287: && paramsEqual(this .port, that.port)
288: && paramsEqual(this .realm, that.realm)
289: && paramsEqual(this .scheme, that.scheme);
290: }
291:
292: /**
293: * @see java.lang.Object#toString()
294: */
295: public String toString() {
296: StringBuffer buffer = new StringBuffer();
297: if (this .scheme != null) {
298: buffer.append(this .scheme.toUpperCase());
299: buffer.append(' ');
300: }
301: if (this .realm != null) {
302: buffer.append('\'');
303: buffer.append(this .realm);
304: buffer.append('\'');
305: } else {
306: buffer.append("<any realm>");
307: }
308: if (this .host != null) {
309: buffer.append('@');
310: buffer.append(this .host);
311: if (this .port >= 0) {
312: buffer.append(':');
313: buffer.append(this .port);
314: }
315: }
316: return buffer.toString();
317: }
318:
319: /**
320: * @see java.lang.Object#hashCode()
321: */
322: public int hashCode() {
323: int hash = LangUtils.HASH_SEED;
324: hash = LangUtils.hashCode(hash, this.host);
325: hash = LangUtils.hashCode(hash, this.port);
326: hash = LangUtils.hashCode(hash, this.realm);
327: hash = LangUtils.hashCode(hash, this.scheme);
328: return hash;
329: }
330: }
|