001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/AuthChallengeParser.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.HashMap;
034: import java.util.List;
035: import java.util.Map;
036:
037: import org.apache.commons.httpclient.Header;
038: import org.apache.commons.httpclient.NameValuePair;
039: import org.apache.commons.httpclient.util.ParameterParser;
040:
041: /**
042: * This class provides utility methods for parsing HTTP www and proxy authentication
043: * challenges.
044: *
045: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
046: *
047: * @since 2.0beta1
048: */
049: public final class AuthChallengeParser {
050: /**
051: * Extracts authentication scheme from the given authentication
052: * challenge.
053: *
054: * @param challengeStr the authentication challenge string
055: * @return authentication scheme
056: *
057: * @throws MalformedChallengeException when the authentication challenge string
058: * is malformed
059: *
060: * @since 2.0beta1
061: */
062: public static String extractScheme(final String challengeStr)
063: throws MalformedChallengeException {
064: if (challengeStr == null) {
065: throw new IllegalArgumentException(
066: "Challenge may not be null");
067: }
068: int idx = challengeStr.indexOf(' ');
069: String s = null;
070: if (idx == -1) {
071: s = challengeStr;
072: } else {
073: s = challengeStr.substring(0, idx);
074: }
075: if (s.equals("")) {
076: throw new MalformedChallengeException("Invalid challenge: "
077: + challengeStr);
078: }
079: return s.toLowerCase();
080: }
081:
082: /**
083: * Extracts a map of challenge parameters from an authentication challenge.
084: * Keys in the map are lower-cased
085: *
086: * @param challengeStr the authentication challenge string
087: * @return a map of authentication challenge parameters
088: * @throws MalformedChallengeException when the authentication challenge string
089: * is malformed
090: *
091: * @since 2.0beta1
092: */
093: public static Map extractParams(final String challengeStr)
094: throws MalformedChallengeException {
095: if (challengeStr == null) {
096: throw new IllegalArgumentException(
097: "Challenge may not be null");
098: }
099: int idx = challengeStr.indexOf(' ');
100: if (idx == -1) {
101: throw new MalformedChallengeException("Invalid challenge: "
102: + challengeStr);
103: }
104: Map map = new HashMap();
105: ParameterParser parser = new ParameterParser();
106: List params = parser.parse(challengeStr.substring(idx + 1,
107: challengeStr.length()), ',');
108: for (int i = 0; i < params.size(); i++) {
109: NameValuePair param = (NameValuePair) params.get(i);
110: map.put(param.getName().toLowerCase(), param.getValue());
111: }
112: return map;
113: }
114:
115: /**
116: * Extracts a map of challenges ordered by authentication scheme name
117: *
118: * @param headers the array of authorization challenges
119: * @return a map of authorization challenges
120: *
121: * @throws MalformedChallengeException if any of challenge strings
122: * is malformed
123: *
124: * @since 3.0
125: */
126: public static Map parseChallenges(final Header[] headers)
127: throws MalformedChallengeException {
128: if (headers == null) {
129: throw new IllegalArgumentException(
130: "Array of challenges may not be null");
131: }
132: String challenge = null;
133: Map challengemap = new HashMap(headers.length);
134: for (int i = 0; i < headers.length; i++) {
135: challenge = headers[i].getValue();
136: String s = AuthChallengeParser.extractScheme(challenge);
137: challengemap.put(s, challenge);
138: }
139: return challengemap;
140: }
141: }
|