Skip to content

Commit

Permalink
Try
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Aug 15, 2024
1 parent e7f4f63 commit b2008a7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/it/projects/MJAVADOC-528/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<version>@project.version@</version>
<configuration>
<detectLinks>true</detectLinks>
<validateLinks>true</validateLinks>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3759,13 +3759,14 @@ private void addLinkofflineArguments(List<String> arguments, Set<OfflineLink> of
if (location == null || location.isEmpty()) {
continue;
}
if (isValidJavadocLink(location, false)) {
addArgIfNotEmpty(
arguments,
"-linkoffline",
JavadocUtil.quotedPathArgument(url) + " " + JavadocUtil.quotedPathArgument(location),
true);
if (validateLinks && !isValidJavadocLink(location, false)) {
continue;
}
addArgIfNotEmpty(
arguments,
"-linkoffline",
JavadocUtil.quotedPathArgument(url) + " " + JavadocUtil.quotedPathArgument(location),
true);
}
}

Expand Down Expand Up @@ -5617,7 +5618,11 @@ private List<String> getDependenciesLinks() {
}
}

if (url != null && isValidJavadocLink(url, detected)) {
if (url != null) {
if (validateLinks && !isValidJavadocLink(url, detected)) {
continue;
}

getLog().debug("Added Javadoc link: " + url + " for " + artifact.getId());

dependenciesLinks.add(url);
Expand Down Expand Up @@ -5814,14 +5819,14 @@ protected boolean isValidJavadocLink(String link, boolean detecting) {
}

try {
if (JavadocUtil.isValidElementList(elementListUri.toURL(), settings, validateLinks)) {
if (JavadocUtil.isValidElementList(elementListUri.toURL(), settings)) {
return true;
}
} catch (IOException e) {
// ignore this because it is optional
}

if (JavadocUtil.isValidPackageList(packageListUri.toURL(), settings, validateLinks)) {
if (JavadocUtil.isValidPackageList(packageListUri.toURL(), settings)) {
return true;
}

Expand Down
49 changes: 26 additions & 23 deletions src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1333,52 +1333,55 @@ protected static URL getRedirectUrl(URL url, Settings settings) throws IOExcepti
}

/**
* Validates an <code>URL</code> to point to a valid <code>package-list</code> resource.
* Validates the <code>URL</code> (content) to point to a valid <code>package-list</code> resource.
*
* @param url The URL to validate.
* @param url The URL (content) to validate.
* @param settings The user settings used to configure the connection to the URL or {@code null}.
* @param validateContent <code>true</code> to validate the content of the <code>package-list</code> resource;
* <code>false</code> to only check the existence of the <code>package-list</code> resource.
* @return <code>true</code> if <code>url</code> points to a valid <code>package-list</code> resource;
* <code>false</code> else.
* @throws IOException if reading the resource fails.
* @see #createHttpClient(org.apache.maven.settings.Settings, java.net.URL)
* @since 2.8
*/
protected static boolean isValidPackageList(URL url, Settings settings, boolean validateContent)
throws IOException {
protected static boolean isValidPackageList(URL url, Settings settings) throws IOException {
if (url == null) {
throw new IllegalArgumentException("The url is null");
throw new NullPointerException("The url is null");
}

try (BufferedReader reader = getReader(url, settings)) {
if (validateContent) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (!isValidPackageName(line)) {
return false;
}
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (!isValidPackageName(line)) {
return false;
}
}
return true;
}
}

protected static boolean isValidElementList(URL url, Settings settings, boolean validateContent)
throws IOException {
/**
* Validates the <code>URL</code> (content) to point to a valid <code>element-list</code> resource.
*
* @param url The URL (content) to validate.
* @param settings The user settings used to configure the connection to the URL or {@code null}.
* @return <code>true</code> if <code>url</code> points to a valid <code>element-list</code> resource;
* <code>false</code> else.
* @throws IOException if reading the resource fails.
* @see #createHttpClient(org.apache.maven.settings.Settings, java.net.URL)
* @since 3.1.0
*/
protected static boolean isValidElementList(URL url, Settings settings) throws IOException {
if (url == null) {
throw new IllegalArgumentException("The url is null");
throw new NullPointerException("The url is null");
}

try (BufferedReader reader = getReader(url, settings)) {
if (validateContent) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.startsWith("module:")) {
continue;
}
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.startsWith("module:")) {
continue;
}

if (!isValidPackageName(line)) {
return false;
}
if (!isValidPackageName(line)) {
return false;
}
}
return true;
Expand Down
28 changes: 14 additions & 14 deletions src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,17 @@ public void testIsValidPackageList() throws Exception {
URL url = null;
URL wrongUrl;
try {
JavadocUtil.isValidPackageList(url, settings, false);
JavadocUtil.isValidPackageList(url, settings);
fail();
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
assertTrue(true);
}

url = new File(getBasedir(), "/pom.xml").toURI().toURL();
assertTrue(JavadocUtil.isValidPackageList(url, settings, false));
assertFalse(JavadocUtil.isValidPackageList(url, settings));

try {
assertFalse(JavadocUtil.isValidPackageList(url, settings, true));
assertFalse(JavadocUtil.isValidPackageList(url, settings));
} catch (IOException e) {
assertTrue(true);
}
Expand All @@ -268,14 +268,14 @@ public void testIsValidPackageList() throws Exception {
.getResource("/JavadocUtilTest-package-list.txt")
.toURI()
.toURL();
assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
assertTrue(JavadocUtil.isValidPackageList(url, settings));

url = new URL("http://maven.apache.org/plugins-archives/maven-javadoc-plugin-3.5.0/apidocs/package-list");
assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
assertTrue(JavadocUtil.isValidPackageList(url, settings));

wrongUrl = new URL("http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list2");
try {
JavadocUtil.isValidPackageList(wrongUrl, settings, false);
JavadocUtil.isValidPackageList(wrongUrl, settings);
fail();
} catch (IOException e) {
assertTrue(true);
Expand All @@ -291,10 +291,10 @@ public void testIsValidPackageList() throws Exception {

settings = new Settings();

assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
assertTrue(JavadocUtil.isValidPackageList(url, settings));

try {
JavadocUtil.isValidPackageList(wrongUrl, settings, false);
JavadocUtil.isValidPackageList(wrongUrl, settings);
fail();
} catch (IOException e) {
assertTrue(true);
Expand All @@ -321,7 +321,7 @@ public void testIsValidPackageList() throws Exception {
proxy.setProtocol("http");
settings.addProxy(proxy);

JavadocUtil.isValidPackageList(url, settings, false);
JavadocUtil.isValidPackageList(url, settings);
fail();
} catch (FileNotFoundException e) {
assertTrue(true);
Expand All @@ -345,10 +345,10 @@ public void testIsValidPackageList() throws Exception {
proxy.setPassword("bar");
settings.addProxy(proxy);

assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
assertTrue(JavadocUtil.isValidPackageList(url, settings));

try {
JavadocUtil.isValidPackageList(wrongUrl, settings, false);
JavadocUtil.isValidPackageList(wrongUrl, settings);
fail();
} catch (IOException e) {
assertTrue(true);
Expand All @@ -373,7 +373,7 @@ public void testIsValidPackageList() throws Exception {
proxy.setPassword("bar");
settings.addProxy(proxy);

JavadocUtil.isValidPackageList(url, settings, true);
JavadocUtil.isValidPackageList(url, settings);
fail();
} catch (SocketTimeoutException e) {
assertTrue(true);
Expand All @@ -398,7 +398,7 @@ public void testIsValidPackageList() throws Exception {
proxy.setNonProxyHosts("maven.apache.org");
settings.addProxy(proxy);

assertTrue(JavadocUtil.isValidPackageList(url, settings, true));
assertTrue(JavadocUtil.isValidPackageList(url, settings));
} finally {
proxyServer.stop();
}
Expand Down

0 comments on commit b2008a7

Please sign in to comment.