1. /*
  2. * $Header: /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: 1.10 $
  4. * $Date: 2004/05/13 04:02:00 $
  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. package org.apache.commons.httpclient.auth;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import org.apache.commons.httpclient.Header;
  34. import org.apache.commons.httpclient.NameValuePair;
  35. import org.apache.commons.httpclient.util.ParameterParser;
  36. /**
  37. * This class provides utility methods for parsing HTTP www and proxy authentication
  38. * challenges.
  39. *
  40. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  41. *
  42. * @since 2.0beta1
  43. */
  44. public final class AuthChallengeParser {
  45. /**
  46. * Extracts authentication scheme from the given authentication
  47. * challenge.
  48. *
  49. * @param challengeStr the authentication challenge string
  50. * @return authentication scheme
  51. *
  52. * @throws MalformedChallengeException when the authentication challenge string
  53. * is malformed
  54. *
  55. * @since 2.0beta1
  56. */
  57. public static String extractScheme(final String challengeStr)
  58. throws MalformedChallengeException {
  59. if (challengeStr == null) {
  60. throw new IllegalArgumentException("Challenge may not be null");
  61. }
  62. int idx = challengeStr.indexOf(' ');
  63. String s = null;
  64. if (idx == -1) {
  65. s = challengeStr;
  66. } else {
  67. s = challengeStr.substring(0, idx);
  68. }
  69. if (s.equals("")) {
  70. throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
  71. }
  72. return s.toLowerCase();
  73. }
  74. /**
  75. * Extracts a map of challenge parameters from an authentication challenge.
  76. * Keys in the map are lower-cased
  77. *
  78. * @param challengeStr the authentication challenge string
  79. * @return a map of authentication challenge parameters
  80. * @throws MalformedChallengeException when the authentication challenge string
  81. * is malformed
  82. *
  83. * @since 2.0beta1
  84. */
  85. public static Map extractParams(final String challengeStr)
  86. throws MalformedChallengeException {
  87. if (challengeStr == null) {
  88. throw new IllegalArgumentException("Challenge may not be null");
  89. }
  90. int idx = challengeStr.indexOf(' ');
  91. if (idx == -1) {
  92. throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
  93. }
  94. Map map = new HashMap();
  95. ParameterParser parser = new ParameterParser();
  96. List params = parser.parse(
  97. challengeStr.substring(idx + 1, challengeStr.length()), ',');
  98. for (int i = 0; i < params.size(); i++) {
  99. NameValuePair param = (NameValuePair) params.get(i);
  100. map.put(param.getName().toLowerCase(), param.getValue());
  101. }
  102. return map;
  103. }
  104. /**
  105. * Extracts a map of challenges ordered by authentication scheme name
  106. *
  107. * @param headers the array of authorization challenges
  108. * @return a map of authorization challenges
  109. *
  110. * @throws MalformedChallengeException if any of challenge strings
  111. * is malformed
  112. *
  113. * @since 3.0
  114. */
  115. public static Map parseChallenges(final Header[] headers)
  116. throws MalformedChallengeException {
  117. if (headers == null) {
  118. throw new IllegalArgumentException("Array of challenges may not be null");
  119. }
  120. String challenge = null;
  121. Map challengemap = new HashMap(headers.length);
  122. for (int i = 0; i < headers.length; i++) {
  123. challenge = headers[i].getValue();
  124. String s = AuthChallengeParser.extractScheme(challenge);
  125. challengemap.put(s, challenge);
  126. }
  127. return challengemap;
  128. }
  129. }