Skip to content

Commit f532607

Browse files
committed
Introduced customizeConnection callbacks for URLConnection used by exists() / contentLength() / lastModified()
Issue: SPR-11320 (cherry picked from commit 4f45ad5)
1 parent 16bf501 commit f532607

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -95,11 +95,10 @@ public boolean exists() {
9595
else {
9696
// Try a URL connection content-length header...
9797
URLConnection con = url.openConnection();
98-
ResourceUtils.useCachesIfNecessary(con);
98+
customizeConnection(con);
9999
HttpURLConnection httpCon =
100100
(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
101101
if (httpCon != null) {
102-
httpCon.setRequestMethod("HEAD");
103102
int code = httpCon.getResponseCode();
104103
if (code == HttpURLConnection.HTTP_OK) {
105104
return true;
@@ -157,10 +156,7 @@ public long contentLength() throws IOException {
157156
else {
158157
// Try a URL connection content-length header...
159158
URLConnection con = url.openConnection();
160-
ResourceUtils.useCachesIfNecessary(con);
161-
if (con instanceof HttpURLConnection) {
162-
((HttpURLConnection) con).setRequestMethod("HEAD");
163-
}
159+
customizeConnection(con);
164160
return con.getContentLength();
165161
}
166162
}
@@ -175,15 +171,40 @@ public long lastModified() throws IOException {
175171
else {
176172
// Try a URL connection last-modified header...
177173
URLConnection con = url.openConnection();
178-
ResourceUtils.useCachesIfNecessary(con);
179-
if (con instanceof HttpURLConnection) {
180-
((HttpURLConnection) con).setRequestMethod("HEAD");
181-
}
174+
customizeConnection(con);
182175
return con.getLastModified();
183176
}
184177
}
185178

186179

180+
/**
181+
* Customize the given {@link URLConnection}, obtained in the course of an
182+
* {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
183+
* <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
184+
* delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
185+
* Can be overridden in subclasses.
186+
* @param con the URLConnection to customize
187+
* @throws IOException if thrown from URLConnection methods
188+
*/
189+
protected void customizeConnection(URLConnection con) throws IOException {
190+
ResourceUtils.useCachesIfNecessary(con);
191+
if (con instanceof HttpURLConnection) {
192+
customizeConnection((HttpURLConnection) con);
193+
}
194+
}
195+
196+
/**
197+
* Customize the given {@link HttpURLConnection}, obtained in the course of an
198+
* {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
199+
* <p>Sets request method "HEAD" by default. Can be overridden in subclasses.
200+
* @param con the HttpURLConnection to customize
201+
* @throws IOException if thrown from HttpURLConnection methods
202+
*/
203+
protected void customizeConnection(HttpURLConnection con) throws IOException {
204+
con.setRequestMethod("HEAD");
205+
}
206+
207+
187208
/**
188209
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
189210
*/

0 commit comments

Comments
 (0)