Consider empty jar entries as readable

Prior to this commit, resource handling would not serve empty files and
return instead HTTP 404 responses. This would only happen for files
contained by JARs, but not on the filesystem.

This can be tracked to changes done in `AbstractFileResolvingResource`
where we avoid serving empty files for directories, see gh-21372.
This commit improves the `checkReadable` method to align this behavior
between file system and JAR files.

Closes gh-28850
This commit is contained in:
Lars Grefer
2022-07-21 21:30:58 +02:00
committed by Brian Clozel
parent dfd1a31435
commit 94e5eb3eb6
2 changed files with 48 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
@@ -27,6 +28,7 @@ import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
import java.util.jar.JarEntry;
import org.springframework.util.ResourceUtils;
@@ -115,6 +117,16 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
return false;
}
}
else if (con instanceof JarURLConnection) {
JarURLConnection jarCon = (JarURLConnection) con;
JarEntry jarEntry = jarCon.getJarEntry();
if (jarEntry == null) {
return false;
}
else {
return !jarEntry.isDirectory();
}
}
long contentLength = con.getContentLengthLong();
if (contentLength > 0) {
return true;