Skip to content

Commit d298561

Browse files
committed
Proper resolution of Tomcat war URL
Issue: SPR-15485 (cherry picked from commit d43dfc7)
1 parent beac891 commit d298561

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
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.
@@ -17,6 +17,7 @@
1717
package org.springframework.core.io;
1818

1919
import java.io.File;
20+
import java.io.FileNotFoundException;
2021
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.net.HttpURLConnection;
@@ -89,11 +90,11 @@ public boolean exists() {
8990
try {
9091
URL url = getURL();
9192
if (ResourceUtils.isFileURL(url)) {
92-
// Proceed with file system resolution...
93+
// Proceed with file system resolution
9394
return getFile().exists();
9495
}
9596
else {
96-
// Try a URL connection content-length header...
97+
// Try a URL connection content-length header
9798
URLConnection con = url.openConnection();
9899
customizeConnection(con);
99100
HttpURLConnection httpCon =
@@ -133,7 +134,7 @@ public boolean isReadable() {
133134
try {
134135
URL url = getURL();
135136
if (ResourceUtils.isFileURL(url)) {
136-
// Proceed with file system resolution...
137+
// Proceed with file system resolution
137138
File file = getFile();
138139
return (file.canRead() && !file.isDirectory());
139140
}
@@ -150,11 +151,11 @@ public boolean isReadable() {
150151
public long contentLength() throws IOException {
151152
URL url = getURL();
152153
if (ResourceUtils.isFileURL(url)) {
153-
// Proceed with file system resolution...
154+
// Proceed with file system resolution
154155
return getFile().length();
155156
}
156157
else {
157-
// Try a URL connection content-length header...
158+
// Try a URL connection content-length header
158159
URLConnection con = url.openConnection();
159160
customizeConnection(con);
160161
return con.getContentLength();
@@ -165,15 +166,18 @@ public long contentLength() throws IOException {
165166
public long lastModified() throws IOException {
166167
URL url = getURL();
167168
if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
168-
// Proceed with file system resolution...
169-
return super.lastModified();
170-
}
171-
else {
172-
// Try a URL connection last-modified header...
173-
URLConnection con = url.openConnection();
174-
customizeConnection(con);
175-
return con.getLastModified();
169+
// Proceed with file system resolution
170+
try {
171+
return super.lastModified();
172+
}
173+
catch (FileNotFoundException ex) {
174+
// Defensively fall back to URL connection check instead
175+
}
176176
}
177+
// Try a URL connection last-modified header
178+
URLConnection con = url.openConnection();
179+
customizeConnection(con);
180+
return con.getLastModified();
177181
}
178182

179183

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

Lines changed: 4 additions & 4 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.
@@ -119,7 +119,6 @@ public File getFile() throws IOException {
119119
* content length. Subclasses will almost always be able to provide
120120
* a more optimal version of this, e.g. checking a File length.
121121
* @see #getInputStream()
122-
* @throws IllegalStateException if {@link #getInputStream()} returns null.
123122
*/
124123
@Override
125124
public long contentLength() throws IOException {
@@ -162,8 +161,9 @@ public long lastModified() throws IOException {
162161
* Determine the File to use for timestamp checking.
163162
* <p>The default implementation delegates to {@link #getFile()}.
164163
* @return the File to use for timestamp checking (never {@code null})
165-
* @throws IOException if the resource cannot be resolved as absolute
166-
* file path, i.e. if the resource is not available in a file system
164+
* @throws FileNotFoundException if the resource cannot be resolved as
165+
* an absolute file path, i.e. is not available in a file system
166+
* @throws IOException in case of general resolution/reading failures
167167
*/
168168
protected File getFileForLastModifiedCheck() throws IOException {
169169
return getFile();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,13 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
583583
// We'll also handle paths with and without leading "file:" prefix.
584584
String urlFile = rootDirURL.getFile();
585585
try {
586-
int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
586+
int separatorIndex = urlFile.indexOf(ResourceUtils.WAR_URL_SEPARATOR);
587+
if (separatorIndex == -1) {
588+
separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
589+
}
587590
if (separatorIndex != -1) {
588591
jarFileUrl = urlFile.substring(0, separatorIndex);
589-
rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
592+
rootEntryPath = urlFile.substring(separatorIndex + 2); // both separators are 2 chars
590593
jarFile = getJarFile(jarFileUrl);
591594
}
592595
else {

0 commit comments

Comments
 (0)