parent
0e22c3d89d
commit
7a15805f6c
|
|
@ -729,7 +729,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
|
||||||
/**
|
/**
|
||||||
* Find all resources in the file system of the supplied root directory that
|
* Find all resources in the file system of the supplied root directory that
|
||||||
* match the given location sub pattern via the Ant-style PathMatcher.
|
* match the given location sub pattern via the Ant-style PathMatcher.
|
||||||
* @param rootDirResource the root directory as Resource
|
* @param rootDirResource the root directory as a Resource
|
||||||
* @param subPattern the sub pattern to match (below the root directory)
|
* @param subPattern the sub pattern to match (below the root directory)
|
||||||
* @return a mutable Set of matching Resource instances
|
* @return a mutable Set of matching Resource instances
|
||||||
* @throws IOException in case of I/O errors
|
* @throws IOException in case of I/O errors
|
||||||
|
|
@ -738,50 +738,47 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
|
||||||
protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
|
protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
Set<Resource> result = new HashSet<>();
|
|
||||||
|
URI rootDirUri = rootDirResource.getURI();
|
||||||
|
String rootDir = rootDirUri.getRawPath();
|
||||||
|
if (!"file".equals(rootDirUri.getScheme()) && rootDir.startsWith("/")) {
|
||||||
|
rootDir = stripLeadingSlash(rootDir);
|
||||||
|
rootDir = stripTrailingSlash(rootDir);
|
||||||
|
if (rootDir.isEmpty()) {
|
||||||
|
rootDir = "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
FileSystem fileSystem;
|
FileSystem fileSystem;
|
||||||
try {
|
try {
|
||||||
fileSystem = FileSystems.getFileSystem(rootDirResource.getURI().resolve("/"));
|
fileSystem = FileSystems.getFileSystem(rootDirUri.resolve("/"));
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
fileSystem = FileSystems.newFileSystem(rootDirResource.getURI().resolve("/"), Map.of(),
|
fileSystem = FileSystems.newFileSystem(rootDirUri.resolve("/"), Map.of(),
|
||||||
ClassUtils.getDefaultClassLoader());
|
ClassUtils.getDefaultClassLoader());
|
||||||
}
|
}
|
||||||
String rootPath = rootDirResource.getURI().getRawPath();
|
|
||||||
if (!("file").equals(rootDirResource.getURI().getScheme()) && rootPath.startsWith("/")) {
|
Path rootPath = fileSystem.getPath(rootDir);
|
||||||
rootPath = rootPath.substring(1);
|
String resourcePattern = rootPath.resolve(subPattern).toString();
|
||||||
if (rootPath.length()==0) {
|
Predicate<Path> resourcePatternMatches = path -> getPathMatcher().match(resourcePattern, path.toString());
|
||||||
return result;
|
Set<Resource> result = new HashSet<>();
|
||||||
}
|
try (Stream<Path> files = Files.walk(rootPath)) {
|
||||||
if (rootPath.endsWith("/")) {
|
files.filter(resourcePatternMatches).sorted().forEach(file -> {
|
||||||
rootPath = rootPath.substring(0, rootPath.length()-1);
|
try {
|
||||||
}
|
result.add(convertToResource(file.toUri()));
|
||||||
if (rootPath.length()==0) {
|
}
|
||||||
return result;
|
catch (Exception ex) {
|
||||||
}
|
// TODO Introduce logging
|
||||||
}
|
|
||||||
Path path = fileSystem.getPath(rootPath);
|
|
||||||
Path patternPath = path.resolve(subPattern);
|
|
||||||
try (Stream<Path> files = Files.walk(path)) {
|
|
||||||
files.forEach(file -> {
|
|
||||||
if (getPathMatcher().match(patternPath.toString(), file.toString())) {
|
|
||||||
try {
|
|
||||||
result.add(convertToResource(file.toUri()));
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (NoSuchFileException ex) {
|
catch (NoSuchFileException ex) {
|
||||||
// ignore
|
// TODO Introduce logging
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
fileSystem.close();
|
fileSystem.close();
|
||||||
}
|
}
|
||||||
catch (UnsupportedOperationException ex) {
|
catch (UnsupportedOperationException ex) {
|
||||||
// ignore
|
// TODO Introduce logging
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -876,6 +873,10 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
|
||||||
return (path.startsWith("/") ? path.substring(1) : path);
|
return (path.startsWith("/") ? path.substring(1) : path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String stripTrailingSlash(String path) {
|
||||||
|
return (path.endsWith("/") ? path.substring(0, path.length() - 1) : path);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
|
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,7 @@ class PathMatchingResourcePatternResolverTests {
|
||||||
Resource[] resources = resolver.getResources(pattern);
|
Resource[] resources = resolver.getResources(pattern);
|
||||||
List<String> actualNames = Arrays.stream(resources)
|
List<String> actualNames = Arrays.stream(resources)
|
||||||
.map(Resource::getFilename)
|
.map(Resource::getFilename)
|
||||||
|
// Need to decode within GraalVM native image to get %23 converted to #.
|
||||||
.map(filename -> URLDecoder.decode(filename, UTF_8))
|
.map(filename -> URLDecoder.decode(filename, UTF_8))
|
||||||
.sorted()
|
.sorted()
|
||||||
.toList();
|
.toList();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue