You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2011/05/27 17:00:45 UTC

svn commit: r1128342 - in /commons/proper/validator/trunk/src: main/java/org/apache/commons/validator/routines/ test/java/org/apache/commons/validator/ test/java/org/apache/commons/validator/routines/

Author: nick
Date: Fri May 27 15:00:45 2011
New Revision: 1128342

URL: http://svn.apache.org/viewvc?rev=1128342&view=rev
Log:
VALIDATOR-292 - Allow the Email and Domain validators to optionally accept local host names

Modified:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/EmailValidator.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/EmailTest.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java?rev=1128342&r1=1128341&r2=1128342&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java (original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java Fri May 27 15:00:45 2011
@@ -68,10 +68,19 @@ public class DomainValidator implements 
     private static final String DOMAIN_NAME_REGEX =
             "^(?:" + DOMAIN_LABEL_REGEX + "\\.)+" + "(" + TOP_LABEL_REGEX + ")$";
 
+    private final boolean allowLocal;
+    
     /**
-     * Singleton instance of this validator.
+     * Singleton instance of this validator, which
+     *  doesn't consider local addresses as valid.
      */
-    private static final DomainValidator DOMAIN_VALIDATOR = new DomainValidator();
+    private static final DomainValidator DOMAIN_VALIDATOR = new DomainValidator(false);
+    
+    /**
+     * Singleton instance of this validator, which does
+     *  consider local addresses valid.
+     */
+    private static final DomainValidator DOMAIN_VALIDATOR_WITH_LOCAL = new DomainValidator(true);
 
     /**
      * RegexValidator for matching domains.
@@ -80,15 +89,30 @@ public class DomainValidator implements 
             new RegexValidator(DOMAIN_NAME_REGEX);
 
     /**
-     * Returns the singleton instance of this validator.
+     * Returns the singleton instance of this validator. It
+     *  will not consider local addresses as valid.
      * @return the singleton instance of this validator
      */
     public static DomainValidator getInstance() {
         return DOMAIN_VALIDATOR;
     }
+    
+    /**
+     * Returns the singleton instance of this validator,
+     *  with local validation as required.
+     * @param allowLocal Should local addresses be considered valid?
+     */
+    public static DomainValidator getInstance(boolean allowLocal) {
+       if(allowLocal) {
+          return DOMAIN_VALIDATOR_WITH_LOCAL;
+       }
+       return DOMAIN_VALIDATOR;
+    }
 
     /** Private constructor. */
-    private DomainValidator() {}
+    private DomainValidator(boolean allowLocal) {
+       this.allowLocal = allowLocal;
+    }
 
     /**
      * Returns true if the specified <code>String</code> parses
@@ -101,9 +125,12 @@ public class DomainValidator implements 
         String[] groups = domainRegex.match(domain);
         if (groups != null && groups.length > 0) {
             return isValidTld(groups[0]);
-        } else {
-            return false;
+        } else if(allowLocal) {
+            if ("localhost".equals(domain)) {
+               return true;
+            }
         }
+        return false;
     }
 
     /**
@@ -114,6 +141,9 @@ public class DomainValidator implements 
      * @return true if the parameter is a TLD
      */
     public boolean isValidTld(String tld) {
+        if(allowLocal && isValidLocalTld(tld)) {
+           return true;
+        }
         return isValidInfrastructureTld(tld)
                 || isValidGenericTld(tld)
                 || isValidCountryCodeTld(tld);
@@ -152,6 +182,17 @@ public class DomainValidator implements 
         return COUNTRY_CODE_TLD_LIST.contains(chompLeadingDot(ccTld.toLowerCase()));
     }
 
+    /**
+     * Returns true if the specified <code>String</code> matches any
+     * widely used "local" domains (localhost or localdomain). Leading dots are
+     *  ignored if present. The search is case-sensitive.
+     * @param iTld the parameter to check for local TLD status
+     * @return true if the parameter is an local TLD
+     */
+    public boolean isValidLocalTld(String iTld) {
+        return LOCAL_TLD_LIST.contains(chompLeadingDot(iTld.toLowerCase()));
+    }
+
     private String chompLeadingDot(String str) {
         if (str.startsWith(".")) {
             return str.substring(1);
@@ -445,7 +486,13 @@ public class DomainValidator implements 
         "zw",                 // Zimbabwe
     };
 
+    private static final String[] LOCAL_TLDS = new String[] {
+       "localhost",           // RFC2606 defined
+       "localdomain"          // Also widely used as localhost.localdomain
+   };
+
     private static final List INFRASTRUCTURE_TLD_LIST = Arrays.asList(INFRASTRUCTURE_TLDS);
     private static final List GENERIC_TLD_LIST = Arrays.asList(GENERIC_TLDS);
     private static final List COUNTRY_CODE_TLD_LIST = Arrays.asList(COUNTRY_CODE_TLDS);
+    private static final List LOCAL_TLD_LIST = Arrays.asList(LOCAL_TLDS);
 }

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/EmailValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/EmailValidator.java?rev=1128342&r1=1128341&r2=1128342&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/EmailValidator.java (original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/EmailValidator.java Fri May 27 15:00:45 2011
@@ -55,10 +55,19 @@ public class EmailValidator implements S
     private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile(IP_DOMAIN_REGEX);
     private static final Pattern USER_PATTERN = Pattern.compile(USER_REGEX);
 
+    private final boolean allowLocal;
+    
     /**
-     * Singleton instance of this class.
+     * Singleton instance of this class, which
+     *  doesn't consider local addresses as valid.
      */
-    private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();
+    private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(false);
+    
+    /**
+     * Singleton instance of this class, which does
+     *  consider local addresses valid.
+     */
+    private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL = new EmailValidator(true);
 
     /**
      * Returns the Singleton instance of this validator.
@@ -69,11 +78,24 @@ public class EmailValidator implements S
         return EMAIL_VALIDATOR;
     }
 
+    /**
+     * Returns the Singleton instance of this validator,
+     *  with local validation as required.
+     * @param allowLocal Should local addresses be considered valid?
+     */
+    public static EmailValidator getInstance(boolean allowLocal) {
+        if(allowLocal) {
+           return EMAIL_VALIDATOR_WITH_LOCAL;
+        }
+        return EMAIL_VALIDATOR;
+    }
+
     /**                                       
      * Protected constructor for subclasses to use.
      */
-    protected EmailValidator() {
+    protected EmailValidator(boolean allowLocal) {
         super();
+        this.allowLocal = allowLocal;
     }
 
     /**
@@ -131,7 +153,7 @@ public class EmailValidator implements S
         } else {
             // Domain is symbolic name
             DomainValidator domainValidator =
-                    DomainValidator.getInstance();
+                    DomainValidator.getInstance(allowLocal);
             return domainValidator.isValid(domain);
         }
     }

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/EmailTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/EmailTest.java?rev=1128342&r1=1128341&r2=1128342&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/EmailTest.java (original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/EmailTest.java Fri May 27 15:00:45 2011
@@ -211,6 +211,18 @@ public class EmailTest extends AbstractC
     }
 
     /**
+     * Test that @localhost and @localhost.localdomain
+     *  addresses aren't declared valid by default 
+     */
+    public void testEmailLocalhost() throws ValidatorException {
+       ValueBean info = new ValueBean();
+       info.setValue("joe@localhost");
+       valueTest(info, false);
+       info.setValue("joe@localhost.localdomain");
+       valueTest(info, false);
+    }
+
+    /**
      * Write this test according to parts of RFC, as opposed to the type of character
      * that is being tested.
      *

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java?rev=1128342&r1=1128341&r2=1128342&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java (original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java Fri May 27 15:00:45 2011
@@ -84,4 +84,24 @@ public class DomainValidatorTest extends
         assertFalse("empty string shouldn't validate as TLD", validator.isValid(""));
         assertFalse("null shouldn't validate as TLD", validator.isValid(null));
     }
+    
+    public void testAllowLocal() {
+       DomainValidator noLocal = DomainValidator.getInstance(false);
+       DomainValidator allowLocal = DomainValidator.getInstance(true);
+       
+       // Default is false, and should use singletons
+       assertEquals(noLocal, validator);
+       
+       // Default won't allow local
+       assertFalse("localhost.localdomain should validate", noLocal.isValid("localhost.localdomain"));
+       assertFalse("localhost should validate", noLocal.isValid("localhost"));
+       
+       // But it may be requested
+       assertTrue("localhost.localdomain should validate", allowLocal.isValid("localhost.localdomain"));
+       assertTrue("localhost should validate", allowLocal.isValid("localhost"));
+       
+       // Check the localhost one with a few others
+       assertTrue("apache.org should validate", allowLocal.isValid("apache.org"));
+       assertFalse("domain name with spaces shouldn't validate", allowLocal.isValid(" apache.org "));
+    }
 }

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java?rev=1128342&r1=1128341&r2=1128342&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java (original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java Fri May 27 15:00:45 2011
@@ -19,6 +19,7 @@ package org.apache.commons.validator.rou
 import junit.framework.TestCase;
 
 import org.apache.commons.validator.ResultPair;
+import org.apache.commons.validator.ValidatorException;
 
 /**
  * Performs Validation Test for e-mail validations.
@@ -168,6 +169,36 @@ public class EmailValidatorTest extends 
         }
         assertFalse("Test control char 127", validator.isValid("foo" + ((char)127) + "bar@domain.com"));
     }
+    
+    /**
+     * Test that @localhost and @localhost.localdomain
+     *  addresses are declared as valid when requested. 
+     */
+    public void testEmailLocalhost() throws ValidatorException {
+       // Check the default is not to allow
+       EmailValidator noLocal = EmailValidator.getInstance(false);
+       EmailValidator allowLocal = EmailValidator.getInstance(true);
+       assertEquals(validator, noLocal);
+       
+       // Depends on the validator
+       assertTrue(
+             "@localhost.localdomain should be accepted but wasn't",
+             allowLocal.isValid("joe@localhost.localdomain")
+       );
+       assertTrue(
+             "@localhost should be accepted but wasn't",
+             allowLocal.isValid("joe@localhost")
+       );
+       
+       assertFalse(
+             "@localhost.localdomain should be accepted but wasn't",
+             noLocal.isValid("joe@localhost.localdomain")
+       );
+       assertFalse(
+             "@localhost should be accepted but wasn't",
+             noLocal.isValid("joe@localhost")
+       );
+    }
 
     /**
      * Write this test according to parts of RFC, as opposed to the type of character