Skip to content

Commit 96a4e11

Browse files
committed
Avoid unnecessary computation of cleaned URL
Closes gh-25531
1 parent 6acbc50 commit 96a4e11

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,8 @@ public class UrlResource extends AbstractFileResolvingResource {
5656
/**
5757
* Cleaned URL (with normalized path), used for comparisons.
5858
*/
59-
private final URL cleanedUrl;
59+
@Nullable
60+
private volatile URL cleanedUrl;
6061

6162

6263
/**
@@ -69,7 +70,6 @@ public UrlResource(URI uri) throws MalformedURLException {
6970
Assert.notNull(uri, "URI must not be null");
7071
this.uri = uri;
7172
this.url = uri.toURL();
72-
this.cleanedUrl = getCleanedUrl(this.url, uri.toString());
7373
}
7474

7575
/**
@@ -78,9 +78,8 @@ public UrlResource(URI uri) throws MalformedURLException {
7878
*/
7979
public UrlResource(URL url) {
8080
Assert.notNull(url, "URL must not be null");
81-
this.url = url;
82-
this.cleanedUrl = getCleanedUrl(this.url, url.toString());
8381
this.uri = null;
82+
this.url = url;
8483
}
8584

8685
/**
@@ -127,7 +126,6 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
127126
try {
128127
this.uri = new URI(protocol, location, fragment);
129128
this.url = this.uri.toURL();
130-
this.cleanedUrl = getCleanedUrl(this.url, this.uri.toString());
131129
}
132130
catch (URISyntaxException ex) {
133131
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
@@ -144,7 +142,7 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
144142
* @return the cleaned URL (possibly the original URL as-is)
145143
* @see org.springframework.util.StringUtils#cleanPath
146144
*/
147-
private URL getCleanedUrl(URL originalUrl, String originalPath) {
145+
private static URL getCleanedUrl(URL originalUrl, String originalPath) {
148146
String cleanedPath = StringUtils.cleanPath(originalPath);
149147
if (!cleanedPath.equals(originalPath)) {
150148
try {
@@ -157,6 +155,21 @@ private URL getCleanedUrl(URL originalUrl, String originalPath) {
157155
return originalUrl;
158156
}
159157

158+
/**
159+
* Lazily determine a cleaned URL for the given original URL.
160+
* @see #getCleanedUrl(URL, String)
161+
*/
162+
private URL getCleanedUrl() {
163+
URL cleanedUrl = this.cleanedUrl;
164+
if (cleanedUrl != null) {
165+
return cleanedUrl;
166+
}
167+
cleanedUrl = getCleanedUrl(this.url, (this.uri != null ? this.uri : this.url).toString());
168+
this.cleanedUrl = cleanedUrl;
169+
return cleanedUrl;
170+
}
171+
172+
160173
/**
161174
* This implementation opens an InputStream for the given URL.
162175
* <p>It sets the {@code useCaches} flag to {@code false},
@@ -262,7 +275,7 @@ protected URL createRelativeURL(String relativePath) throws MalformedURLExceptio
262275
*/
263276
@Override
264277
public String getFilename() {
265-
return StringUtils.getFilename(this.cleanedUrl.getPath());
278+
return StringUtils.getFilename(getCleanedUrl().getPath());
266279
}
267280

268281
/**
@@ -280,15 +293,15 @@ public String getDescription() {
280293
@Override
281294
public boolean equals(@Nullable Object other) {
282295
return (this == other || (other instanceof UrlResource &&
283-
this.cleanedUrl.equals(((UrlResource) other).cleanedUrl)));
296+
getCleanedUrl().equals(((UrlResource) other).getCleanedUrl())));
284297
}
285298

286299
/**
287300
* This implementation returns the hash code of the underlying URL reference.
288301
*/
289302
@Override
290303
public int hashCode() {
291-
return this.cleanedUrl.hashCode();
304+
return getCleanedUrl().hashCode();
292305
}
293306

294307
}

0 commit comments

Comments
 (0)