Revert "[MSHARED-607] Simplify UrlValidationUtil to modern rules"


git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1777798 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java b/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
index ded4c1b..ca73afe 100644
--- a/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
+++ b/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
@@ -19,19 +19,32 @@
  * under the License.
  */
 
+import org.apache.commons.validator.routines.RegexValidator;
 import org.apache.commons.validator.routines.UrlValidator;
 
 /**
  * Static utility class intended to help {@link AbstractMavenReportRenderer} in validating URLs. Validation uses two
  * UrlValidator instances. The first validates public URLs, the second validates local URLs. At least one validator has
  * to accept the given URL. A URL is called local if it uses an unqualified hostname (such as "localhost" or
- * "workstation-12") or fully-qualified domain names.
+ * "workstation-12") or qualified domain names within the special use top level domain ".local".
  *
  * @author <a href="mailto:jan.schultze@gmail.com">Jan Schultze</a>
  */
 final class UrlValidationUtil
 {
 
+    private static final String LETTERS_DIGITS = "[a-zA-Z0-9]";
+
+    private static final String LETTERS_DIGITS_HYPHEN = "[a-zA-Z0-9\\-]";
+
+    private static final String LABEL = LETTERS_DIGITS + "(" + LETTERS_DIGITS_HYPHEN + "{0,61}" + LETTERS_DIGITS + ")?";
+
+    private static final String OPTIONAL_PORT = "(:(([1-5]\\d{1,4})|([1-9]\\d{1,3})))?";
+
+    private static final String AUTHORITY_REGEX = LABEL + "(\\." + LABEL + ")*\\.local\\.?" + OPTIONAL_PORT;
+
+    private static final String[] SCHEMES = { "http", "https" };
+
     private UrlValidationUtil()
     {
         throw new RuntimeException( "Instantiation of " + UrlValidationUtil.class.getName() + " is not allowed." );
@@ -50,7 +63,7 @@
 
     private static UrlValidator configurePublicUrlValidator()
     {
-        return UrlValidator.getInstance();
+        return new UrlValidator( SCHEMES );
     }
 
     private static boolean isValidLocalUrl( final String url )
@@ -61,7 +74,14 @@
 
     private static UrlValidator configureLocalUrlValidator()
     {
-        return new UrlValidator( UrlValidator.ALLOW_LOCAL_URLS );
+        RegexValidator authorityValidator = configureLocalAuthorityValidator();
+        return new UrlValidator( SCHEMES, authorityValidator, UrlValidator.ALLOW_LOCAL_URLS );
+    }
+
+    /* package-private for testing purposes */
+    static RegexValidator configureLocalAuthorityValidator()
+    {
+        return new RegexValidator( AUTHORITY_REGEX, false );
     }
 
 }
diff --git a/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java b/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
index 3466a61..604ba0f 100644
--- a/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
+++ b/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
@@ -21,13 +21,15 @@
 
 import junit.framework.TestCase;
 
+import org.apache.commons.validator.routines.RegexValidator;
+
 public class UrlValidationUtilTest
     extends TestCase
 {
 
     public void testUrlWithPortIsAccepted()
     {
-        testUrlIsAccepted( "http://host.organization.com:8080/something" );
+        testUrlIsAccepted( "http://host.organization.local:8080/something" );
     }
 
     public void testUrlWithLocalhostIsAccepted()
@@ -80,114 +82,111 @@
         assertFalse( UrlValidationUtil.isValidUrl( string ) );
     }
 
-    public void testAuthorityHostDotCompanyDotLocalIsRejected()
+    public void testAuthorityHostDotCompanyDotLocalIsAccepted()
     {
-        testAuthorityIsRejected( "host.organization.local" );
+        testAuthorityIsAccepted( "host.organization.local" );
     }
 
-    public void testAuthorityHostDotLocalIsRejected()
+    public void testAuthorityHostDotLocalIsAccepted()
     {
-        testAuthorityIsRejected( "host.local" );
+        testAuthorityIsAccepted( "host.local" );
     }
 
     public void testAuthorityWithStandardHttpPortIsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.com:80" );
+        testAuthorityIsAccepted( "host.organization.local:80" );
     }
 
     public void testAuthorityWithStandardHttpsPortIsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.com:443" );
+        testAuthorityIsAccepted( "host.organization.local:443" );
     }
 
     public void testAuthorityWithPort8080IsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.com:8080" );
+        testAuthorityIsAccepted( "host.organization.local:8080" );
     }
 
     public void testAuthorityWithPortHighPortIsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.com:55555" );
+        testAuthorityIsAccepted( "host.organization.local:55555" );
     }
 
     public void testAuthorityWithPort59999IsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.com:59999" );
+        testAuthorityIsAccepted( "host.organization.local:59999" );
     }
 
-    public void testAuthorityWithPort60000IsAccepted()
+    public void testAuthorityWithPort60000IsRejected()
     {
-        testAuthorityIsAccepted( "host.organization.com:60000" );
+        testAuthorityRejects( "host.organization.local:60000" );
     }
 
     public void testAuthorityWithPort6000IsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.com:6000" );
+        testAuthorityIsAccepted( "host.organization.local:6000" );
     }
 
-    // This is a bug in Commons Validator VALIDATOR-411
     public void testAuthorityWithPort100000IsRejected()
     {
-        //testAuthorityIsRejected( "host.organization.com:100000" );
+        testAuthorityRejects( "host.organization.local:100000" );
     }
 
-    public void testAuthorityWithLeadingZeroInPortIsAccepted()
+    public void testAuthorityWithLeadingZeroInPortIsRejected()
     {
-        // Though this looks awkward, RFC 3986, Section 3.2.3 says
-        // "port = *DIGIT" whereas digit is 0 to 9
-        testAuthorityIsAccepted( "host.organization.com:080" );
+        testAuthorityRejects( "host.organization.local:080" );
     }
 
     public void testAuthorityWithTrainlingDotIsAccepted()
     {
-        testAuthorityIsAccepted( "host.com." );
+        testAuthorityIsAccepted( "host.local." );
     }
 
     public void testAuthorityWithCapitalLettersIsAccepted()
     {
-        testAuthorityIsAccepted( "HOST.oRGaNiZAtION.cOm" );
+        testAuthorityIsAccepted( "HOST.oRGaNiZAtION.LOcaL" );
     }
 
-    public void testAuthorityIPIsAccepted()
+    public void testAuthorityIPIsRejected()
     {
-        testAuthorityIsAccepted( "1.2.3.4" );
+        testAuthorityRejects( "1.2.3.4" );
     }
 
     public void testAuthorityWithLeadingDotIsRejected()
     {
-        testAuthorityIsRejected( ".host.organization.com" );
+        testAuthorityRejects( ".host.organization.local" );
     }
 
-    public void testAuthorityOnlyConsistingOfLocalIsAccepted()
+    public void testAuthorityOnlyConsistingOfLocalIsRejected()
     {
-        testAuthorityIsAccepted( "local" );
+        testAuthorityRejects( "local" );
     }
 
     public void testAuthorityWithEmptySubDomainIsRejected()
     {
-        testAuthorityIsRejected( "host..com" );
+        testAuthorityRejects( "host..local" );
     }
 
-    public void testAuthorityWithNonLocalDomainIsAccepted()
+    public void testAuthorityWithNonLocalDomainIsRejected()
     {
-        testAuthorityIsAccepted( "www.example.org" );
+        testAuthorityRejects( "www.example.org" );
     }
 
     public void testAuthorityWithLeadingHyphenIsRejected()
     {
-        testAuthorityIsRejected( "host.-organization.com" );
+        testAuthorityRejects( "host.-organization.local" );
     }
 
     public void testAuthorityWithTrailingHyphenIsRejected()
     {
-        testAuthorityIsRejected( "host.organization-.com" );
+        testAuthorityRejects( "host.organization-.local" );
     }
 
     public void testAuthorityWithTooLongSubDomainIsRejected()
     {
         String tooLongDomainName = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffabcd";
         assertTrue( tooLongDomainName.length() == 64 );
-        testAuthorityIsRejected( "host." + tooLongDomainName + ".com" );
+        testAuthorityRejects( "host." + tooLongDomainName + ".local" );
     }
 
     private void testAuthorityIsAccepted( final String input )
@@ -197,10 +196,11 @@
 
     private boolean isValidAuthority( final String input )
     {
-        return UrlValidationUtil.isValidUrl( "http://" + input );
+        RegexValidator authority = UrlValidationUtil.configureLocalAuthorityValidator();
+        return authority.isValid( input );
     }
 
-    private void testAuthorityIsRejected( final String input )
+    private void testAuthorityRejects( final String input )
     {
         assertFalse( isValidAuthority( input ) );
     }