1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -56,7 +56,8 @@ public class UrlResource extends AbstractFileResolvingResource {
56
56
/**
57
57
* Cleaned URL (with normalized path), used for comparisons.
58
58
*/
59
- private final URL cleanedUrl ;
59
+ @ Nullable
60
+ private volatile URL cleanedUrl ;
60
61
61
62
62
63
/**
@@ -69,7 +70,6 @@ public UrlResource(URI uri) throws MalformedURLException {
69
70
Assert .notNull (uri , "URI must not be null" );
70
71
this .uri = uri ;
71
72
this .url = uri .toURL ();
72
- this .cleanedUrl = getCleanedUrl (this .url , uri .toString ());
73
73
}
74
74
75
75
/**
@@ -78,9 +78,8 @@ public UrlResource(URI uri) throws MalformedURLException {
78
78
*/
79
79
public UrlResource (URL url ) {
80
80
Assert .notNull (url , "URL must not be null" );
81
- this .url = url ;
82
- this .cleanedUrl = getCleanedUrl (this .url , url .toString ());
83
81
this .uri = null ;
82
+ this .url = url ;
84
83
}
85
84
86
85
/**
@@ -127,7 +126,6 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
127
126
try {
128
127
this .uri = new URI (protocol , location , fragment );
129
128
this .url = this .uri .toURL ();
130
- this .cleanedUrl = getCleanedUrl (this .url , this .uri .toString ());
131
129
}
132
130
catch (URISyntaxException ex ) {
133
131
MalformedURLException exToThrow = new MalformedURLException (ex .getMessage ());
@@ -144,17 +142,34 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
144
142
* @return the cleaned URL
145
143
* @see org.springframework.util.StringUtils#cleanPath
146
144
*/
147
- private URL getCleanedUrl (URL originalUrl , String originalPath ) {
148
- try {
149
- return new URL (StringUtils .cleanPath (originalPath ));
145
+ private static URL getCleanedUrl (URL originalUrl , String originalPath ) {
146
+ String cleanedPath = StringUtils .cleanPath (originalPath );
147
+ if (!cleanedPath .equals (originalPath )) {
148
+ try {
149
+ return new URL (cleanedPath );
150
+ }
151
+ catch (MalformedURLException ex ) {
152
+ // Cleaned URL path cannot be converted to URL -> take original URL.
153
+ }
150
154
}
151
- catch (MalformedURLException ex ) {
152
- // Cleaned URL path cannot be converted to URL
153
- // -> take original URL.
154
- return originalUrl ;
155
+ return originalUrl ;
156
+ }
157
+
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 ;
155
166
}
167
+ cleanedUrl = getCleanedUrl (this .url , (this .uri != null ? this .uri : this .url ).toString ());
168
+ this .cleanedUrl = cleanedUrl ;
169
+ return cleanedUrl ;
156
170
}
157
171
172
+
158
173
/**
159
174
* This implementation opens an InputStream for the given URL.
160
175
* <p>It sets the {@code useCaches} flag to {@code false},
@@ -245,7 +260,7 @@ public Resource createRelative(String relativePath) throws MalformedURLException
245
260
*/
246
261
@ Override
247
262
public String getFilename () {
248
- return StringUtils .getFilename (this . cleanedUrl .getPath ());
263
+ return StringUtils .getFilename (getCleanedUrl () .getPath ());
249
264
}
250
265
251
266
/**
@@ -263,15 +278,15 @@ public String getDescription() {
263
278
@ Override
264
279
public boolean equals (Object other ) {
265
280
return (this == other || (other instanceof UrlResource &&
266
- this . cleanedUrl . equals (((UrlResource ) other ).cleanedUrl )));
281
+ getCleanedUrl (). equals (((UrlResource ) other ).getCleanedUrl () )));
267
282
}
268
283
269
284
/**
270
285
* This implementation returns the hash code of the underlying URL reference.
271
286
*/
272
287
@ Override
273
288
public int hashCode () {
274
- return this . cleanedUrl .hashCode ();
289
+ return getCleanedUrl () .hashCode ();
275
290
}
276
291
277
292
}
0 commit comments