View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/RFC2617Scheme.java,v 1.4.2.1 2004/02/22 18:21:15 olegk Exp $
3    * $Revision: 1.4.2.1 $
4    * $Date: 2004/02/22 18:21:15 $
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   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient.auth;
33  
34  import java.util.Map;
35  
36  /***
37   * <p>
38   * Abstract authentication scheme class that lays foundation for all
39   * RFC 2617 compliant authetication schemes and provides capabilities common 
40   * to all authentication schemes defined in RFC 2617.
41   * </p>
42   *
43   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
44  */
45  public abstract class RFC2617Scheme extends AuthSchemeBase {
46  
47      /***
48       * Authentication parameter map.
49       */
50      private Map params = null;
51  
52      /***
53       * Default constructor for RFC2617 compliant authetication schemes.
54       * 
55       * @param challenge authentication challenge
56       * 
57       * @throws MalformedChallengeException is thrown if the authentication challenge
58       * is malformed
59       */
60      public RFC2617Scheme(final String challenge) throws MalformedChallengeException {
61          super(challenge);
62          String s = AuthChallengeParser.extractScheme(challenge);
63          if (!s.equalsIgnoreCase(getSchemeName())) {
64              throw new MalformedChallengeException(
65                "Invalid " + getSchemeName() + " challenge: " + challenge); 
66          }
67          this.params = AuthChallengeParser.extractParams(challenge);
68      }
69  
70      /***
71       * Returns authentication parameters map. Keys in the map are lower-cased.
72       * 
73       * @return the map of authentication parameters
74       */
75      protected Map getParameters() {
76          return this.params;
77      }
78  
79      /***
80       * Returns authentication parameter with the given name, if available.
81       * 
82       * @param name The name of the parameter to be returned
83       * 
84       * @return the parameter with the given name
85       */
86      public String getParameter(String name) {
87          if (name == null) {
88              throw new IllegalArgumentException("Parameter name may not be null"); 
89          }
90          return (String) this.params.get(name.toLowerCase());
91      }
92  
93      /***
94       * Returns authentication realm. The realm may not be null.
95       * 
96       * @return the authentication realm
97       */
98      public String getRealm() {
99          return getParameter("realm");
100     }
101     
102     /***
103      * Returns a String identifying the authentication challenge.  This is
104      * used, in combination with the host and port to determine if
105      * authorization has already been attempted or not.  Schemes which
106      * require multiple requests to complete the authentication should
107      * return a different value for each stage in the request.
108      * 
109      * <p>Additionally, the ID should take into account any changes to the
110      * authentication challenge and return a different value when appropriate.
111      * For example when the realm changes in basic authentication it should be
112      * considered a different authentication attempt and a different value should
113      * be returned.</p>
114      * 
115      * <p>This method simply returns the realm for the challenge.</p>
116      * 
117      * @return String a String identifying the authentication challenge.  The
118      * returned value may be null.
119      */
120     public String getID() {
121         return getRealm();
122     }
123 }