View Javadoc

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