Browse Source

Avoid use of java.net.URL constructors (for JDK 20 compatibility)

Explicit path computation also leads to consistent relative path semantics for resource URLs.

Closes gh-29481
Closes gh-28522
pull/30419/head
Juergen Hoeller 2 years ago
parent
commit
9342317291
  1. 32
      spring-core/src/main/java/org/springframework/core/io/UrlResource.java
  2. 18
      spring-core/src/main/java/org/springframework/util/ResourceUtils.java
  3. 7
      spring-webflux/src/test/java/org/springframework/web/reactive/resource/PathResourceResolverTests.java
  4. 8
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java

32
spring-core/src/main/java/org/springframework/core/io/UrlResource.java

@ -89,33 +89,31 @@ public class UrlResource extends AbstractFileResolvingResource { @@ -89,33 +89,31 @@ public class UrlResource extends AbstractFileResolvingResource {
}
/**
* Create a new {@code UrlResource} based on a URL path.
* Create a new {@code UrlResource} based on a URI path.
* <p>Note: The given path needs to be pre-encoded if necessary.
* @param path a URL path
* @throws MalformedURLException if the given URL path is not valid
* @see java.net.URL#URL(String)
* @param path a URI path
* @throws MalformedURLException if the given URI path is not valid
* @see ResourceUtils#toURI(String)
*/
public UrlResource(String path) throws MalformedURLException {
Assert.notNull(path, "Path must not be null");
String cleanedPath = StringUtils.cleanPath(path);
URI uri;
URL url;
// Equivalent without java.net.URL constructor - for building on JDK 20+
/*
try {
String cleanedPath = StringUtils.cleanPath(path);
this.uri = ResourceUtils.toURI(cleanedPath);
this.url = this.uri.toURL();
this.cleanedUrl = cleanedPath;
// Prefer URI construction with toURL conversion (as of 6.1)
uri = ResourceUtils.toURI(cleanedPath);
url = uri.toURL();
}
catch (URISyntaxException | IllegalArgumentException ex) {
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
exToThrow.initCause(ex);
throw exToThrow;
uri = null;
url = ResourceUtils.toURL(path);
}
*/
this.uri = null;
this.url = ResourceUtils.toURL(path);
this.cleanedUrl = StringUtils.cleanPath(path);
this.uri = uri;
this.url = url;
this.cleanedUrl = cleanedPath;
}
/**

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

@ -390,21 +390,18 @@ public abstract class ResourceUtils { @@ -390,21 +390,18 @@ public abstract class ResourceUtils {
* @throws MalformedURLException if the location wasn't a valid URL
* @since 6.0
*/
@SuppressWarnings("deprecation") // on JDK 20
public static URL toURL(String location) throws MalformedURLException {
// Equivalent without java.net.URL constructor - for building on JDK 20+
/*
try {
// Prefer URI construction with toURL conversion (as of 6.1)
return toURI(StringUtils.cleanPath(location)).toURL();
}
catch (URISyntaxException | IllegalArgumentException ex) {
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
exToThrow.initCause(ex);
throw exToThrow;
}
*/
// Lenient fallback to deprecated (on JDK 20) URL constructor,
// e.g. for decoded location Strings with percent characters.
return new URL(location);
}
}
/**
* Create a URL instance for the given root URL and relative path,
@ -419,12 +416,7 @@ public abstract class ResourceUtils { @@ -419,12 +416,7 @@ public abstract class ResourceUtils {
// # can appear in filenames, java.net.URL should not treat it as a fragment
relativePath = StringUtils.replace(relativePath, "#", "%23");
// Equivalent without java.net.URL constructor - for building on JDK 20+
/*
return toURL(StringUtils.applyRelativePath(root.toString(), relativePath));
*/
return new URL(root, relativePath);
}
/**

7
spring-webflux/src/test/java/org/springframework/web/reactive/resource/PathResourceResolverTests.java

@ -40,7 +40,6 @@ public class PathResourceResolverTests { @@ -40,7 +40,6 @@ public class PathResourceResolverTests {
private static final Duration TIMEOUT = Duration.ofSeconds(5);
private final PathResourceResolver resolver = new PathResourceResolver();
@ -111,7 +110,11 @@ public class PathResourceResolverTests { @@ -111,7 +110,11 @@ public class PathResourceResolverTests {
@Test // gh-23463
public void ignoreInvalidEscapeSequence() throws IOException {
UrlResource location = new UrlResource(getClass().getResource("./test/"));
Resource resource = location.createRelative("test%file.txt");
Resource resource = new UrlResource(location.getURL() + "test%file.txt");
assertThat(this.resolver.checkResource(resource, location)).isTrue();
resource = location.createRelative("test%file.txt");
assertThat(this.resolver.checkResource(resource, location)).isTrue();
}

8
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -95,7 +95,11 @@ public class PathResourceResolverTests { @@ -95,7 +95,11 @@ public class PathResourceResolverTests {
@Test // gh-23463
public void ignoreInvalidEscapeSequence() throws IOException {
UrlResource location = new UrlResource(getClass().getResource("./test/"));
Resource resource = location.createRelative("test%file.txt");
Resource resource = new UrlResource(location.getURL() + "test%file.txt");
assertThat(this.resolver.checkResource(resource, location)).isTrue();
resource = location.createRelative("test%file.txt");
assertThat(this.resolver.checkResource(resource, location)).isTrue();
}

Loading…
Cancel
Save