Skip to content

Commit 0252e39

Browse files
committed
Check for the existence of any actual jar entries in case of jar root
Closes gh-34796
1 parent 2f60083 commit 0252e39

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
8686
if (con instanceof JarURLConnection jarCon) {
8787
// For JarURLConnection, do not check content-length but rather the
8888
// existence of the entry (or the jar root in case of no entryName).
89-
return (jarCon.getEntryName() == null || jarCon.getJarEntry() != null);
89+
try {
90+
if (jarCon.getEntryName() == null) {
91+
// Jar root: check for the existence of any actual jar entries.
92+
return jarCon.getJarFile().entries().hasMoreElements();
93+
}
94+
return (jarCon.getJarEntry() != null);
95+
}
96+
finally {
97+
jarCon.getJarFile().close();
98+
}
9099
}
91100
else if (con.getContentLengthLong() > 0) {
92101
return true;

spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ private void writeAssetJar(Path path) throws Exception {
335335
}
336336
assertThat(new FileSystemResource(path).exists()).isTrue();
337337
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR).exists()).isTrue();
338+
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/file.txt").exists()).isTrue();
339+
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/none.txt").exists()).isFalse();
340+
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + "X" + path + ResourceUtils.JAR_URL_SEPARATOR).exists()).isFalse();
341+
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + "X" + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/file.txt").exists()).isFalse();
342+
assertThat(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + "X" + path + ResourceUtils.JAR_URL_SEPARATOR + "assets/none.txt").exists()).isFalse();
338343
}
339344

340345
private void writeApplicationJar(Path path) throws Exception {

0 commit comments

Comments
 (0)