Skip to content

Commit

Permalink
Avoid unnecessary computation of cleaned URL
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller authored and xcl(徐程林) committed Aug 16, 2020
1 parent 480fb26 commit 617c608
Showing 1 changed file with 23 additions and 10 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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 @@ -56,7 +56,8 @@ public class UrlResource extends AbstractFileResolvingResource {
/**
* Cleaned URL (with normalized path), used for comparisons.
*/
private final URL cleanedUrl;
@Nullable
private volatile URL cleanedUrl;


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

/**
Expand All @@ -78,9 +78,8 @@ public UrlResource(URI uri) throws MalformedURLException {
*/
public UrlResource(URL url) {
Assert.notNull(url, "URL must not be null");
this.url = url;
this.cleanedUrl = getCleanedUrl(this.url, url.toString());
this.uri = null;
this.url = url;
}

/**
Expand Down Expand Up @@ -127,7 +126,6 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
try {
this.uri = new URI(protocol, location, fragment);
this.url = this.uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, this.uri.toString());
}
catch (URISyntaxException ex) {
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
Expand All @@ -144,7 +142,7 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
* @return the cleaned URL (possibly the original URL as-is)
* @see org.springframework.util.StringUtils#cleanPath
*/
private URL getCleanedUrl(URL originalUrl, String originalPath) {
private static URL getCleanedUrl(URL originalUrl, String originalPath) {
String cleanedPath = StringUtils.cleanPath(originalPath);
if (!cleanedPath.equals(originalPath)) {
try {
Expand All @@ -157,6 +155,21 @@ private URL getCleanedUrl(URL originalUrl, String originalPath) {
return originalUrl;
}

/**
* Lazily determine a cleaned URL for the given original URL.
* @see #getCleanedUrl(URL, String)
*/
private URL getCleanedUrl() {
URL cleanedUrl = this.cleanedUrl;
if (cleanedUrl != null) {
return cleanedUrl;
}
cleanedUrl = getCleanedUrl(this.url, (this.uri != null ? this.uri : this.url).toString());
this.cleanedUrl = cleanedUrl;
return cleanedUrl;
}


/**
* This implementation opens an InputStream for the given URL.
* <p>It sets the {@code useCaches} flag to {@code false},
Expand Down Expand Up @@ -262,7 +275,7 @@ protected URL createRelativeURL(String relativePath) throws MalformedURLExceptio
*/
@Override
public String getFilename() {
return StringUtils.getFilename(this.cleanedUrl.getPath());
return StringUtils.getFilename(getCleanedUrl().getPath());
}

/**
Expand All @@ -280,15 +293,15 @@ public String getDescription() {
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof UrlResource &&
this.cleanedUrl.equals(((UrlResource) other).cleanedUrl)));
getCleanedUrl().equals(((UrlResource) other).getCleanedUrl())));
}

/**
* This implementation returns the hash code of the underlying URL reference.
*/
@Override
public int hashCode() {
return this.cleanedUrl.hashCode();
return getCleanedUrl().hashCode();
}

}

0 comments on commit 617c608

Please sign in to comment.