Skip to content

Commit 3daf626

Browse files
committed
Defensive fallback for file system resolution in lastModified()
Issue: SPR-15485
1 parent 8b50f88 commit 3daf626

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.FileInputStream;
21+
import java.io.FileNotFoundException;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.net.HttpURLConnection;
@@ -139,11 +140,11 @@ public boolean exists() {
139140
try {
140141
URL url = getURL();
141142
if (ResourceUtils.isFileURL(url)) {
142-
// Proceed with file system resolution...
143+
// Proceed with file system resolution
143144
return getFile().exists();
144145
}
145146
else {
146-
// Try a URL connection content-length header...
147+
// Try a URL connection content-length header
147148
URLConnection con = url.openConnection();
148149
customizeConnection(con);
149150
HttpURLConnection httpCon =
@@ -183,7 +184,7 @@ public boolean isReadable() {
183184
try {
184185
URL url = getURL();
185186
if (ResourceUtils.isFileURL(url)) {
186-
// Proceed with file system resolution...
187+
// Proceed with file system resolution
187188
File file = getFile();
188189
return (file.canRead() && !file.isDirectory());
189190
}
@@ -200,11 +201,11 @@ public boolean isReadable() {
200201
public long contentLength() throws IOException {
201202
URL url = getURL();
202203
if (ResourceUtils.isFileURL(url)) {
203-
// Proceed with file system resolution...
204+
// Proceed with file system resolution
204205
return getFile().length();
205206
}
206207
else {
207-
// Try a URL connection content-length header...
208+
// Try a URL connection content-length header
208209
URLConnection con = url.openConnection();
209210
customizeConnection(con);
210211
return con.getContentLength();
@@ -215,15 +216,18 @@ public long contentLength() throws IOException {
215216
public long lastModified() throws IOException {
216217
URL url = getURL();
217218
if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
218-
// Proceed with file system resolution...
219-
return super.lastModified();
220-
}
221-
else {
222-
// Try a URL connection last-modified header...
223-
URLConnection con = url.openConnection();
224-
customizeConnection(con);
225-
return con.getLastModified();
219+
// Proceed with file system resolution
220+
try {
221+
return super.lastModified();
222+
}
223+
catch (FileNotFoundException ex) {
224+
// Defensively fall back to URL connection check instead
225+
}
226226
}
227+
// Try a URL connection last-modified header
228+
URLConnection con = url.openConnection();
229+
customizeConnection(con);
230+
return con.getLastModified();
227231
}
228232

229233

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -181,8 +181,9 @@ public long lastModified() throws IOException {
181181
* Determine the File to use for timestamp checking.
182182
* <p>The default implementation delegates to {@link #getFile()}.
183183
* @return the File to use for timestamp checking (never {@code null})
184-
* @throws IOException if the resource cannot be resolved as absolute
185-
* file path, i.e. if the resource is not available in a file system
184+
* @throws FileNotFoundException if the resource cannot be resolved as
185+
* an absolute file path, i.e. is not available in a file system
186+
* @throws IOException in case of general resolution/reading failures
186187
*/
187188
protected File getFileForLastModifiedCheck() throws IOException {
188189
return getFile();

0 commit comments

Comments
 (0)