From 0e22c3d89d1c227f6dc8f5ae5cdc3b84a25bf818 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 25 Sep 2022 18:23:13 +0200 Subject: [PATCH] Convert `file:` URL to FileSystemResource for backward compatibility See gh-29163 --- .../PathMatchingResourcePatternResolver.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 65d17a38e3a2..31f1ef9eee3e 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -26,6 +26,7 @@ import java.lang.reflect.Method; import java.net.JarURLConnection; import java.net.MalformedURLException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; @@ -765,9 +766,9 @@ protected Set doFindPathMatchingFileResources(Resource rootDirResource files.forEach(file -> { if (getPathMatcher().match(patternPath.toString(), file.toString())) { try { - result.add(new UrlResource(file.toUri())); + result.add(convertToResource(file.toUri())); } - catch (MalformedURLException ex) { + catch (Exception ex) { // ignore } } @@ -849,14 +850,12 @@ protected Set findAllModulePathResources(String locationPattern) throw } @Nullable - private static Resource findResource(ModuleReader moduleReader, String name) { + private Resource findResource(ModuleReader moduleReader, String name) { try { return moduleReader.find(name) // If it's a "file:" URI, use FileSystemResource to avoid duplicates // for the same path discovered via class-path scanning. - .map(uri -> ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ? - new FileSystemResource(uri.getPath()) : - UrlResource.from(uri)) + .map(this::convertToResource) .orElse(null); } catch (Exception ex) { @@ -867,6 +866,12 @@ private static Resource findResource(ModuleReader moduleReader, String name) { } } + private Resource convertToResource(URI uri) { + return ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme()) ? + new FileSystemResource(uri.getPath()) : + UrlResource.from(uri); + } + private static String stripLeadingSlash(String path) { return (path.startsWith("/") ? path.substring(1) : path); }