Skip to content

Commit

Permalink
Merge pull request #15360 from jaikiran/qk-15358
Browse files Browse the repository at this point in the history
Return properly encoded URLs from JarResource#getResourceURL()
  • Loading branch information
stuartwdouglas authored Mar 2, 2021
2 parents dad1c11 + fdd3588 commit 39ab3db
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ public URL getResourceURL(String resource) {
if (realName.endsWith("/")) {
realName = realName.substring(0, realName.length() - 1);
}
URI jarUri = jarPath.toUri();
return new URL("jar", null, jarUri.getScheme() + ':' + jarUri.getPath() + "!/" + resource);
} catch (MalformedURLException e) {
final URI jarUri = jarPath.toUri();
// first create a URI which includes both the jar file path and the relative resource name
// and then invoke a toURL on it. The URI reconstruction allows for any encoding to be done
// for the "path" which includes the "realName"
final URL resUrl = new URI(jarUri.getScheme(), jarUri.getPath() + "!/" + realName, null).toURL();
// wrap it up into a "jar" protocol URL
return new URL("jar", null, resUrl.getProtocol() + ':' + resUrl.getPath());
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.bootstrap.runner;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Tests {@link JarResource}
*/
public class JarResourceTest {

/**
* Tests that the URL(s) returned from {@link JarResource#getResourceURL(String)} are properly encoded and can be used
* to open connection to the URL to read data
*/
@Test
public void testResourceURLEncoding() throws Exception {
final JavaArchive jar = ShrinkWrap.create(JavaArchive.class);
final String[] files = new String[] { "a.txt", "a b.txt", ",;~!@#$%^&().txt" };
for (final String file : files) {
jar.add(new StringAsset("hello"), file);
}
final Path testDir = Files.createTempDirectory("test");
// create a child dir with special characters
final Path specialCharDir = Files.createDirectory(Paths.get(testDir.toString(), ",;~!@#$%^&()"));
final Path jarFilePath = Files.createTempFile(specialCharDir, "test", "quarkus-test.jar");
// create a jar file under the directory which has the special characters
jar.as(ZipExporter.class).exportTo(jarFilePath.toFile(), true);
final JarResource jarResource = new JarResource(null, jarFilePath);
for (final String resource : files) {
final URL url = jarResource.getResourceURL(resource);
Assertions.assertNotNull(url, resource + " is missing in jar");
// check that opening the resource URL works and data can be read
final URLConnection conn = url.openConnection();
try (final InputStream is = conn.getInputStream()) {
drainFully(is);
}
}
}

private static void drainFully(final InputStream inputStream) throws IOException {
int read = -1;
final byte[] data = new byte[1024];
while ((read = inputStream.read(data)) != -1) {
}
}
}

0 comments on commit 39ab3db

Please sign in to comment.