Skip to content

Commit 7175c4c

Browse files
committed
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
1 parent af617c7 commit 7175c4c

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

spring-core/src/main/java/org/springframework/core/io/FileSystemResource.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
2424
import java.net.URI;
25+
import java.net.URISyntaxException;
2526
import java.net.URL;
2627
import java.nio.channels.FileChannel;
2728
import java.nio.channels.ReadableByteChannel;
@@ -34,6 +35,7 @@
3435

3536
import org.springframework.lang.Nullable;
3637
import org.springframework.util.Assert;
38+
import org.springframework.util.ResourceUtils;
3739
import org.springframework.util.StringUtils;
3840

3941
/**
@@ -48,6 +50,7 @@
4850
* interactions via NIO.2, only resorting to {@link File} on {@link #getFile()}.
4951
*
5052
* @author Juergen Hoeller
53+
* @author Sam Brannen
5154
* @since 28.12.2003
5255
* @see #FileSystemResource(String)
5356
* @see #FileSystemResource(File)
@@ -226,7 +229,23 @@ public URL getURL() throws IOException {
226229
*/
227230
@Override
228231
public URI getURI() throws IOException {
229-
return (this.file != null ? this.file.toURI() : this.filePath.toUri());
232+
if (this.file != null) {
233+
return this.file.toURI();
234+
}
235+
else {
236+
URI uri = this.filePath.toUri();
237+
// Normalize URI? See https://github.com/spring-projects/spring-framework/issues/29275
238+
String scheme = uri.getScheme();
239+
if (ResourceUtils.URL_PROTOCOL_FILE.equals(scheme)) {
240+
try {
241+
uri = new URI(scheme, uri.getPath(), null);
242+
}
243+
catch (URISyntaxException ex) {
244+
throw new IllegalStateException("Failed to normalize URI: " + uri, ex);
245+
}
246+
}
247+
return uri;
248+
}
230249
}
231250

232251
/**

spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Arrays;
2424
import java.util.List;
2525

26-
import org.junit.jupiter.api.Disabled;
2726
import org.junit.jupiter.api.Nested;
2827
import org.junit.jupiter.api.Test;
2928

@@ -108,7 +107,6 @@ void usingFileProtocol() {
108107
assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt");
109108
}
110109

111-
@Disabled("Disabled until gh-29275 is addressed")
112110
@Test
113111
void usingFileProtocolAndAssertingUrlAndUriSyntax() throws Exception {
114112
Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath();

0 commit comments

Comments
 (0)