Skip to content

Commit

Permalink
fix: jarUrl opening fails when not targeting an embedded archive
Browse files Browse the repository at this point in the history
When trying to open a jarUrl to compute a SHA if the target archive is the jar itself
and not an embedded archive then openStream will fail.
So in that case we need to open a stream to a file URL and not a jarUrl.

Signed-off-by: Emmanuel Hugonnet <[email protected]>
  • Loading branch information
ehsavoie committed Nov 15, 2023
1 parent 7540c78 commit 3348c5a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
23 changes: 15 additions & 8 deletions api/src/main/java/com/redhat/insights/jars/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,34 @@ private static Map<String, String> getEmbeddedFormatToExtension(String... fileEx
* @throws IOException
*/
public static InputStream getInputStream(URL url) throws IOException {

String jarLocation = url.toExternalForm();
URL jarURL = url;
for (Entry<String, String> entry : EMBEDDED_FORMAT_TO_EXTENSION.entrySet()) {
int index = url.toExternalForm().indexOf(entry.getKey());
if (index > 0) {
int index = jarLocation.indexOf(entry.getKey());
// if the target is the jar file then we can't use openStream as it is there to look into the
// jar content.
if (index > 0 && (index + entry.getKey().length()) < jarLocation.length()) {
String path = url.toExternalForm().substring(index + entry.getKey().length());
// add 1 to skip past the `.` and the value length, which is the length of the file
// extension
url = new URL(url.toExternalForm().substring(0, index + 1 + entry.getValue().length()));
InputStream inputStream = url.openStream();
jarURL =
new URL(jarURL.toExternalForm().substring(0, index + 1 + entry.getValue().length()));
InputStream inputStream = jarURL.openStream();
JarInputStream jarStream = new JarInputStream(inputStream);

if (!readToEntry(jarStream, path)) {
inputStream.close();
throw new IOException(
"Unable to open stream for " + path + " in " + url.toExternalForm());
"Unable to open stream for " + path + " in " + jarURL.toExternalForm());
}
return jarStream;
}
}

return url.openStream();
if (jarLocation.startsWith("jar:") && jarLocation.endsWith("!/")) {
String jarLoc = jarLocation.substring(4, jarLocation.length() - 2);
jarURL = new URL(jarLoc);
}
return jarURL.openStream();
}

/**
Expand Down
13 changes: 12 additions & 1 deletion api/src/test/java/com/redhat/insights/jars/TestJarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import org.junit.jupiter.api.Test;
Expand All @@ -26,7 +27,7 @@ public void basicHashTest() throws IOException, NoSuchAlgorithmException {
}

@Test
public void basicCheckJars() throws NoSuchAlgorithmException, IOException {
public void basicCheckJars() throws NoSuchAlgorithmException, IOException, URISyntaxException {
URL jar1URL = getURL(JAR_PATH);
URL jar2URL = getURL(JAR_PATH_2);

Expand All @@ -43,4 +44,14 @@ private byte[] readAllBytes(InputStream inputStream) throws IOException {
return buffer.toByteArray();
}
}

@Test
public void basicCheckJarUrls() throws NoSuchAlgorithmException, IOException, URISyntaxException {
String file = "jar:" + getURL(JAR_PATH).toExternalForm() + "!/";
try {
computeSha(new URL(file));
} catch (Exception ex) {
fail(ex);
}
}
}

0 comments on commit 3348c5a

Please sign in to comment.