Skip to content

Commit

Permalink
Merge pull request #157 from ehsavoie/jar_url_issue
Browse files Browse the repository at this point in the history
fixing issue ocurring when trying to open a jarUrl to compute a SHA if
  • Loading branch information
jponge authored Nov 15, 2023
2 parents 7540c78 + 3348c5a commit 2de64f3
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 2de64f3

Please sign in to comment.