Restore check for jar root existence (now via getEntryName/getJarEntry)

Closes gh-34607
This commit is contained in:
Juergen Hoeller
2025-03-17 19:20:41 +01:00
parent 5b6abe4c13
commit 760376c318
3 changed files with 30 additions and 20 deletions

View File

@@ -56,6 +56,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
// Try a URL connection content-length header
URLConnection con = url.openConnection();
customizeConnection(con);
HttpURLConnection httpCon = (con instanceof HttpURLConnection huc ? huc : null);
if (httpCon != null) {
httpCon.setRequestMethod("HEAD");
@@ -81,12 +82,16 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
}
}
}
// Check content-length entry but not for JarURLConnection where
// this would open the jar file but effectively never close it ->
// for jar entries, always fall back to stream existence instead.
if (!(con instanceof JarURLConnection) && con.getContentLengthLong() > 0) {
if (con instanceof JarURLConnection jarCon) {
// For JarURLConnection, do not check content-length but rather the
// existence of the entry (or the jar root in case of no entryName).
return (jarCon.getEntryName() == null || jarCon.getJarEntry() != null);
}
else if (con.getContentLengthLong() > 0) {
return true;
}
if (httpCon != null) {
// No HTTP OK status, and no content-length header: give up
httpCon.disconnect();
@@ -346,8 +351,8 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
*/
protected void customizeConnection(URLConnection con) throws IOException {
ResourceUtils.useCachesIfNecessary(con);
if (con instanceof HttpURLConnection httpConn) {
customizeConnection(httpConn);
if (con instanceof HttpURLConnection httpCon) {
customizeConnection(httpCon);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -234,8 +234,8 @@ public class UrlResource extends AbstractFileResolvingResource {
}
catch (IOException ex) {
// Close the HTTP connection (if applicable).
if (con instanceof HttpURLConnection httpConn) {
httpConn.disconnect();
if (con instanceof HttpURLConnection httpCon) {
httpCon.disconnect();
}
throw ex;
}