Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed May 15, 2024
2 parents d625b3d + e4e6910 commit 7d8279a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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 Down Expand Up @@ -49,6 +49,7 @@ public class UrlResource extends AbstractFileResolvingResource {

private static final String AUTHORIZATION = "Authorization";


/**
* Original URI, if available; used for URI and File access.
*/
Expand Down Expand Up @@ -310,7 +311,8 @@ public Resource createRelative(String relativePath) throws MalformedURLException
/**
* This delegate creates a {@code java.net.URL}, applying the given path
* relative to the path of the underlying URL of this resource descriptor.
* A leading slash will get dropped; a "#" symbol will get encoded.
* <p>A leading slash will get dropped; a "#" symbol will get encoded.
* Note that this method effectively cleans the combined path as of 6.1.
* @since 5.2
* @see #createRelative(String)
* @see ResourceUtils#toRelativeURL(URL, String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,17 @@ protected Resource convertClassLoaderURL(URL url) {
}
}
else {
String urlString = url.toString();
String cleanedPath = StringUtils.cleanPath(urlString);
if (!cleanedPath.equals(urlString)) {
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
try {
return new UrlResource(ResourceUtils.toURI(cleanedPath));
}
catch (URISyntaxException | MalformedURLException ex) {
// Fallback to regular URL construction below...
}
}
return new UrlResource(url);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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 Down Expand Up @@ -34,7 +34,7 @@
* <p>Consider using Spring's Resource abstraction in the core package
* for handling all kinds of file resources in a uniform manner.
* {@link org.springframework.core.io.ResourceLoader}'s {@code getResource()}
* method can resolve any location to an {@link org.springframework.core.io.Resource}
* method can resolve any location to a {@link org.springframework.core.io.Resource}
* object, which in turn allows one to obtain a {@code java.io.File} in the
* file system through its {@code getFile()} method.
*
Expand Down Expand Up @@ -101,6 +101,7 @@ public abstract class ResourceUtils {
* @return whether the location qualifies as a URL
* @see #CLASSPATH_URL_PREFIX
* @see java.net.URL
* @see #toURL(String)
*/
public static boolean isUrl(@Nullable String resourceLocation) {
if (resourceLocation == null) {
Expand All @@ -126,6 +127,7 @@ public static boolean isUrl(@Nullable String resourceLocation) {
* "classpath:" pseudo URL, a "file:" URL, or a plain file path
* @return a corresponding URL object
* @throws FileNotFoundException if the resource cannot be resolved to a URL
* @see #toURL(String)
*/
public static URL getURL(String resourceLocation) throws FileNotFoundException {
Assert.notNull(resourceLocation, "Resource location must not be null");
Expand Down Expand Up @@ -166,6 +168,7 @@ public static URL getURL(String resourceLocation) throws FileNotFoundException {
* @return a corresponding File object
* @throws FileNotFoundException if the resource cannot be resolved to
* a file in the file system
* @see #getFile(URL)
*/
public static File getFile(String resourceLocation) throws FileNotFoundException {
Assert.notNull(resourceLocation, "Resource location must not be null");
Expand Down Expand Up @@ -197,6 +200,7 @@ public static File getFile(String resourceLocation) throws FileNotFoundException
* @return a corresponding File object
* @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system
* @see #getFile(URL, String)
*/
public static File getFile(URL resourceUrl) throws FileNotFoundException {
return getFile(resourceUrl, "URL");
Expand Down Expand Up @@ -237,6 +241,7 @@ public static File getFile(URL resourceUrl, String description) throws FileNotFo
* @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system
* @since 2.5
* @see #getFile(URI, String)
*/
public static File getFile(URI resourceUri) throws FileNotFoundException {
return getFile(resourceUri, "URI");
Expand Down Expand Up @@ -268,6 +273,7 @@ public static File getFile(URI resourceUri, String description) throws FileNotFo
* i.e. has protocol "file", "vfsfile" or "vfs".
* @param url the URL to check
* @return whether the URL has been identified as a file system URL
* @see #isJarURL(URL)
*/
public static boolean isFileURL(URL url) {
String protocol = url.getProtocol();
Expand All @@ -281,6 +287,7 @@ public static boolean isFileURL(URL url) {
* "vfszip", or "wsjar".
* @param url the URL to check
* @return whether the URL has been identified as a JAR URL
* @see #isJarFileURL(URL)
*/
public static boolean isJarURL(URL url) {
String protocol = url.getProtocol();
Expand All @@ -295,6 +302,7 @@ public static boolean isJarURL(URL url) {
* @param url the URL to check
* @return whether the URL has been identified as a JAR file URL
* @since 4.1
* @see #extractJarFileURL(URL)
*/
public static boolean isJarFileURL(URL url) {
return (URL_PROTOCOL_FILE.equals(url.getProtocol()) &&
Expand All @@ -307,6 +315,7 @@ public static boolean isJarFileURL(URL url) {
* @param jarUrl the original URL
* @return the URL for the actual jar file
* @throws MalformedURLException if no valid jar file URL could be extracted
* @see #extractArchiveURL(URL)
*/
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {
String urlFile = jarUrl.getFile();
Expand Down Expand Up @@ -368,6 +377,7 @@ public static URL extractArchiveURL(URL jarUrl) throws MalformedURLException {
* @return the URI instance
* @throws URISyntaxException if the URL wasn't a valid URI
* @see java.net.URL#toURI()
* @see #toURI(String)
*/
public static URI toURI(URL url) throws URISyntaxException {
return toURI(url.toString());
Expand All @@ -379,18 +389,21 @@ public static URI toURI(URL url) throws URISyntaxException {
* @param location the location String to convert into a URI instance
* @return the URI instance
* @throws URISyntaxException if the location wasn't a valid URI
* @see #toURI(URL)
*/
public static URI toURI(String location) throws URISyntaxException {
return new URI(StringUtils.replace(location, " ", "%20"));
}

/**
* Create a URL instance for the given location String,
* Create a clean URL instance for the given location String,
* going through URI construction and then URL conversion.
* @param location the location String to convert into a URL instance
* @return the URL instance
* @throws MalformedURLException if the location wasn't a valid URL
* @since 6.0
* @see java.net.URI#toURL()
* @see #toURI(String)
*/
@SuppressWarnings("deprecation") // on JDK 20
public static URL toURL(String location) throws MalformedURLException {
Expand All @@ -406,13 +419,15 @@ public static URL toURL(String location) throws MalformedURLException {
}

/**
* Create a URL instance for the given root URL and relative path,
* Create a clean URL instance for the given root URL and relative path,
* going through URI construction and then URL conversion.
* @param root the root URL to start from
* @param relativePath the relative path to apply
* @return the relative URL instance
* @throws MalformedURLException if the end result is not a valid URL
* @since 6.0
* @see #toURL(String)
* @see StringUtils#applyRelativePath
*/
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException {
// # can appear in filenames, java.net.URL should not treat it as a fragment
Expand All @@ -426,6 +441,7 @@ public static URL toRelativeURL(URL root, String relativePath) throws MalformedU
* given connection, preferring {@code false} but leaving the flag at
* its JVM default value for jar resources (typically {@code true}).
* @param con the URLConnection to set the flag on
* @see URLConnection#setUseCaches
*/
public static void useCachesIfNecessary(URLConnection con) {
if (!(con instanceof JarURLConnection)) {
Expand Down

0 comments on commit 7d8279a

Please sign in to comment.