Skip to content

Commit 6fd476e

Browse files
committed
extracted ResourceUtils.useCachesIfNecessary(URLConnection) method (SPR-9117)
1 parent ab98f28 commit 6fd476e

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public boolean exists() {
9595
else {
9696
// Try a URL connection content-length header...
9797
URLConnection con = url.openConnection();
98-
con.setUseCaches(false);
98+
ResourceUtils.useCachesIfNecessary(con);
9999
HttpURLConnection httpCon =
100100
(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
101101
if (httpCon != null) {
@@ -157,7 +157,7 @@ public long contentLength() throws IOException {
157157
else {
158158
// Try a URL connection content-length header...
159159
URLConnection con = url.openConnection();
160-
con.setUseCaches(false);
160+
ResourceUtils.useCachesIfNecessary(con);
161161
if (con instanceof HttpURLConnection) {
162162
((HttpURLConnection) con).setRequestMethod("HEAD");
163163
}
@@ -175,7 +175,7 @@ public long lastModified() throws IOException {
175175
else {
176176
// Try a URL connection last-modified header...
177177
URLConnection con = url.openConnection();
178-
con.setUseCaches(false);
178+
ResourceUtils.useCachesIfNecessary(con);
179179
if (con instanceof HttpURLConnection) {
180180
((HttpURLConnection) con).setRequestMethod("HEAD");
181181
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -26,6 +26,7 @@
2626
import java.net.URLConnection;
2727

2828
import org.springframework.util.Assert;
29+
import org.springframework.util.ResourceUtils;
2930
import org.springframework.util.StringUtils;
3031

3132
/**
@@ -119,7 +120,7 @@ private URL getCleanedUrl(URL originalUrl, String originalPath) {
119120
*/
120121
public InputStream getInputStream() throws IOException {
121122
URLConnection con = this.url.openConnection();
122-
con.setUseCaches(false);
123+
ResourceUtils.useCachesIfNecessary(con);
123124
try {
124125
return con.getInputStream();
125126
}

org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
433433
if (con instanceof JarURLConnection) {
434434
// Should usually be the case for traditional JAR files.
435435
JarURLConnection jarCon = (JarURLConnection) con;
436-
jarCon.setUseCaches(jarCon.getClass().getName().startsWith("JNLP"));
436+
ResourceUtils.useCachesIfNecessary(jarCon);
437437
jarFile = jarCon.getJarFile();
438438
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
439439
JarEntry jarEntry = jarCon.getJarEntry();

org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -26,6 +26,7 @@
2626
import org.springframework.core.io.Resource;
2727
import org.springframework.util.Assert;
2828
import org.springframework.util.ClassUtils;
29+
import org.springframework.util.ResourceUtils;
2930

3031
/**
3132
* Convenient utility methods for loading of <code>java.util.Properties</code>,
@@ -106,7 +107,7 @@ public static Properties loadAllProperties(String resourceName, ClassLoader clas
106107
InputStream is = null;
107108
try {
108109
URLConnection con = url.openConnection();
109-
con.setUseCaches(false);
110+
ResourceUtils.useCachesIfNecessary(con);
110111
is = con.getInputStream();
111112
properties.load(is);
112113
}

org.springframework.core/src/main/java/org/springframework/util/ResourceUtils.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -22,6 +22,7 @@
2222
import java.net.URI;
2323
import java.net.URISyntaxException;
2424
import java.net.URL;
25+
import java.net.URLConnection;
2526

2627
/**
2728
* Utility methods for resolving resource locations to files in the
@@ -328,4 +329,14 @@ public static URI toURI(String location) throws URISyntaxException {
328329
return new URI(StringUtils.replace(location, " ", "%20"));
329330
}
330331

332+
/**
333+
* Set the {@link URLConnection#setUseCaches "useCaches"} flag on the
334+
* given connection, preferring <code>false</code> but leaving the
335+
* flag at <code>true</code> for JNLP based resources.
336+
* @param con the URLConnection to set the flag on
337+
*/
338+
public static void useCachesIfNecessary(URLConnection con) {
339+
con.setUseCaches(con.getClass().getName().startsWith("JNLP"));
340+
}
341+
331342
}

0 commit comments

Comments
 (0)