Skip to content

Commit 155bbf5

Browse files
committed
ResourceUtils supports Tomcat's "war:jar:" URL format
Issue: SPR-13393
1 parent b15f404 commit 155bbf5

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

Lines changed: 10 additions & 5 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.
@@ -69,6 +69,9 @@ public abstract class ResourceUtils {
6969
/** URL protocol for an entry from a zip file: "zip" */
7070
public static final String URL_PROTOCOL_ZIP = "zip";
7171

72+
/** URL protocol for an entry from a Tomcat war file: "war" */
73+
public static final String URL_PROTOCOL_WAR = "war";
74+
7275
/** URL protocol for an entry from a WebSphere jar file: "wsjar" */
7376
public static final String URL_PROTOCOL_WSJAR = "wsjar";
7477

@@ -275,7 +278,8 @@ public static boolean isFileURL(URL url) {
275278
public static boolean isJarURL(URL url) {
276279
String protocol = url.getProtocol();
277280
return (URL_PROTOCOL_JAR.equals(protocol) || URL_PROTOCOL_ZIP.equals(protocol) ||
278-
URL_PROTOCOL_VFSZIP.equals(protocol) || URL_PROTOCOL_WSJAR.equals(protocol));
281+
URL_PROTOCOL_WAR.equals(protocol) || URL_PROTOCOL_WSJAR.equals(protocol) ||
282+
URL_PROTOCOL_VFSZIP.equals(protocol));
279283
}
280284

281285
/**
@@ -299,9 +303,10 @@ public static boolean isJarFileURL(URL url) {
299303
*/
300304
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {
301305
String urlFile = jarUrl.getFile();
302-
int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR);
303-
if (separatorIndex != -1) {
304-
String jarFile = urlFile.substring(0, separatorIndex);
306+
int startIndex = (urlFile.startsWith(JAR_URL_PREFIX) ? JAR_URL_PREFIX.length() : 0);
307+
int endIndex = urlFile.indexOf(JAR_URL_SEPARATOR);
308+
if (endIndex != -1) {
309+
String jarFile = urlFile.substring(startIndex, endIndex);
305310
try {
306311
return new URL(jarFile);
307312
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void isJarURL() throws Exception {
3535
assertTrue(ResourceUtils.isJarURL(new URL("jar:file:myjar.jar!/mypath")));
3636
assertTrue(ResourceUtils.isJarURL(new URL(null, "zip:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
3737
assertTrue(ResourceUtils.isJarURL(new URL(null, "wsjar:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
38+
assertTrue(ResourceUtils.isJarURL(new URL(null, "war:jar:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
3839
assertFalse(ResourceUtils.isJarURL(new URL("file:myjar.jar")));
3940
assertFalse(ResourceUtils.isJarURL(new URL("http:myserver/myjar.jar")));
4041
}
@@ -49,12 +50,17 @@ public void extractJarFileURL() throws Exception {
4950
ResourceUtils.extractJarFileURL(new URL(null, "zip:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
5051
assertEquals(new URL("file:myjar.jar"),
5152
ResourceUtils.extractJarFileURL(new URL(null, "wsjar:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
53+
assertEquals(new URL("file:myjar.jar"),
54+
ResourceUtils.extractJarFileURL(new URL(null, "war:jar:file:myjar.jar!/mypath", new DummyURLStreamHandler())));
55+
5256
assertEquals(new URL("file:myjar.jar"),
5357
ResourceUtils.extractJarFileURL(new URL("jar:file:myjar.jar!/")));
5458
assertEquals(new URL("file:myjar.jar"),
5559
ResourceUtils.extractJarFileURL(new URL(null, "zip:file:myjar.jar!/", new DummyURLStreamHandler())));
5660
assertEquals(new URL("file:myjar.jar"),
5761
ResourceUtils.extractJarFileURL(new URL(null, "wsjar:file:myjar.jar!/", new DummyURLStreamHandler())));
62+
assertEquals(new URL("file:myjar.jar"),
63+
ResourceUtils.extractJarFileURL(new URL(null, "war:jar:file:myjar.jar!/", new DummyURLStreamHandler())));
5864
assertEquals(new URL("file:myjar.jar"),
5965
ResourceUtils.extractJarFileURL(new URL("file:myjar.jar")));
6066
}

0 commit comments

Comments
 (0)