Explicit URI decoding for FileSystemResource in convertClassLoaderURL

Also raising the log level to warn for file system retrieval failures.

Closes gh-30031
This commit is contained in:
Juergen Hoeller 2023-03-02 10:18:13 +01:00
parent 244c97993b
commit 4419d56178
2 changed files with 20 additions and 18 deletions

View File

@ -405,8 +405,19 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
* @see #doFindPathMatchingFileResources
*/
protected Resource convertClassLoaderURL(URL url) {
return (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol()) ?
new FileSystemResource(url.getPath()) : new UrlResource(url));
if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())) {
try {
// URI decoding for special characters such as spaces.
return new FileSystemResource(ResourceUtils.toURI(url).getSchemeSpecificPart());
}
catch (URISyntaxException ex) {
// Fallback for URLs that are not valid URIs (should hardly ever happen).
return new FileSystemResource(url.getFile());
}
}
else {
return new UrlResource(url);
}
}
/**
@ -748,8 +759,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
rootDirUri = rootDirResource.getURI();
}
catch (Exception ex) {
if (logger.isInfoEnabled()) {
logger.info("Failed to resolve %s as URI: %s".formatted(rootDirResource, ex));
if (logger.isWarnEnabled()) {
logger.warn("Failed to resolve directory [%s] as URI: %s".formatted(rootDirResource, ex));
}
return Collections.emptySet();
}
@ -797,21 +808,11 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
Set<Resource> result = new LinkedHashSet<>();
try (Stream<Path> files = Files.walk(rootPath)) {
files.filter(isMatchingFile).sorted().forEach(file -> {
try {
result.add(new FileSystemResource(file));
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to convert file %s to an org.springframework.core.io.Resource: %s"
.formatted(file, ex));
}
}
});
files.filter(isMatchingFile).sorted().forEach(file -> result.add(new FileSystemResource(file)));
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to complete search in directory [%s] for files matching pattern [%s]: %s"
if (logger.isWarnEnabled()) {
logger.warn("Failed to search in directory [%s] for files matching pattern [%s]: %s"
.formatted(rootPath.toAbsolutePath(), subPattern, ex));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -219,6 +219,7 @@ public abstract class ResourceUtils {
"because it does not reside in the file system: " + resourceUrl);
}
try {
// URI decoding for special characters such as spaces.
return new File(toURI(resourceUrl).getSchemeSpecificPart());
}
catch (URISyntaxException ex) {