Skip to content

Commit 7a15805

Browse files
committed
Polish contribution
See gh-29163
1 parent 0e22c3d commit 7a15805

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+31-30
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ protected JarFile getJarFile(String jarFileUrl) throws IOException {
729729
/**
730730
* Find all resources in the file system of the supplied root directory that
731731
* match the given location sub pattern via the Ant-style PathMatcher.
732-
* @param rootDirResource the root directory as Resource
732+
* @param rootDirResource the root directory as a Resource
733733
* @param subPattern the sub pattern to match (below the root directory)
734734
* @return a mutable Set of matching Resource instances
735735
* @throws IOException in case of I/O errors
@@ -738,50 +738,47 @@ protected JarFile getJarFile(String jarFileUrl) throws IOException {
738738
protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
739739
throws IOException {
740740

741-
Set<Resource> result = new HashSet<>();
741+
742+
URI rootDirUri = rootDirResource.getURI();
743+
String rootDir = rootDirUri.getRawPath();
744+
if (!"file".equals(rootDirUri.getScheme()) && rootDir.startsWith("/")) {
745+
rootDir = stripLeadingSlash(rootDir);
746+
rootDir = stripTrailingSlash(rootDir);
747+
if (rootDir.isEmpty()) {
748+
rootDir = "/";
749+
}
750+
}
742751
FileSystem fileSystem;
743752
try {
744-
fileSystem = FileSystems.getFileSystem(rootDirResource.getURI().resolve("/"));
753+
fileSystem = FileSystems.getFileSystem(rootDirUri.resolve("/"));
745754
}
746755
catch (Exception ex) {
747-
fileSystem = FileSystems.newFileSystem(rootDirResource.getURI().resolve("/"), Map.of(),
756+
fileSystem = FileSystems.newFileSystem(rootDirUri.resolve("/"), Map.of(),
748757
ClassUtils.getDefaultClassLoader());
749758
}
750-
String rootPath = rootDirResource.getURI().getRawPath();
751-
if (!("file").equals(rootDirResource.getURI().getScheme()) && rootPath.startsWith("/")) {
752-
rootPath = rootPath.substring(1);
753-
if (rootPath.length()==0) {
754-
return result;
755-
}
756-
if (rootPath.endsWith("/")) {
757-
rootPath = rootPath.substring(0, rootPath.length()-1);
758-
}
759-
if (rootPath.length()==0) {
760-
return result;
761-
}
762-
}
763-
Path path = fileSystem.getPath(rootPath);
764-
Path patternPath = path.resolve(subPattern);
765-
try (Stream<Path> files = Files.walk(path)) {
766-
files.forEach(file -> {
767-
if (getPathMatcher().match(patternPath.toString(), file.toString())) {
768-
try {
769-
result.add(convertToResource(file.toUri()));
770-
}
771-
catch (Exception ex) {
772-
// ignore
773-
}
759+
760+
Path rootPath = fileSystem.getPath(rootDir);
761+
String resourcePattern = rootPath.resolve(subPattern).toString();
762+
Predicate<Path> resourcePatternMatches = path -> getPathMatcher().match(resourcePattern, path.toString());
763+
Set<Resource> result = new HashSet<>();
764+
try (Stream<Path> files = Files.walk(rootPath)) {
765+
files.filter(resourcePatternMatches).sorted().forEach(file -> {
766+
try {
767+
result.add(convertToResource(file.toUri()));
768+
}
769+
catch (Exception ex) {
770+
// TODO Introduce logging
774771
}
775772
});
776773
}
777774
catch (NoSuchFileException ex) {
778-
// ignore
775+
// TODO Introduce logging
779776
}
780777
try {
781778
fileSystem.close();
782779
}
783780
catch (UnsupportedOperationException ex) {
784-
// ignore
781+
// TODO Introduce logging
785782
}
786783
return result;
787784
}
@@ -876,6 +873,10 @@ private static String stripLeadingSlash(String path) {
876873
return (path.startsWith("/") ? path.substring(1) : path);
877874
}
878875

876+
private static String stripTrailingSlash(String path) {
877+
return (path.endsWith("/") ? path.substring(0, path.length() - 1) : path);
878+
}
879+
879880

880881
/**
881882
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.

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

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ private void assertFilenames(String pattern, String... filenames) throws IOExcep
137137
Resource[] resources = resolver.getResources(pattern);
138138
List<String> actualNames = Arrays.stream(resources)
139139
.map(Resource::getFilename)
140+
// Need to decode within GraalVM native image to get %23 converted to #.
140141
.map(filename -> URLDecoder.decode(filename, UTF_8))
141142
.sorted()
142143
.toList();

0 commit comments

Comments
 (0)