From 966b0a92c6282356a0a3ebc2609bd0e4999f33fb Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 13 Sep 2023 17:14:04 +0200 Subject: [PATCH] Defensively call Resource.getFile() for fallback resolution Closes gh-31216 --- .../PathMatchingResourcePatternResolver.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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 6586e6c0bd2e..5666fdbd1da5 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 @@ -17,6 +17,7 @@ package org.springframework.core.io.support; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.UncheckedIOException; import java.lang.module.ModuleFinder; @@ -786,10 +787,26 @@ protected Set doFindPathMatchingFileResources(Resource rootDirResource // Fallback via Resource.getFile() below } } + if (rootPath == null) { // Resource.getFile() resolution as a fallback - // for custom URI formats and custom Resource implementations - rootPath = Path.of(rootDirResource.getFile().getAbsolutePath()); + try { + rootPath = Path.of(rootDirResource.getFile().getAbsolutePath()); + } + catch (FileNotFoundException ex) { + if (logger.isDebugEnabled()) { + logger.debug("Cannot search for matching files underneath " + rootDirResource + + " in the file system: " + ex.getMessage()); + } + return result; + } + catch (Exception ex) { + if (logger.isInfoEnabled()) { + logger.info("Failed to resolve " + rootDirResource + " in the file system: " + ex); + } + return result; + } } if (!Files.exists(rootPath)) {