View Javadoc

1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient.auth;
31  
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  import org.apache.commons.httpclient.Header;
37  import org.apache.commons.httpclient.NameValuePair;
38  import org.apache.commons.httpclient.util.ParameterParser;
39  
40  /***
41   * This class provides utility methods for parsing HTTP www and proxy authentication 
42   * challenges.
43   * 
44   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
45   * 
46   * @since 2.0beta1
47   */
48  public final class AuthChallengeParser {
49      /*** 
50       * Extracts authentication scheme from the given authentication 
51       * challenge.
52       *
53       * @param challengeStr the authentication challenge string
54       * @return authentication scheme
55       * 
56       * @throws MalformedChallengeException when the authentication challenge string
57       *  is malformed
58       * 
59       * @since 2.0beta1
60       */
61      public static String extractScheme(final String challengeStr) 
62        throws MalformedChallengeException {
63          if (challengeStr == null) {
64              throw new IllegalArgumentException("Challenge may not be null"); 
65          }
66          int idx = challengeStr.indexOf(' ');
67          String s = null; 
68          if (idx == -1) {
69              s = challengeStr;
70          } else {
71              s = challengeStr.substring(0, idx);
72          }
73          if (s.equals("")) {
74              throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
75          }
76          return s.toLowerCase();
77      }
78  
79      /*** 
80       * Extracts a map of challenge parameters from an authentication challenge.
81       * Keys in the map are lower-cased
82       *
83       * @param challengeStr the authentication challenge string
84       * @return a map of authentication challenge parameters
85       * @throws MalformedChallengeException when the authentication challenge string
86       *  is malformed
87       * 
88       * @since 2.0beta1
89       */
90      public static Map extractParams(final String challengeStr)
91        throws MalformedChallengeException {
92          if (challengeStr == null) {
93              throw new IllegalArgumentException("Challenge may not be null"); 
94          }
95          int idx = challengeStr.indexOf(' ');
96          if (idx == -1) {
97              throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
98          }
99          Map map = new HashMap();
100         ParameterParser parser = new ParameterParser();
101         List params = parser.parse(
102             challengeStr.substring(idx + 1, challengeStr.length()), ',');
103         for (int i = 0; i < params.size(); i++) {
104             NameValuePair param = (NameValuePair) params.get(i);
105             map.put(param.getName().toLowerCase(), param.getValue());
106         }
107         return map;
108     }
109 
110     /*** 
111      * Extracts a map of challenges ordered by authentication scheme name
112      *
113      * @param headers the array of authorization challenges
114      * @return a map of authorization challenges
115      * 
116      * @throws MalformedChallengeException if any of challenge strings
117      *  is malformed
118      * 
119      * @since 3.0
120      */
121     public static Map parseChallenges(final Header[] headers)
122       throws MalformedChallengeException {
123         if (headers == null) {
124             throw new IllegalArgumentException("Array of challenges may not be null");
125         }
126         String challenge = null;
127         Map challengemap = new HashMap(headers.length); 
128         for (int i = 0; i < headers.length; i++) {
129             challenge = headers[i].getValue();
130             String s = AuthChallengeParser.extractScheme(challenge);
131             challengemap.put(s, challenge);
132         }
133         return challengemap;
134    }
135 }