Skip to content

Commit

Permalink
Handle the case where the file resolver is used with a classloader th…
Browse files Browse the repository at this point in the history
…at return a URL for directories.

When unpackFromJarURL is called with a directory, the method throws an exception as the entry does not really exist. By checking the length we can detect the case and not try to retrieve the JarFile from the connection.
  • Loading branch information
cescoffier authored and vietj committed Feb 5, 2024
1 parent 05a35aa commit 0eeba1a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/io/vertx/core/file/impl/FileResolverImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -317,6 +318,17 @@ private File unpackFromJarURL(URL url, String fileName, ClassLoader cl) {
switch (listOfEntries.size()) {
case 1: {
JarURLConnection conn = (JarURLConnection) url.openConnection();
if (conn.getContentLength() == -1) {
// the entry is a directory, so, it does not really exist.
// which means we cannot get the JarFile from the connection.

// In general, we should not be here, as the URL should be a directory, and we should not have a JarURLConnection.
// However, classloader implementation may provide an URL. In this case, we should not try to get the JarFile
// as it does not exist (or is not valid), and it will throw an exception.

// We still retrieve it from the cache if it exists (got unzipped before or concurrently)
return cache.getFile(fileName);
}
ZipFile zip = conn.getJarFile();
try {
extractFilesFromJarFile(zip, fileName);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/vertx/core/file/CustomJarFileResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ protected URLConnection openConnection(URL u) throws IOException {
public JarFile getJarFile() throws IOException {
return new JarFile(files);
}

@Override
public int getContentLength() {
try {
return (int) getJarFile().getJarEntry(name).getSize();
} catch (IOException e) {
return -1;
}
}

@Override
public void connect() throws IOException {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.vertx.core.file;

import io.vertx.core.file.impl.FileResolverImpl;
import io.vertx.core.spi.file.FileResolver;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class JarFileResolverWithOddClassLoaderTest {

@Test
public void testWhenClassLoaderReturnUrlsForDirectory() throws IOException {
File webjar = new File("src/test/resources/jars/wc-chatbot-0.1.2.jar");
Assertions.assertThat(webjar).exists();
ClassLoader cl = new DecoratedClassLoader(new URLClassLoader(new URL[]{webjar.toURI().toURL()}, Thread.currentThread().getContextClassLoader()));

ClassLoader orig = Thread.currentThread().getContextClassLoader();
try (FileResolver resolver = new FileResolverImpl()) {
Thread.currentThread().setContextClassLoader(cl);
String fileName = "META-INF/resources/_static/wc-chatbot/0.1.2/LICENSE";
File resolved = resolver.resolveFile(fileName);
Assertions.assertThat(resolved).exists();
} finally {
Thread.currentThread().setContextClassLoader(orig);
}
}

/**
* A somewhat broken classloader returning an URL even for directory included in the given jars.
*/
private static class DecoratedClassLoader extends ClassLoader {
private final URL[] urls;

public DecoratedClassLoader(URLClassLoader parent) {
super(parent);
this.urls = parent.getURLs();
}

@Override
protected URL findResource(String name) {
if (! name.contains("META-INF/resources")) {
return super.findResource(name);
}
for (URL url : urls) {
if (url.getFile().endsWith(".jar")) {
try {
return new URL("jar:" + url + "!/" + name);
} catch (MalformedURLException e) {
return super.findResource(name);
}
}
}
return super.findResource(name);
}
}
}
Binary file added src/test/resources/jars/wc-chatbot-0.1.2.jar
Binary file not shown.

0 comments on commit 0eeba1a

Please sign in to comment.