From 89b7a6bf471212a8e9564933381b17e9df8421aa Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 25 Aug 2023 14:48:15 +0200 Subject: [PATCH] Skip searching of nonexistent directory in PathMatchingResourcePatternResolver Prior to this commit, when PathMatchingResourcePatternResolver processed a `file:` pattern (for example, `file:/app-config/**`) for a `rootPath` that did not exist in the filesystem, the resolver attempted to search the directory and logged a WARNING message similar to the following when it failed to do so. Failed to search in directory [/app-config/] for files matching pattern [**]: java.nio.file.NoSuchFileException: /app-config/ To avoid unnecessary attempts to search a nonexistent directory, PathMatchingResourcePatternResolver now skips searching of a nonexistent directory and preemptively logs an INFO message similar to the following. Skipping search for files matching pattern [**]: directory [/app-config] does not exist Closes gh-31111 --- .../PathMatchingResourcePatternResolver.java | 8 ++++++++ .../PathMatchingResourcePatternResolverTests.java | 13 +++++++++++++ 2 files changed, 21 insertions(+) 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 6147711e6dc6..6586e6c0bd2e 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 @@ -792,6 +792,14 @@ protected Set doFindPathMatchingFileResources(Resource rootDirResource rootPath = Path.of(rootDirResource.getFile().getAbsolutePath()); } + if (!Files.exists(rootPath)) { + if (logger.isInfoEnabled()) { + logger.info("Skipping search for files matching pattern [%s]: directory [%s] does not exist" + .formatted(subPattern, rootPath.toAbsolutePath())); + } + return result; + } + String rootDir = StringUtils.cleanPath(rootPath.toString()); if (!rootDir.endsWith("/")) { rootDir += "/"; diff --git a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java index 61de1af5dce8..b234739a270a 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java @@ -90,6 +90,19 @@ void classpathStarWithPatternOnFileSystem() { assertFilenames(pattern, expectedFilenames); } + @Test // gh-31111 + void usingFileProtocolWithWildcardInPatternAndNonexistentRootPath() throws IOException { + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); + String pattern = String.format("file:%s/example/bogus/**", testResourcesDir); + assertThat(resolver.getResources(pattern)).isEmpty(); + // When the log level for the resolver is set to at least INFO, we should see + // a log entry similar to the following. + // + // [main] INFO o.s.c.i.s.PathMatchingResourcePatternResolver - + // Skipping search for files matching pattern [**]: directory + // [/<...>/spring-core/src/test/resources/example/bogus] does not exist + } + @Test void encodedHashtagInPath() throws IOException { Path rootDir = Paths.get("src/test/resources/custom%23root").toAbsolutePath();