From 58c0c6da6f23df9f48b319734dd87b1ce5106fd1 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Mon, 29 Apr 2019 20:48:25 +0200 Subject: [PATCH] [MJAVADOC-539] Strip index.html from redirect URLs --- pom.xml | 3 ++ .../maven/plugins/javadoc/JavadocUtil.java | 29 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4d81bb90..6f22e87d 100644 --- a/pom.xml +++ b/pom.xml @@ -119,6 +119,9 @@ under the License. Michael Stumpf + + Julian Reschke + 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 636bf322..7767b312 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -32,6 +32,7 @@ import java.lang.reflect.Modifier; import java.net.SocketTimeoutException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.Charset; @@ -1554,7 +1555,33 @@ protected static URL getRedirectUrl( URL url, Settings settings ) } List redirects = httpContext.getRedirectLocations(); - return isEmpty( redirects ) ? url : redirects.get( redirects.size() - 1 ).toURL(); + + if ( isEmpty( redirects ) ) + { + return url; + } + else + { + URI last = redirects.get( redirects.size() - 1 ); + + // URI must refer to directory, so prevent redirects to index.html + // see https://issues.apache.org/jira/browse/MJAVADOC-539 + String truncate = "index.html"; + if ( last.getPath().endsWith( "/" + truncate ) ) + { + try + { + String fixedPath = last.getPath().substring( 0, last.getPath().length() - truncate.length() ); + last = new URI( last.getScheme(), last.getAuthority(), fixedPath, last.getQuery(), + last.getFragment() ); + } + catch ( URISyntaxException ex ) + { + // not supposed to happen, but when it does just keep the last URI + } + } + return last.toURL(); + } } }