Skip to content

Commit daa109e

Browse files
committed
Preserve URLStreamHandler in toRelativeURL and convertClassLoaderURL
Closes gh-33561 See gh-33199
1 parent ca04482 commit daa109e

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ protected Set<Resource> doFindAllClassPathResources(String path) throws IOExcept
412412
* @see #doFindAllClassPathResources
413413
* @see #doFindPathMatchingFileResources
414414
*/
415+
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
415416
protected Resource convertClassLoaderURL(URL url) {
416417
if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())) {
417418
try {
@@ -429,14 +430,10 @@ protected Resource convertClassLoaderURL(URL url) {
429430
if (!cleanedPath.equals(urlString)) {
430431
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
431432
try {
432-
// Cannot test for URLStreamHandler directly: URL equality for same String
433-
// in order to find out whether original URL uses default URLStreamHandler.
434-
if (ResourceUtils.toURL(urlString).equals(url)) {
435-
// Plain URL with default URLStreamHandler -> replace with cleaned path.
436-
return new UrlResource(ResourceUtils.toURI(cleanedPath));
437-
}
433+
// Retain original URL instance, potentially including custom URLStreamHandler.
434+
return new UrlResource(new URL(url, cleanedPath));
438435
}
439-
catch (URISyntaxException | MalformedURLException ex) {
436+
catch (MalformedURLException ex) {
440437
// Fallback to regular URL construction below...
441438
}
442439
}

spring-core/src/main/java/org/springframework/util/ResourceUtils.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,14 @@ public static URI toURI(String location) throws URISyntaxException {
405405
* @see java.net.URI#toURL()
406406
* @see #toURI(String)
407407
*/
408-
@SuppressWarnings("deprecation") // on JDK 20
408+
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
409409
public static URL toURL(String location) throws MalformedURLException {
410410
try {
411411
// Prefer URI construction with toURL conversion (as of 6.1)
412412
return toURI(StringUtils.cleanPath(location)).toURL();
413413
}
414414
catch (URISyntaxException | IllegalArgumentException ex) {
415-
// Lenient fallback to deprecated (on JDK 20) URL constructor,
415+
// Lenient fallback to deprecated URL constructor,
416416
// e.g. for decoded location Strings with percent characters.
417417
return new URL(location);
418418
}
@@ -429,11 +429,13 @@ public static URL toURL(String location) throws MalformedURLException {
429429
* @see #toURL(String)
430430
* @see StringUtils#applyRelativePath
431431
*/
432+
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
432433
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException {
433434
// # can appear in filenames, java.net.URL should not treat it as a fragment
434435
relativePath = StringUtils.replace(relativePath, "#", "%23");
435436

436-
return toURL(StringUtils.applyRelativePath(root.toString(), relativePath));
437+
// Retain original URL instance, potentially including custom URLStreamHandler.
438+
return new URL(root, StringUtils.cleanPath(StringUtils.applyRelativePath(root.toString(), relativePath)));
437439
}
438440

439441
/**

spring-core/src/test/java/org/springframework/core/io/ResourceTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ void relativeResourcesAreEqual() throws Exception {
377377
assertThat(relative).isEqualTo(new UrlResource("file:dir/subdir"));
378378
}
379379

380+
@Test
381+
void unusualRelativeResourcesAreEqual() throws Exception {
382+
Resource resource = new UrlResource("file:dir/");
383+
Resource relative = resource.createRelative("http://spring.io");
384+
assertThat(relative).isEqualTo(new UrlResource("file:dir/http://spring.io"));
385+
}
386+
380387
@Test
381388
void missingRemoteResourceDoesNotExist() throws Exception {
382389
String baseUrl = startServer();

0 commit comments

Comments
 (0)