Skip to content

Commit

Permalink
Preserve URLStreamHandler in toRelativeURL and convertClassLoaderURL
Browse files Browse the repository at this point in the history
Closes gh-33561
See gh-33199
  • Loading branch information
jhoeller committed Sep 25, 2024
1 parent ca04482 commit daa109e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ protected Set<Resource> doFindAllClassPathResources(String path) throws IOExcept
* @see #doFindAllClassPathResources
* @see #doFindPathMatchingFileResources
*/
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
protected Resource convertClassLoaderURL(URL url) {
if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())) {
try {
Expand All @@ -429,14 +430,10 @@ protected Resource convertClassLoaderURL(URL url) {
if (!cleanedPath.equals(urlString)) {
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
try {
// Cannot test for URLStreamHandler directly: URL equality for same String
// in order to find out whether original URL uses default URLStreamHandler.
if (ResourceUtils.toURL(urlString).equals(url)) {
// Plain URL with default URLStreamHandler -> replace with cleaned path.
return new UrlResource(ResourceUtils.toURI(cleanedPath));
}
// Retain original URL instance, potentially including custom URLStreamHandler.
return new UrlResource(new URL(url, cleanedPath));
}
catch (URISyntaxException | MalformedURLException ex) {
catch (MalformedURLException ex) {
// Fallback to regular URL construction below...
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,14 @@ public static URI toURI(String location) throws URISyntaxException {
* @see java.net.URI#toURL()
* @see #toURI(String)
*/
@SuppressWarnings("deprecation") // on JDK 20
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
public static URL toURL(String location) throws MalformedURLException {
try {
// Prefer URI construction with toURL conversion (as of 6.1)
return toURI(StringUtils.cleanPath(location)).toURL();
}
catch (URISyntaxException | IllegalArgumentException ex) {
// Lenient fallback to deprecated (on JDK 20) URL constructor,
// Lenient fallback to deprecated URL constructor,
// e.g. for decoded location Strings with percent characters.
return new URL(location);
}
Expand All @@ -429,11 +429,13 @@ public static URL toURL(String location) throws MalformedURLException {
* @see #toURL(String)
* @see StringUtils#applyRelativePath
*/
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException {
// # can appear in filenames, java.net.URL should not treat it as a fragment
relativePath = StringUtils.replace(relativePath, "#", "%23");

return toURL(StringUtils.applyRelativePath(root.toString(), relativePath));
// Retain original URL instance, potentially including custom URLStreamHandler.
return new URL(root, StringUtils.cleanPath(StringUtils.applyRelativePath(root.toString(), relativePath)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ void relativeResourcesAreEqual() throws Exception {
assertThat(relative).isEqualTo(new UrlResource("file:dir/subdir"));
}

@Test
void unusualRelativeResourcesAreEqual() throws Exception {
Resource resource = new UrlResource("file:dir/");
Resource relative = resource.createRelative("http://spring.io");
assertThat(relative).isEqualTo(new UrlResource("file:dir/http://spring.io"));
}

@Test
void missingRemoteResourceDoesNotExist() throws Exception {
String baseUrl = startServer();
Expand Down

0 comments on commit daa109e

Please sign in to comment.