You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by su...@apache.org on 2002/09/14 15:36:35 UTC

cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient Authenticator.java

sullis      2002/09/14 06:36:35

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        Authenticator.java
  Log:
  use the java.util.Map interface (where appropriate)
  added javadoc comments
  
  
  Revision  Changes    Path
  1.27      +54 -47    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java
  
  Index: Authenticator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- Authenticator.java	1 Sep 2002 01:27:37 -0000	1.26
  +++ Authenticator.java	14 Sep 2002 13:36:35 -0000	1.27
  @@ -62,6 +62,7 @@
   package org.apache.commons.httpclient;
   
   import java.security.MessageDigest;
  +import java.util.Map;
   import java.util.Hashtable;
   import java.util.StringTokenizer;
   
  @@ -94,6 +95,8 @@
    * @author Rodney Waldhoff
    * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
    * @author Ortwin Gl�ck
  + * @author Sean C. Sullivan
  + * 
    */
   class Authenticator {
       //~ Static variables/initializers ������������������������������������������
  @@ -141,7 +144,7 @@
        * 
        * @param uname Username
        * @param pwd Password
  -     * @param dCreds Hashtable containing necessary header parameters to
  +     * @param mapCreds map containing necessary header parameters to
        *        construct the digest. It must/can contain: uri, realm, nonce,
        *        cnonce, qop, nc.
        * 
  @@ -152,21 +155,21 @@
        * @todo + Add createDigest() method 
        */
       public static String createDigest(String uname, String pwd, 
  -                                      Hashtable dCreds)
  +                                      Map mapCreds)
                                  throws HttpException {
           log.trace("enter Authenticator.createDigest(String, String, "
  -            + "Hashtable)");
  +            + "Map)");
   
           final String digAlg = "MD5";
   
           // Collecting required tokens
  -        String uri = removeQuotes((String) dCreds.get("uri"));
  -        String realm = removeQuotes((String) dCreds.get("realm"));
  -        String nonce = removeQuotes((String) dCreds.get("nonce"));
  -        String nc = removeQuotes((String) dCreds.get("nc"));
  -        String cnonce = removeQuotes((String) dCreds.get("cnonce"));
  -        String qop = removeQuotes((String) dCreds.get("qop"));
  -        String method = (String) dCreds.get("methodname");
  +        String uri = removeQuotes((String) mapCreds.get("uri"));
  +        String realm = removeQuotes((String) mapCreds.get("realm"));
  +        String nonce = removeQuotes((String) mapCreds.get("nonce"));
  +        String nc = removeQuotes((String) mapCreds.get("nc"));
  +        String cnonce = removeQuotes((String) mapCreds.get("cnonce"));
  +        String qop = removeQuotes((String) mapCreds.get("qop"));
  +        String method = (String) mapCreds.get("methodname");
   
           if (qop != null) {
               qop = "auth";
  @@ -259,7 +262,7 @@
        * Return a Basic <tt>Authorization</tt> header value for the given {@link
        * UsernamePasswordCredentials}.
        * 
  -     * @param cred the credentials to encode
  +     * @param cred the credentials to encode. Must be non-null
        * 
        * @return the credentials as a Basic Authentication string
        */
  @@ -359,7 +362,7 @@
               throw new HttpException("No credentials available for the Digest "
                   + "authentication realm \"" + realm + "\"/");
           } else {
  -            Hashtable headers = getHTTPDigestCredentials(method, proxy);
  +            Map headers = getHTTPDigestCredentials(method, proxy);
               headers.put("cnonce", "\"" + createCnonce() + "\"");
               headers.put("nc", "00000001");
               headers.put("uri", method.getPath());
  @@ -378,16 +381,18 @@
        * @return a string containing the authorization header for digest
        * @throws HttpException When a recoverable error occurs
        */
  -    static String digest(UsernamePasswordCredentials cred, Hashtable headers)
  +    static String digest(UsernamePasswordCredentials cred, 
  +    			Map mapHeaders)
                     throws HttpException {
           log.trace("enter Authenticator.digest(UsernamePasswordCredentials, "
  -                  + "Hashtable)");
  +                  + "Map)");
   
           String digest = createDigest(cred.getUserName(), cred.getPassword(), 
  -                                     headers);
  +                                     mapHeaders);
   
           return "Digest "
  -               + createDigestHeader(cred.getUserName(), headers, digest);
  +               + createDigestHeader(cred.getUserName(), 
  +               				mapHeaders, digest);
       }
   
       /**
  @@ -397,15 +402,16 @@
        * @param method The HTTP method.
        * @param proxy true if authorizing for a proxy
        * 
  -     * @return The parameters from the authenticate header as a Hashtable or
  -     *         empty Hashtable if there is no valid authorization.
  +     * @return The parameters from the authenticate header as a Map or
  +     *         an empty Map if there is no valid authorization.
        * 
        * @since 2.0
  -     * @see #processDigestToken(String,java.util.Hashtable)
  +     * @see #processDigestToken(String, java.util.Map)
        * @see PROXY_AUTH
        * @see WWW_AUTH
  +     * 
        */
  -    private static Hashtable getHTTPDigestCredentials(HttpMethod method, 
  +    private static Map getHTTPDigestCredentials(HttpMethod method, 
                                                         boolean proxy) {
           log.trace("enter Authenticator.getHTTPDigestCredentials(HttpMethod, "
               + "boolean)");
  @@ -419,27 +425,27 @@
               authHeader = method.getResponseHeader(authName).getValue();
               authHeader = authHeader.substring(7).trim();
           } catch (NullPointerException npe) {
  -            return new Hashtable(0);
  +            return (Map) (new java.util.Hashtable(0));
           }
   
  -        //Hashtable of digest tokens
  -        Hashtable ht = new Hashtable(17);
  +        // map of digest tokens
  +        Map mapTokens = new Hashtable(17);
   
           //parse the authenticate header
           int i = 0;
           int j = authHeader.indexOf(",");
   
           while (j >= 0) {
  -            processDigestToken(authHeader.substring(i, j), ht);
  +            processDigestToken(authHeader.substring(i, j), mapTokens);
               i = j + 1;
               j = authHeader.indexOf(",", i);
           }
   
           if (i < authHeader.length()) {
  -            processDigestToken(authHeader.substring(i), ht);
  +            processDigestToken(authHeader.substring(i), mapTokens);
           }
   
  -        return ht;
  +        return mapTokens;
       }
   
       /**
  @@ -526,7 +532,7 @@
           //FIXME: This fails if the contents of a challenge contains a ','
           StringTokenizer challengeTok = new StringTokenizer(authenticateValue, 
                                                              ",");
  -        Hashtable challengeMap = new Hashtable(7);
  +        Map challengeMap = new Hashtable(7);
   
           while (challengeTok.hasMoreTokens()) {
               // Parse the authentication scheme from the challenge
  @@ -607,7 +613,7 @@
        * string in the HTTP Authorization header (digest-response in RFC2617).
        * 
        * @param uname Username
  -     * @param dCreds Hashtable containing header information (uri, realm,
  +     * @param mapCreds Map containing header information (uri, realm,
        *        nonce, nc, cnonce, opaque, qop).
        * @param digest The response tag's value as String.
        * 
  @@ -615,20 +621,21 @@
        * 
        * @todo + Add createDigestHeader() method 
        */
  -    private static String createDigestHeader(String uname, Hashtable dCreds, 
  -                                             String digest) {
  -        log.trace("enter Authenticator.createDigestHeader(String, Hashtable, "
  +    private static String createDigestHeader(String uname, 
  +    					Map mapCreds, 
  +						String digest) {
  +        log.trace("enter Authenticator.createDigestHeader(String, Map, "
               + "String)");
   
           StringBuffer sb = new StringBuffer();
  -        String uri = removeQuotes((String) dCreds.get("uri"));
  -        String realm = removeQuotes((String) dCreds.get("realm"));
  -        String nonce = removeQuotes((String) dCreds.get("nonce"));
  -        String nc = removeQuotes((String) dCreds.get("nc"));
  -        String cnonce = removeQuotes((String) dCreds.get("cnonce"));
  -        String opaque = removeQuotes((String) dCreds.get("opaque"));
  +        String uri = removeQuotes((String) mapCreds.get("uri"));
  +        String realm = removeQuotes((String) mapCreds.get("realm"));
  +        String nonce = removeQuotes((String) mapCreds.get("nonce"));
  +        String nc = removeQuotes((String) mapCreds.get("nc"));
  +        String cnonce = removeQuotes((String) mapCreds.get("cnonce"));
  +        String opaque = removeQuotes((String) mapCreds.get("opaque"));
           String response = digest;
  -        String qop = removeQuotes((String) dCreds.get("qop"));
  +        String qop = removeQuotes((String) mapCreds.get("qop"));
   
           if (qop != null) {
               qop = "auth"; //we only support auth
  @@ -718,21 +725,21 @@
       /**
        * Takes an entry of <CODE>"xxx=yyy"</CODE> format, partitions into a key
        * and a value (key will be the left side, value will be the right side of
  -     * the equal sign) and places that into a <CODE>Hashtable</CODE>.
  +     * the equal sign) and places that into a <CODE>Map</CODE>.
        * 
        * @param token the entry to be processed
  -     * @param ht the <CODE>java.util.Hashtable</CODE> into which the processed
  +     * @param tokens the <CODE>java.util.Map</CODE> into which the processed
        *        entry is placed (only if it has <CODE>"xxx=yyy"</CODE> format).
        * 
        * @todo + Add processDigestToken() method 
        */
  -    private static void processDigestToken(String token, Hashtable ht) {
  -        log.trace("enter Authenticator.processDigestToken(String, Hashtable)");
  +    private static void processDigestToken(String token, Map tokens) {
  +        log.trace("enter Authenticator.processDigestToken(String, Map)");
   
           int eqpos = token.indexOf("=");
   
           if ((eqpos > 0) && (eqpos < (token.length() - 1))) {
  -            ht.put(token.substring(0, eqpos).trim(), 
  +            tokens.put(token.substring(0, eqpos).trim(), 
                      token.substring(eqpos + 1).trim());
           }
       }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>