You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2017/01/07 14:33:45 UTC

svn commit: r1777798 - in /maven/shared/trunk/maven-reporting-impl/src: main/java/org/apache/maven/reporting/UrlValidationUtil.java test/java/org/apache/maven/reporting/UrlValidationUtilTest.java

Author: michaelo
Date: Sat Jan  7 14:33:45 2017
New Revision: 1777798

URL: http://svn.apache.org/viewvc?rev=1777798&view=rev
Log:
Revert "[MSHARED-607] Simplify UrlValidationUtil to modern rules"

Modified:
    maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
    maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java

Modified: maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java?rev=1777798&r1=1777797&r2=1777798&view=diff
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java (original)
+++ maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java Sat Jan  7 14:33:45 2017
@@ -19,19 +19,32 @@ package org.apache.maven.reporting;
  * 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 @@ final class UrlValidationUtil
 
     private static UrlValidator configurePublicUrlValidator()
     {
-        return UrlValidator.getInstance();
+        return new UrlValidator( SCHEMES );
     }
 
     private static boolean isValidLocalUrl( final String url )
@@ -61,7 +74,14 @@ final class UrlValidationUtil
 
     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 );
     }
 
 }

Modified: maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java?rev=1777798&r1=1777797&r2=1777798&view=diff
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java (original)
+++ maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java Sat Jan  7 14:33:45 2017
@@ -21,13 +21,15 @@ package org.apache.maven.reporting;
 
 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 @@ public class UrlValidationUtilTest
         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 @@ public class UrlValidationUtilTest
 
     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 ) );
     }