Skip to content

Commit

Permalink
Normalize URIs returned from FileSystemResource.getURI()
Browse files Browse the repository at this point in the history
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 spring-projectsgh-29163
Closes spring-projectsgh-29275
  • Loading branch information
sbrannen committed Oct 7, 2022
1 parent af617c7 commit 7175c4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -22,6 +22,7 @@
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;
Expand All @@ -34,6 +35,7 @@

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

/**
Expand All @@ -48,6 +50,7 @@
* 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)
Expand Down Expand Up @@ -226,7 +229,23 @@ public URL getURL() throws IOException {
*/
@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;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;

Expand Down Expand Up @@ -108,7 +107,6 @@ void usingFileProtocol() {
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();
Expand Down

0 comments on commit 7175c4c

Please sign in to comment.