001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/RFC2617Scheme.java,v 1.10 2004/05/13 04:02:00 mbecke 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 java.util.Map;
034:
035: /**
036: * <p>
037: * Abstract authentication scheme class that lays foundation for all
038: * RFC 2617 compliant authetication schemes and provides capabilities common
039: * to all authentication schemes defined in RFC 2617.
040: * </p>
041: *
042: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
043: */
044: public abstract class RFC2617Scheme implements AuthScheme {
045:
046: /**
047: * Authentication parameter map.
048: */
049: private Map params = null;
050:
051: /**
052: * Default constructor for RFC2617 compliant authetication schemes.
053: *
054: * @since 3.0
055: */
056: public RFC2617Scheme() {
057: super ();
058: }
059:
060: /**
061: * Default constructor for RFC2617 compliant authetication schemes.
062: *
063: * @param challenge authentication challenge
064: *
065: * @throws MalformedChallengeException is thrown if the authentication challenge
066: * is malformed
067: *
068: * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)}
069: * method
070: */
071: public RFC2617Scheme(final String challenge)
072: throws MalformedChallengeException {
073: super ();
074: processChallenge(challenge);
075: }
076:
077: /**
078: * Processes the given challenge token. Some authentication schemes
079: * may involve multiple challenge-response exchanges. Such schemes must be able
080: * to maintain the state information when dealing with sequential challenges
081: *
082: * @param challenge the challenge string
083: *
084: * @throws MalformedChallengeException is thrown if the authentication challenge
085: * is malformed
086: *
087: * @since 3.0
088: */
089: public void processChallenge(final String challenge)
090: throws MalformedChallengeException {
091: String s = AuthChallengeParser.extractScheme(challenge);
092: if (!s.equalsIgnoreCase(getSchemeName())) {
093: throw new MalformedChallengeException("Invalid "
094: + getSchemeName() + " challenge: " + challenge);
095: }
096: this .params = AuthChallengeParser.extractParams(challenge);
097: }
098:
099: /**
100: * Returns authentication parameters map. Keys in the map are lower-cased.
101: *
102: * @return the map of authentication parameters
103: */
104: protected Map getParameters() {
105: return this .params;
106: }
107:
108: /**
109: * Returns authentication parameter with the given name, if available.
110: *
111: * @param name The name of the parameter to be returned
112: *
113: * @return the parameter with the given name
114: */
115: public String getParameter(String name) {
116: if (name == null) {
117: throw new IllegalArgumentException(
118: "Parameter name may not be null");
119: }
120: if (this .params == null) {
121: return null;
122: }
123: return (String) this .params.get(name.toLowerCase());
124: }
125:
126: /**
127: * Returns authentication realm. The realm may not be null.
128: *
129: * @return the authentication realm
130: */
131: public String getRealm() {
132: return getParameter("realm");
133: }
134:
135: /**
136: * Returns a String identifying the authentication challenge. This is
137: * used, in combination with the host and port to determine if
138: * authorization has already been attempted or not. Schemes which
139: * require multiple requests to complete the authentication should
140: * return a different value for each stage in the request.
141: *
142: * <p>Additionally, the ID should take into account any changes to the
143: * authentication challenge and return a different value when appropriate.
144: * For example when the realm changes in basic authentication it should be
145: * considered a different authentication attempt and a different value should
146: * be returned.</p>
147: *
148: * <p>This method simply returns the realm for the challenge.</p>
149: *
150: * @return String a String identifying the authentication challenge. The
151: * returned value may be null.
152: *
153: * @deprecated no longer used
154: */
155: public String getID() {
156: return getRealm();
157: }
158: }
|