You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2019/01/04 18:26:23 UTC

[GitHub] asfgit closed pull request #4: [MJAVADOC-527] - detectLinks may pass invalid urls to javadoc

asfgit closed pull request #4: [MJAVADOC-527] - detectLinks may pass invalid urls to javadoc
URL: https://github.com/apache/maven-javadoc-plugin/pull/4
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
index 7ae705b..a068b74 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -1766,7 +1766,7 @@
         DEFAULT_JAVA_API_LINKS.put( "api_1.8", "https://docs.oracle.com/javase/8/docs/api/" );
         DEFAULT_JAVA_API_LINKS.put( "api_9",   "https://docs.oracle.com/javase/9/docs/api/" );
         DEFAULT_JAVA_API_LINKS.put( "api_10",  "https://docs.oracle.com/javase/10/docs/api/" );
-        DEFAULT_JAVA_API_LINKS.put( "api_11",  "https://docs.oracle.com/en/java/javase/11/docs/api" );
+        DEFAULT_JAVA_API_LINKS.put( "api_11",  "https://docs.oracle.com/en/java/javase/11/docs/api/" );
     }
 
     // ----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
index e2f9b26..7b21981 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -33,10 +33,13 @@
 import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.conn.params.ConnRoutePNames;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.RedirectLocations;
 import org.apache.http.impl.conn.PoolingClientConnectionManager;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.params.CoreConnectionPNames;
 import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Proxy;
@@ -1679,14 +1682,15 @@ private static BufferedReader getReader( URL url, Settings settings ) throws IOE
             final HttpGet httpMethod = new HttpGet( url.toString() );
             
             HttpResponse response;
+            HttpContext context = new BasicHttpContext();
             try
             {
-                response = httpClient.execute( httpMethod );
+                response = httpClient.execute( httpMethod, context );
             }
             catch ( SocketTimeoutException e )
             {
                 // could be a sporadic failure, one more retry before we give up
-                response = httpClient.execute( httpMethod );
+                response = httpClient.execute( httpMethod, context );
             }
 
             int status = response.getStatusLine().getStatusCode();
@@ -1695,6 +1699,23 @@ private static BufferedReader getReader( URL url, Settings settings ) throws IOE
                 throw new FileNotFoundException( "Unexpected HTTP status code " + status + " getting resource "
                     + url.toExternalForm() + "." );
             }
+            else
+            {
+                int pos = url.getPath().lastIndexOf( '/' );
+                RedirectLocations redirects = (RedirectLocations)
+                        context.getAttribute( "http.protocol.redirect-locations" );
+                if ( pos >= 0 && redirects != null )
+                {
+                    URI location = redirects.get( redirects.size() - 1 );
+                    String suffix = url.getPath().substring( pos );
+                    // Redirections shall point to the same file, e.g. /package-list
+                    if ( !location.toURL().getPath().endsWith( suffix ) )
+                    {
+                        throw new FileNotFoundException( url.toExternalForm() + " redirects to "
+                                + location.toURL().toExternalForm() + "." );
+                    }
+                }
+            }
 
             // Intentionally using the platform default encoding here since this is what Javadoc uses internally.
             reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) ) 
diff --git a/src/test/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojoTest.java b/src/test/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojoTest.java
index 56988f7..83dbb3d 100644
--- a/src/test/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojoTest.java
@@ -76,4 +76,15 @@ public void testMJAVADOC432_DetectLinksMessages()
         verify( log, times( 4 ) ).error( anyString() );
         verify( log, times( 4 ) ).warn( anyString() ); // no extra warnings
     }
+    
+    public void testMJAVADOC527_DetectLinksRecursion()
+    {
+        Log log = mock( Log.class );
+        when( log.isErrorEnabled() ).thenReturn( true );
+        mojo.setLog( log );
+        mojo.outputDirectory = new File( "target/test-classes" );
+
+        assertFalse( mojo.isValidJavadocLink( "http://javamail.java.net/mailapi/apidocs", false ) );
+        assertTrue( mojo.isValidJavadocLink( "http://commons.apache.org/proper/commons-lang/apidocs", false ) );
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services