-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Robert Saenger opened SPR-16833 and commented
org.springframework.core.io.PathResource#createRelative(String) doesn't work correctly if the underlying path refers to a file and not to a directory. The problem of the implementation is, that it doesn't properly distinguish between files an directories. In both cases the implementation returns a Resource whose path is simply extended by the given relative path. This is correct if the path points to a directory but invalid if the path points to a file, e.g. <path-to-file>/<relative path> is always invalid it should be <path-to-file-parent>/<relative-path>.
Suggested fix ist to change the implementation as follows:
/**
* This implementation creates a PathResource, applying the given path relative
* to the path of the underlying file of this resource descriptor. It properly
* distinguishes whether the underlying file of this resource descriptor is a
* file or directory.
*
* @see java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...)
* @see java.nio.file.Path#resolve(String)
* @see java.nio.file.Path#resolveSibling(String)
*/
@Override
public Resource createRelative(final String relativePath) throws IOException {
return new PathResource(Files.isDirectory(path) ? path.resolve(relativePath) : path.resolveSibling(relativePath));
}
Since the implementation prevents access to the instance variable path, it is not possible to fix the problem via inheritance and overriding the method. Thus as workaround the entire class may be copied and modified as described above.
Affects: 5.0.2
Issue Links:
- Introduce java.nio.file.Path based Resource implementation [SPR-10608] #15237 Introduce java.nio.file.Path based Resource implementation
- ClassPathResource.isReadable() returns true for directory in runnable jar result in download empty file [SPR-16832] #21372 ClassPathResource.isReadable() returns true for directory in runnable jar result in download empty file
- PathMatchingResourcePatternResolver should enforce OS-independent sorting of directory content [SPR-16838] #21378 PathMatchingResourcePatternResolver should enforce OS-independent sorting of directory content
- FilePart.transferTo should accept java.nio.file.Path [SPR-16925] #21464 FilePart.transferTo should accept java.nio.file.Path
- Revise FileSystemResource / FileSystemUtils / FileCopyUtils towards NIO.2 [SPR-15748] #20304 Revise FileSystemResource / FileSystemUtils / FileCopyUtils towards NIO.2
- HTTP 404 for static resources with last modified = 0L (breaks Docker images build with Jib) [SPR-17320] #21853 HTTP 404 for static resources with last modified = 0L (breaks Docker images build with Jib)
Referenced from: commits 38f9a7b