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

fixing issue ocurring when trying to open a jarUrl to compute a SHA if #157

Merged
merged 1 commit into from
Nov 15, 2023
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
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);
}
}
}