001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/AuthChallengeProcessor.java,v 1.2 2004/04/18 23:51:36 jsdever 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.Collection;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: import org.apache.commons.httpclient.params.HttpParams;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * This class provides utility methods for processing HTTP www and proxy authentication
043: * challenges.
044: *
045: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
046: *
047: * @since 3.0
048: */
049: public final class AuthChallengeProcessor {
050:
051: private static final Log LOG = LogFactory
052: .getLog(AuthChallengeProcessor.class);
053:
054: private HttpParams params = null;
055:
056: /**
057: * Creates an authentication challenge processor with the given {@link HttpParams HTTP
058: * parameters}
059: *
060: * @param params the {@link HttpParams HTTP parameters} used by this processor
061: */
062: public AuthChallengeProcessor(final HttpParams params) {
063: super ();
064: if (params == null) {
065: throw new IllegalArgumentException(
066: "Parameter collection may not be null");
067: }
068: this .params = params;
069: }
070:
071: /**
072: * Determines the preferred {@link AuthScheme authentication scheme} that can be used
073: * to respond to the given collection of challenges.
074: *
075: * @param challenges the collection of authentication challenges
076: *
077: * @return the preferred {@link AuthScheme authentication scheme}
078: *
079: * @throws AuthChallengeException if the preferred authentication scheme
080: * cannot be determined or is not supported
081: */
082: public AuthScheme selectAuthScheme(final Map challenges)
083: throws AuthChallengeException {
084: if (challenges == null) {
085: throw new IllegalArgumentException(
086: "Challenge map may not be null");
087: }
088: Collection authPrefs = (Collection) this .params
089: .getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY);
090: if (authPrefs == null || authPrefs.isEmpty()) {
091: authPrefs = AuthPolicy.getDefaultAuthPrefs();
092: }
093: if (LOG.isDebugEnabled()) {
094: LOG
095: .debug("Supported authentication schemes in the order of preference: "
096: + authPrefs);
097: }
098: AuthScheme authscheme = null;
099: String challenge = null;
100: Iterator item = authPrefs.iterator();
101: while (item.hasNext()) {
102: String id = (String) item.next();
103: challenge = (String) challenges.get(id.toLowerCase());
104: if (challenge != null) {
105: if (LOG.isInfoEnabled()) {
106: LOG.info(id + " authentication scheme selected");
107: }
108: try {
109: authscheme = AuthPolicy.getAuthScheme(id);
110: } catch (IllegalStateException e) {
111: throw new AuthChallengeException(e.getMessage());
112: }
113: break;
114: } else {
115: if (LOG.isDebugEnabled()) {
116: LOG.debug("Challenge for " + id
117: + " authentication scheme not available");
118: // Try again
119: }
120: }
121: }
122: if (authscheme == null) {
123: // If none selected, something is wrong
124: throw new AuthChallengeException(
125: "Unable to respond to any of these challenges: "
126: + challenges);
127: }
128: return authscheme;
129: }
130:
131: /**
132: * Processes the given collection of challenges and updates the
133: * {@link AuthState state} of the authentication process.
134: *
135: * @param challenges the collection of authentication challenges
136: *
137: * @return the {@link AuthScheme authentication scheme} used to
138: * process the challenge
139: *
140: * @throws AuthChallengeException if authentication challenges cannot be
141: * successfully processed or the preferred authentication scheme cannot
142: * be determined
143: */
144: public AuthScheme processChallenge(final AuthState state,
145: final Map challenges) throws MalformedChallengeException,
146: AuthenticationException {
147: if (state == null) {
148: throw new IllegalArgumentException(
149: "Authentication state may not be null");
150: }
151: if (challenges == null) {
152: throw new IllegalArgumentException(
153: "Challenge map may not be null");
154: }
155:
156: if (state.isPreemptive() || state.getAuthScheme() == null) {
157: // Authentication not attempted before
158: state.setAuthScheme(selectAuthScheme(challenges));
159: }
160: AuthScheme authscheme = state.getAuthScheme();
161: String id = authscheme.getSchemeName();
162: if (LOG.isDebugEnabled()) {
163: LOG.debug("Using authentication scheme: " + id);
164: }
165: String challenge = (String) challenges.get(id.toLowerCase());
166: if (challenge == null) {
167: throw new AuthenticationException(
168: id
169: + " authorization challenge expected, but not found");
170: }
171: authscheme.processChallenge(challenge);
172: LOG.debug("Authorization challenge processed");
173: return authscheme;
174: }
175: }
|