Skip to content

Commit b80431d

Browse files
committed
Additional fix with test cases for BZ 69623
Calls to ClassLoader.getResource().getContent() failed when made from within a web application with resource caching enabled if the target resource was packaged in a JAR file. https://bz.apache.org/bugzilla/show_bug.cgi?id=69623
1 parent 78eeba0 commit b80431d

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

java/org/apache/catalina/webresources/CachedResource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,5 +663,10 @@ public JarFile getJarFile() throws IOException {
663663
return ((JarURLConnection) resourceURL.openConnection()).getJarFile();
664664
}
665665

666+
@Override
667+
public String getContentType() {
668+
// "content/unknown" is the value used by sun.net.www.URLConnection. It is used here for consistency.
669+
return Objects.requireNonNullElse(getResource().getMimeType(), "content/unknown");
670+
}
666671
}
667672
}

test/org/apache/catalina/loader/TestVirtualWebappLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void testStartInternal() throws Exception {
8686

8787
loader.start();
8888
String[] repos = loader.getLoaderRepositories();
89-
Assert.assertEquals(5, repos.length);
89+
Assert.assertEquals(6, repos.length);
9090
loader.stop();
9191

9292
repos = loader.getLoaderRepositories();
@@ -95,7 +95,7 @@ public void testStartInternal() throws Exception {
9595
// no leak
9696
loader.start();
9797
repos = loader.getLoaderRepositories();
98-
Assert.assertEquals(5, repos.length);
98+
Assert.assertEquals(6, repos.length);
9999

100100
// clear loader
101101
ctx.setLoader(null);

test/org/apache/catalina/webresources/TestCachedResource.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.catalina.webresources;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.File;
2021
import java.io.InputStream;
2122
import java.net.JarURLConnection;
@@ -131,4 +132,38 @@ public void testDirectoryListingsDir() throws Exception {
131132
Assert.assertNotNull(is);
132133
}
133134
}
135+
136+
137+
@Test
138+
public void testGetContentWebInfClasses() throws Exception {
139+
Tomcat tomcat = getTomcatInstance();
140+
File docBase = new File("test/webapp");
141+
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
142+
tomcat.start();
143+
144+
URL url = ctx.getLoader().getClassLoader().getResource("bug69623-a.mdd");
145+
Object o = url.getContent();
146+
/*
147+
* Could test the actual content but a non-null return without an exception is enough to demonstrate the bug has
148+
* not occurred.
149+
*/
150+
Assert.assertTrue(o instanceof ByteArrayInputStream);
151+
}
152+
153+
154+
@Test
155+
public void testGetContentWebInfLib() throws Exception {
156+
Tomcat tomcat = getTomcatInstance();
157+
File docBase = new File("test/webapp");
158+
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
159+
tomcat.start();
160+
161+
URL url = ctx.getLoader().getClassLoader().getResource("bug69623-b.mdd");
162+
Object o = url.getContent();
163+
/*
164+
* Could test the actual content but a non-null return without an exception is enough to demonstrate the bug has
165+
* not occurred.
166+
*/
167+
Assert.assertTrue(o instanceof ByteArrayInputStream);
168+
}
134169
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test file for https://bz.apache.org/bugzilla/show_bug.cgi?id=69623
517 Bytes
Binary file not shown.

webapps/docs/changelog.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@
191191
to <code>true</code>. (markt)
192192
</update>
193193
<!-- Entries for backport and removal before 12.0.0-M1 below this line -->
194+
<fix>
195+
<bug>69623</bug>: Additional fix for the long standing regression that
196+
meant that calls to <code>ClassLoader.getResource().getContent()</code>
197+
failed when made from within a web application with resource caching
198+
enabled if the target resource was packaged in a JAR file. (markt)
199+
</fix>
194200
</changelog>
195201
</subsection>
196202
<subsection name="Coyote">

0 commit comments

Comments
 (0)