From 7175c4ca9ad661f487f87daf837a4204a1cb71fd Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 7 Oct 2022 13:06:31 +0200 Subject: [PATCH] Normalize URIs returned from FileSystemResource.getURI() Prior to this commit, if a FileSystemResource was created from a java.nio.file.Path or java.nio.file.FileSystem, the URI returned from getURI() was based on the semantics of Path#toUri() which always includes the "authority component" (i.e., "//") after the scheme (e.g., "file://..."). In contrast, if a FileSystemResource is created from a java.io.File or String, the URI returned from getURI() never includes the "authority component" due to the semantics of File#toURI(). Consequently, invoking myFileSystemResource.getURI().toString() could result in "file:/my/path" or "file:///my/path", depending on how the FileSystemResource was created. This behavior is not new; however, recent changes in PathMatchingResourcePatternResolver -- which result in a FileSystemResource being created from a java.nio.file.Path instead of from a java.io.File -- make this difference more noticeable to users and may in fact surface as a breaking change. To address that issue, this commit revises the implementation of FileSystemResource.getURI() by normalizing any `file:` URIs that it returns. Effectively, URIs like "file:///my/path" will now take the form "file:/my/path". See gh-29163 Closes gh-29275 --- .../core/io/FileSystemResource.java | 23 +++++++++++++++++-- ...hMatchingResourcePatternResolverTests.java | 2 -- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/FileSystemResource.java b/spring-core/src/main/java/org/springframework/core/io/FileSystemResource.java index feb2f9067da..8cee9ae95d6 100644 --- a/spring-core/src/main/java/org/springframework/core/io/FileSystemResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/FileSystemResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; @@ -34,6 +35,7 @@ import java.nio.file.StandardOpenOption; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; /** @@ -48,6 +50,7 @@ import org.springframework.util.StringUtils; * interactions via NIO.2, only resorting to {@link File} on {@link #getFile()}. * * @author Juergen Hoeller + * @author Sam Brannen * @since 28.12.2003 * @see #FileSystemResource(String) * @see #FileSystemResource(File) @@ -226,7 +229,23 @@ public class FileSystemResource extends AbstractResource implements WritableReso */ @Override public URI getURI() throws IOException { - return (this.file != null ? this.file.toURI() : this.filePath.toUri()); + if (this.file != null) { + return this.file.toURI(); + } + else { + URI uri = this.filePath.toUri(); + // Normalize URI? See https://github.com/spring-projects/spring-framework/issues/29275 + String scheme = uri.getScheme(); + if (ResourceUtils.URL_PROTOCOL_FILE.equals(scheme)) { + try { + uri = new URI(scheme, uri.getPath(), null); + } + catch (URISyntaxException ex) { + throw new IllegalStateException("Failed to normalize URI: " + uri, ex); + } + } + return uri; + } } /** 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 ec62677a910..ec0dffa9884 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 @@ -23,7 +23,6 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.List; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -108,7 +107,6 @@ class PathMatchingResourcePatternResolverTests { assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt"); } - @Disabled("Disabled until gh-29275 is addressed") @Test void usingFileProtocolAndAssertingUrlAndUriSyntax() throws Exception { Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath();