1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/RFC2617Scheme.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.Map;
  31. /**
  32. * <p>
  33. * Abstract authentication scheme class that lays foundation for all
  34. * RFC 2617 compliant authetication schemes and provides capabilities common
  35. * to all authentication schemes defined in RFC 2617.
  36. * </p>
  37. *
  38. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  39. */
  40. public abstract class RFC2617Scheme implements AuthScheme {
  41. /**
  42. * Authentication parameter map.
  43. */
  44. private Map params = null;
  45. /**
  46. * Default constructor for RFC2617 compliant authetication schemes.
  47. *
  48. * @since 3.0
  49. */
  50. public RFC2617Scheme() {
  51. super();
  52. }
  53. /**
  54. * Default constructor for RFC2617 compliant authetication schemes.
  55. *
  56. * @param challenge authentication challenge
  57. *
  58. * @throws MalformedChallengeException is thrown if the authentication challenge
  59. * is malformed
  60. *
  61. * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)}
  62. * method
  63. */
  64. public RFC2617Scheme(final String challenge) throws MalformedChallengeException {
  65. super();
  66. processChallenge(challenge);
  67. }
  68. /**
  69. * Processes the given challenge token. Some authentication schemes
  70. * may involve multiple challenge-response exchanges. Such schemes must be able
  71. * to maintain the state information when dealing with sequential challenges
  72. *
  73. * @param challenge the challenge string
  74. *
  75. * @throws MalformedChallengeException is thrown if the authentication challenge
  76. * is malformed
  77. *
  78. * @since 3.0
  79. */
  80. public void processChallenge(final String challenge) throws MalformedChallengeException {
  81. String s = AuthChallengeParser.extractScheme(challenge);
  82. if (!s.equalsIgnoreCase(getSchemeName())) {
  83. throw new MalformedChallengeException(
  84. "Invalid " + getSchemeName() + " challenge: " + challenge);
  85. }
  86. this.params = AuthChallengeParser.extractParams(challenge);
  87. }
  88. /**
  89. * Returns authentication parameters map. Keys in the map are lower-cased.
  90. *
  91. * @return the map of authentication parameters
  92. */
  93. protected Map getParameters() {
  94. return this.params;
  95. }
  96. /**
  97. * Returns authentication parameter with the given name, if available.
  98. *
  99. * @param name The name of the parameter to be returned
  100. *
  101. * @return the parameter with the given name
  102. */
  103. public String getParameter(String name) {
  104. if (name == null) {
  105. throw new IllegalArgumentException("Parameter name may not be null");
  106. }
  107. if (this.params == null) {
  108. return null;
  109. }
  110. return (String) this.params.get(name.toLowerCase());
  111. }
  112. /**
  113. * Returns authentication realm. The realm may not be null.
  114. *
  115. * @return the authentication realm
  116. */
  117. public String getRealm() {
  118. return getParameter("realm");
  119. }
  120. /**
  121. * Returns a String identifying the authentication challenge. This is
  122. * used, in combination with the host and port to determine if
  123. * authorization has already been attempted or not. Schemes which
  124. * require multiple requests to complete the authentication should
  125. * return a different value for each stage in the request.
  126. *
  127. * <p>Additionally, the ID should take into account any changes to the
  128. * authentication challenge and return a different value when appropriate.
  129. * For example when the realm changes in basic authentication it should be
  130. * considered a different authentication attempt and a different value should
  131. * be returned.</p>
  132. *
  133. * <p>This method simply returns the realm for the challenge.</p>
  134. *
  135. * @return String a String identifying the authentication challenge. The
  136. * returned value may be null.
  137. *
  138. * @deprecated no longer used
  139. */
  140. public String getID() {
  141. return getRealm();
  142. }
  143. }