Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix File resolver when classloader provides URLs for directories #5092

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading