You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2015/06/25 22:32:44 UTC

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

Author: hboutemy
Date: Thu Jun 25 20:32:44 2015
New Revision: 1687636

URL: http://svn.apache.org/r1687636
Log:
[MSHARED-429] accept plain hostnames and hosts within the special use top level domain ".local"
Submitted by: Jan Schultze

Added:
    maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java   (with props)
    maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java   (with props)
Modified:
    maven/shared/trunk/maven-reporting-impl/pom.xml
    maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java

Modified: maven/shared/trunk/maven-reporting-impl/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-reporting-impl/pom.xml?rev=1687636&r1=1687635&r2=1687636&view=diff
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/pom.xml (original)
+++ maven/shared/trunk/maven-reporting-impl/pom.xml Thu Jun 25 20:32:44 2015
@@ -117,7 +117,7 @@
     <dependency>
       <groupId>commons-validator</groupId>
       <artifactId>commons-validator</artifactId>
-      <version>1.3.1</version>
+      <version>1.4.1</version>
     </dependency>
 
     <!-- test -->

Modified: maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java?rev=1687636&r1=1687635&r2=1687636&view=diff
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java (original)
+++ maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java Thu Jun 25 20:32:44 2015
@@ -19,8 +19,7 @@ package org.apache.maven.reporting;
  * under the License.
  */
 
-import org.apache.commons.validator.EmailValidator;
-import org.apache.commons.validator.UrlValidator;
+import org.apache.commons.validator.routines.EmailValidator;
 
 import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.doxia.util.HtmlTools;
@@ -618,8 +617,6 @@ public abstract class AbstractMavenRepor
 
         href = href.trim();
 
-        String[] schemes = {"http", "https"};
-        UrlValidator urlValidator = new UrlValidator( schemes );
         EmailValidator emailValidator = EmailValidator.getInstance();
 
         if ( emailValidator.isValid( href )
@@ -631,7 +628,7 @@ public abstract class AbstractMavenRepor
         {
             return href;
         }
-        else if ( urlValidator.isValid( href ) )
+        else if ( UrlValidationUtil.isValidUrl( href ) )
         {
             return href;
         }
@@ -647,7 +644,7 @@ public abstract class AbstractMavenRepor
                 hrefTmp = href + "index.html";
             }
 
-            if ( urlValidator.isValid( hrefTmp ) )
+            if ( UrlValidationUtil.isValidUrl( hrefTmp ) )
             {
                 return href;
             }

Added: 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=1687636&view=auto
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java (added)
+++ maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java Thu Jun 25 20:32:44 2015
@@ -0,0 +1,88 @@
+package org.apache.maven.reporting;
+
+import org.apache.commons.validator.routines.RegexValidator;
+import org.apache.commons.validator.routines.UrlValidator;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * 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 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 String LETTERS_DIGITS = "[a-zA-Z0-9]";
+
+    private static String LETTERS_DIGITS_HYPHEN = "[a-zA-Z0-9\\-]";
+
+    private static String LABEL = LETTERS_DIGITS + "(" + LETTERS_DIGITS_HYPHEN + "{0,61}" + LETTERS_DIGITS + ")?";
+
+    private static 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 final static String[] SCHEMES = { "http", "https" };
+
+    private UrlValidationUtil()
+    {
+        throw new RuntimeException( "Instantiation of " + UrlValidationUtil.class.getName() + " is not allowed." );
+    }
+
+    static boolean isValidUrl( final String url )
+    {
+        return isValidPublicUrl( url ) || isValidLocalUrl( url );
+    }
+
+    private static boolean isValidPublicUrl( final String url )
+    {
+        UrlValidator validator = configurePublicUrlValidator();
+        return validator.isValid( url );
+    }
+
+    private static UrlValidator configurePublicUrlValidator()
+    {
+        return new UrlValidator( SCHEMES );
+    }
+
+    private static boolean isValidLocalUrl( final String url )
+    {
+        UrlValidator validator = configureLocalUrlValidator();
+        return validator.isValid( url );
+    }
+
+    private static UrlValidator configureLocalUrlValidator()
+    {
+        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 );
+    }
+
+}

Propchange: maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 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=1687636&view=auto
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java (added)
+++ maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java Thu Jun 25 20:32:44 2015
@@ -0,0 +1,208 @@
+package org.apache.maven.reporting;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+
+import org.apache.commons.validator.routines.RegexValidator;
+
+public class UrlValidationUtilTest
+    extends TestCase
+{
+
+    public void testUrlWithPortIsAccepted()
+    {
+        testUrlIsAccepted( "http://host.organization.local:8080/something" );
+    }
+
+    public void testUrlWithLocalhostIsAccepted()
+    {
+        testUrlIsAccepted( "http://localhost/something" );
+    }
+
+    public void testUrlWithLocalhostAndPortIsAccepted()
+    {
+        testUrlIsAccepted( "http://localhost:8080/something" );
+    }
+
+    public void testUrlWithIpIsAccepted()
+    {
+        testUrlIsAccepted( "http://1.2.3.4/something" );
+    }
+
+    public void testUrlWithIpAndPortIsAccepted()
+    {
+        testUrlIsAccepted( "http://1.2.3.4:8080/something" );
+    }
+
+    public void testUrlWithHostNameLocalIsAccepted()
+    {
+        testUrlIsAccepted( "http://local/something" );
+    }
+
+    public void testUrlWithDotParisTopLevelDomainIsAccepted()
+    {
+        testUrlIsAccepted( "http://cool.project.paris/" );
+    }
+
+    public void testUrlWithDotAcademyTopLevelDomainIsAccepted()
+    {
+        testUrlIsAccepted( "http://cool.project.academy/" );
+    }
+
+    public void testUrlWithUnknownTopLevelDomainIsAccepted()
+    {
+        testUrlIsRejected( "http://cool.project.foobarisnotarealtld/" );
+    }
+
+    private void testUrlIsAccepted( final String string )
+    {
+        assertTrue( UrlValidationUtil.isValidUrl( string ) );
+    }
+
+    private void testUrlIsRejected( final String string )
+    {
+        assertFalse( UrlValidationUtil.isValidUrl( string ) );
+    }
+
+    public void testAuthorityHostDotCompanyDotLocalIsAccepted()
+    {
+        testAuthorityIsAccepted( "host.organization.local" );
+    }
+
+    public void testAuthorityHostDotLocalIsAccepted()
+    {
+        testAuthorityIsAccepted( "host.local" );
+    }
+
+    public void testAuthorityWithStandardHttpPortIsAccepted()
+    {
+        testAuthorityIsAccepted( "host.organization.local:80" );
+    }
+
+    public void testAuthorityWithStandardHttpsPortIsAccepted()
+    {
+        testAuthorityIsAccepted( "host.organization.local:443" );
+    }
+
+    public void testAuthorityWithPort8080IsAccepted()
+    {
+        testAuthorityIsAccepted( "host.organization.local:8080" );
+    }
+
+    public void testAuthorityWithPortHighPortIsAccepted()
+    {
+        testAuthorityIsAccepted( "host.organization.local:55555" );
+    }
+
+    public void testAuthorityWithPort59999IsAccepted()
+    {
+        testAuthorityIsAccepted( "host.organization.local:59999" );
+    }
+
+    public void testAuthorityWithPort60000IsRejected()
+    {
+        testAuthorityRejects( "host.organization.local:60000" );
+    }
+
+    public void testAuthorityWithPort6000IsAccepted()
+    {
+        testAuthorityIsAccepted( "host.organization.local:6000" );
+    }
+
+    public void testAuthorityWithPort100000IsRejected()
+    {
+        testAuthorityRejects( "host.organization.local:100000" );
+    }
+
+    public void testAuthorityWithLeadingZeroInPortIsRejected()
+    {
+        testAuthorityRejects( "host.organization.local:080" );
+    }
+
+    public void testAuthorityWithTrainlingDotIsAccepted()
+    {
+        testAuthorityIsAccepted( "host.local." );
+    }
+
+    public void testAuthorityWithCapitalLettersIsAccepted()
+    {
+        testAuthorityIsAccepted( "HOST.oRGaNiZAtION.LOcaL" );
+    }
+
+    public void testAuthorityIPIsRejected()
+    {
+        testAuthorityRejects( "1.2.3.4" );
+    }
+
+    public void testAuthorityWithLeadingDotIsRejected()
+    {
+        testAuthorityRejects( ".host.organization.local" );
+    }
+
+    public void testAuthorityOnlyConsistingOfLocalIsRejected()
+    {
+        testAuthorityRejects( "local" );
+    }
+
+    public void testAuthorityWithEmptySubDomainIsRejected()
+    {
+        testAuthorityRejects( "host..local" );
+    }
+
+    public void testAuthorityWithNonLocalDomainIsRejected()
+    {
+        testAuthorityRejects( "www.example.org" );
+    }
+
+    public void testAuthorityWithLeadingHyphenIsRejected()
+    {
+        testAuthorityRejects( "host.-organization.local" );
+    }
+
+    public void testAuthorityWithTrailingHyphenIsRejected()
+    {
+        testAuthorityRejects( "host.organization-.local" );
+    }
+
+    public void testAuthorityWithTooLongSubDomainIsRejected()
+    {
+        String tooLongDomainName = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffabcd";
+        assertTrue( tooLongDomainName.length() == 64 );
+        testAuthorityRejects( "host." + tooLongDomainName + ".local" );
+    }
+
+    private void testAuthorityIsAccepted( final String input )
+    {
+        assertTrue( isValidAuthority( input ) );
+    }
+
+    private boolean isValidAuthority( final String input )
+    {
+        RegexValidator authority = UrlValidationUtil.configureLocalAuthorityValidator();
+        return authority.isValid( input );
+    }
+
+    private void testAuthorityRejects( final String input )
+    {
+        assertFalse( isValidAuthority( input ) );
+    }
+
+}

Propchange: maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain