You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/11/24 23:17:02 UTC

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

olegk       2003/11/24 14:17:02

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        NTCredentials.java UsernamePasswordCredentials.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestAuthenticator.java
  Log:
  changelog:
  
  - UserNamePasswordCredentials & NTCredentials made immutable (setter methods have been deprecated)
  - UserNamePasswordCredentials & NTCredentials validate input parameters and throw IllegalArgumentExceptions in case of username, host or domain (applicable to NTCredentials only) being null
  
  Contributed by Oleg Kalnichevski
  Reviewed By Michael Becke & Ortwin Glueck
  
  Revision  Changes    Path
  1.8       +29 -9     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/NTCredentials.java
  
  Index: NTCredentials.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/NTCredentials.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- NTCredentials.java	16 Aug 2003 00:41:24 -0000	1.7
  +++ NTCredentials.java	24 Nov 2003 22:17:02 -0000	1.8
  @@ -88,6 +88,8 @@
   
       /**
        * Default constructor.
  +     * 
  +     * @deprecated Do not use. Null user name, domain & host no longer allowed
        */
       public NTCredentials() {
           super();
  @@ -105,21 +107,31 @@
       public NTCredentials(String userName, String password, String host,
               String domain) {
           super(userName, password);
  +        if (domain == null) {
  +            throw new IllegalArgumentException("Domain may not be null");
  +        }
           this.domain = domain;
  +        if (host == null) {
  +            throw new IllegalArgumentException("Host may not be null");
  +        }
           this.host = host;
       }
       // ------------------------------------------------------- Instance Methods
   
   
       /**
  -     * Sets the domain to authenticate with.
  +     * Sets the domain to authenticate with. The domain may not be null.
        *
        * @param domain the NT domain to authenticate in.
        * 
        * @see #getDomain()
        * 
  +     * @deprecated Do not use. The NTCredentials objects should be immutable
        */
       public void setDomain(String domain) {
  +        if (domain == null) {
  +            throw new IllegalArgumentException("Domain may not be null");
  +        }
           this.domain = domain;
       }
   
  @@ -135,11 +147,18 @@
           return domain;
       }
   
  -    /** Sets the host name of the computer originating the request.
  +    /** 
  +     * Sets the host name of the computer originating the request. The host name may
  +     * not be null.
        *
        * @param host the Host the user is logged into.
  +     * 
  +     * @deprecated Do not use. The NTCredentials objects should be immutable
        */
       public void setHost(String host) {
  +        if (host == null) {
  +            throw new IllegalArgumentException("Host may not be null");
  +        }
           this.host = host;
       }
   
  @@ -159,9 +178,10 @@
       public String toString() {
           final StringBuffer sbResult = new StringBuffer(super.toString());
           
  -        sbResult.append(":");
  -        sbResult.append(this.host == null ? "null" : this.host);
  -        sbResult.append(this.domain == null ? "null" : this.domain);
  +        sbResult.append("@");
  +        sbResult.append(this.host);
  +        sbResult.append(".");
  +        sbResult.append(this.domain);
   
           return sbResult.toString();
       }
  
  
  
  1.12      +25 -6     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java
  
  Index: UsernamePasswordCredentials.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- UsernamePasswordCredentials.java	31 Jan 2003 00:33:36 -0000	1.11
  +++ UsernamePasswordCredentials.java	24 Nov 2003 22:17:02 -0000	1.12
  @@ -69,6 +69,7 @@
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
    * @author Sean C. Sullivan
    * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  + * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    * 
    * @version $Revision$ $Date$
    * 
  @@ -79,8 +80,11 @@
   
       /**
        * Default constructor.
  +     * 
  +     * @deprecated Do not use. Null user name no longer allowed
        */
       public UsernamePasswordCredentials() {
  +        super();
       }
   
   
  @@ -91,6 +95,10 @@
        * @see #toString
        */
       public UsernamePasswordCredentials(String usernamePassword) {
  +        super();
  +        if (usernamePassword == null) {
  +            throw new IllegalArgumentException("Username:password string may not be null");            
  +        }
           int atColon = usernamePassword.indexOf(':');
           if (atColon >= 0) {
               this.userName = usernamePassword.substring(0, atColon);
  @@ -108,6 +116,10 @@
        * @param password the password
        */
       public UsernamePasswordCredentials(String userName, String password) {
  +        super();
  +        if (userName == null) {
  +            throw new IllegalArgumentException("Username may not be null");            
  +        }
           this.userName = userName;
           this.password = password;
       }
  @@ -130,12 +142,17 @@
   
   
       /**
  -     * User name property setter.
  +     * User name property setter. User name may not be null.
        *
        * @param userName
        * @see #getUserName()
  +     * 
  +     * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
        */
       public void setUserName(String userName) {
  +        if (userName == null) {
  +            throw new IllegalArgumentException("Username may not be null");            
  +        }
           this.userName = userName;
       }
   
  @@ -156,6 +173,8 @@
        *
        * @param password
        * @see #getPassword()
  +     * 
  +     * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
        */
       public void setPassword(String password) {
           this.password = password;
  @@ -180,7 +199,7 @@
        */
       public String toString() {
           StringBuffer result = new StringBuffer();
  -        result.append((this.userName == null) ? "null" : this.userName);
  +        result.append(this.userName);
           result.append(":");
           result.append((this.password == null) ? "null" : this.password);
           return result.toString();
  
  
  
  1.34      +31 -4     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java
  
  Index: TestAuthenticator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- TestAuthenticator.java	14 Nov 2003 02:28:49 -0000	1.33
  +++ TestAuthenticator.java	24 Nov 2003 22:17:02 -0000	1.34
  @@ -99,6 +99,33 @@
   
       // ---------------------------------- Test Methods for BasicScheme Authentication
   
  +    public void testCredentialConstructors() {
  +        try {
  +            new UsernamePasswordCredentials(null, null);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +        try {
  +            new NTCredentials("user", "password", null, null);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +        try {
  +            new NTCredentials("user", "password", "host", null);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +        NTCredentials creds = new NTCredentials("user", null, "host", "domain");
  +        assertNotNull(creds.getUserName());
  +        assertNull(creds.getPassword());
  +        assertNotNull(creds.getDomain());
  +        assertNotNull(creds.getHost());
  +    }
  +
  +
       public void testBasicAuthenticationWithNoCreds() {
           String challenge = "Basic realm=\"realm1\"";
           HttpState state = new HttpState();
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org