From 7cb54de985b648d637cb46c3d7180fa184aa4379 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 26 Sep 2022 16:52:34 +0200 Subject: [PATCH] Apply root dir cleanup only for GraalVM native image resource scheme See gh-29163 --- .../PathMatchingResourcePatternResolver.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 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 ba8bfc17e6..603311f447 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 @@ -738,16 +738,16 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol protected Set doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { - 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 = "/"; - } + String rootDir = rootDirUri.getPath(); + // If the URI is for a "resource" in the GraalVM native image file system, we have to + // ensure that the root directory does not end in a slash while simultaneously ensuring + // that the root directory is not an empty string (since fileSystem.getPath("").resolve(str) + // throws an ArrayIndexOutOfBoundsException in a native image). + if ("resource".equals(rootDirUri.getScheme()) && (rootDir.length() > 1) && rootDir.endsWith("/")) { + rootDir = rootDir.substring(0, rootDir.length() - 1); } + FileSystem fileSystem; try { fileSystem = FileSystems.getFileSystem(rootDirUri.resolve("/")); @@ -873,10 +873,6 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol 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.