Skip to content

Commit 302a069

Browse files
committed
AbstractFileResolvingResource uses extractArchiveURL for last-modified check
Issue: SPR-13393
1 parent c33c26a commit 302a069

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -60,7 +60,7 @@ public File getFile() throws IOException {
6060
protected File getFileForLastModifiedCheck() throws IOException {
6161
URL url = getURL();
6262
if (ResourceUtils.isJarURL(url)) {
63-
URL actualUrl = ResourceUtils.extractJarFileURL(url);
63+
URL actualUrl = ResourceUtils.extractArchiveURL(url);
6464
if (actualUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
6565
return VfsResourceDelegate.getResource(actualUrl).getFile();
6666
}

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -57,9 +57,12 @@ public abstract class ResourceUtils {
5757
/** URL prefix for loading from the file system: "file:" */
5858
public static final String FILE_URL_PREFIX = "file:";
5959

60-
/** URL prefix for loading from the file system: "jar:" */
60+
/** URL prefix for loading from a jar file: "jar:" */
6161
public static final String JAR_URL_PREFIX = "jar:";
6262

63+
/** URL prefix for loading from a war file on Tomcat: "war:" */
64+
public static final String WAR_URL_PREFIX = "war:";
65+
6366
/** URL protocol for a file in the file system: "file" */
6467
public static final String URL_PROTOCOL_FILE = "file";
6568

@@ -87,6 +90,9 @@ public abstract class ResourceUtils {
8790
/** Separator between JAR URL and file path within the JAR: "!/" */
8891
public static final String JAR_URL_SEPARATOR = "!/";
8992

93+
/** Special separator between WAR URL and jar part on Tomcat */
94+
public static final String WAR_URL_SEPARATOR = "*/";
95+
9096

9197
/**
9298
* Return whether the given resource location is a URL:
@@ -319,6 +325,34 @@ public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {
319325
}
320326
}
321327

328+
/**
329+
* Extract the URL for the outermost archive from the given jar/war URL
330+
* (which may point to a resource in a jar file or to a jar file itself).
331+
* <p>In the case of a jar file nested within a war file, this will return
332+
* a URL to the war file since that is the one resolvable in the file system.
333+
* @param jarUrl the original URL
334+
* @return the URL for the actual jar file
335+
* @throws MalformedURLException if no valid jar file URL could be extracted
336+
* @since 4.1.8
337+
* @see #extractJarFileURL(URL)
338+
*/
339+
public static URL extractArchiveURL(URL jarUrl) throws MalformedURLException {
340+
String urlFile = jarUrl.getFile();
341+
342+
int endIndex = urlFile.indexOf(WAR_URL_SEPARATOR);
343+
if (endIndex != -1) {
344+
// Tomcat's "jar:war:file:...mywar.war*/WEB-INF/lib/myjar.jar!/myentry.txt"
345+
String warFile = urlFile.substring(0, endIndex);
346+
int startIndex = warFile.indexOf(WAR_URL_PREFIX);
347+
if (startIndex != -1) {
348+
return new URL(warFile.substring(startIndex + WAR_URL_PREFIX.length()));
349+
}
350+
}
351+
352+
// Regular "jar:file:...myjar.jar!/myentry.txt"
353+
return extractJarFileURL(jarUrl);
354+
}
355+
322356
/**
323357
* Create a URI instance for the given URL,
324358
* replacing spaces with "%20" URI encoding first.

spring-core/src/test/java/org/springframework/util/ResourceUtilsTests.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -21,22 +21,27 @@
2121
import java.net.URLConnection;
2222
import java.net.URLStreamHandler;
2323

24-
import junit.framework.TestCase;
24+
import org.junit.Test;
25+
26+
import static org.junit.Assert.*;
2527

2628
/**
2729
* @author Juergen Hoeller
2830
*/
29-
public class ResourceUtilsTests extends TestCase {
31+
public class ResourceUtilsTests {
3032

31-
public void testIsJarURL() throws Exception {
33+
@Test
34+
public void isJarURL() throws Exception {
3235
assertTrue(ResourceUtils.isJarURL(new URL("jar:file:myjar.jar!/mypath")));
3336
assertTrue(ResourceUtils.isJarURL(new URL(null, "zip:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
3437
assertTrue(ResourceUtils.isJarURL(new URL(null, "wsjar:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
38+
assertTrue(ResourceUtils.isJarURL(new URL(null, "jar:war:file:mywar.war*/myjar.jar!/mypath", new DummyURLStreamHandler())));
3539
assertFalse(ResourceUtils.isJarURL(new URL("file:myjar.jar")));
3640
assertFalse(ResourceUtils.isJarURL(new URL("http:myserver/myjar.jar")));
3741
}
3842

39-
public void testExtractJarFileURL() throws Exception {
43+
@Test
44+
public void extractJarFileURL() throws Exception {
4045
assertEquals(new URL("file:myjar.jar"),
4146
ResourceUtils.extractJarFileURL(new URL("jar:file:myjar.jar!/mypath")));
4247
assertEquals(new URL("file:/myjar.jar"),
@@ -45,14 +50,40 @@ public void testExtractJarFileURL() throws Exception {
4550
ResourceUtils.extractJarFileURL(new URL(null, "zip:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
4651
assertEquals(new URL("file:myjar.jar"),
4752
ResourceUtils.extractJarFileURL(new URL(null, "wsjar:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
53+
54+
assertEquals(new URL("file:myjar.jar"),
55+
ResourceUtils.extractJarFileURL(new URL("file:myjar.jar")));
4856
assertEquals(new URL("file:myjar.jar"),
4957
ResourceUtils.extractJarFileURL(new URL("jar:file:myjar.jar!/")));
5058
assertEquals(new URL("file:myjar.jar"),
5159
ResourceUtils.extractJarFileURL(new URL(null, "zip:file:myjar.jar!/", new DummyURLStreamHandler())));
5260
assertEquals(new URL("file:myjar.jar"),
5361
ResourceUtils.extractJarFileURL(new URL(null, "wsjar:file:myjar.jar!/", new DummyURLStreamHandler())));
62+
}
63+
64+
@Test
65+
public void extractArchiveURL() throws Exception {
5466
assertEquals(new URL("file:myjar.jar"),
55-
ResourceUtils.extractJarFileURL(new URL("file:myjar.jar")));
67+
ResourceUtils.extractArchiveURL(new URL("jar:file:myjar.jar!/mypath")));
68+
assertEquals(new URL("file:/myjar.jar"),
69+
ResourceUtils.extractArchiveURL(new URL(null, "jar:myjar.jar!/mypath", new DummyURLStreamHandler())));
70+
assertEquals(new URL("file:myjar.jar"),
71+
ResourceUtils.extractArchiveURL(new URL(null, "zip:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
72+
assertEquals(new URL("file:myjar.jar"),
73+
ResourceUtils.extractArchiveURL(new URL(null, "wsjar:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
74+
assertEquals(new URL("file:mywar.war"),
75+
ResourceUtils.extractArchiveURL(new URL(null, "jar:war:file:mywar.war*/myjar.jar!/mypath", new DummyURLStreamHandler())));
76+
77+
assertEquals(new URL("file:myjar.jar"),
78+
ResourceUtils.extractArchiveURL(new URL("file:myjar.jar")));
79+
assertEquals(new URL("file:myjar.jar"),
80+
ResourceUtils.extractArchiveURL(new URL("jar:file:myjar.jar!/")));
81+
assertEquals(new URL("file:myjar.jar"),
82+
ResourceUtils.extractArchiveURL(new URL(null, "zip:file:myjar.jar!/", new DummyURLStreamHandler())));
83+
assertEquals(new URL("file:myjar.jar"),
84+
ResourceUtils.extractArchiveURL(new URL(null, "wsjar:file:myjar.jar!/", new DummyURLStreamHandler())));
85+
assertEquals(new URL("file:mywar.war"),
86+
ResourceUtils.extractArchiveURL(new URL(null, "jar:war:file:mywar.war*/myjar.jar!/", new DummyURLStreamHandler())));
5687
}
5788

5889

0 commit comments

Comments
 (0)