Commit ed15345d authored by Dave Syer's avatar Dave Syer

Try to make Jetty scan TLDs in nested JARs

Jetty apparently does it differently (different version of
Jasper maybe), so you need a unique jarFileURL for each
nested JAR (previously they were all set to the parent
archive URL).

Also added the root of the main archive as a valid
document root.

For gh-367
parent 6631dd02
...@@ -21,6 +21,7 @@ import java.io.IOException; ...@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.jar.Manifest;
/** /**
* {@link java.net.JarURLConnection} used to support {@link JarFile#getUrl()}. * {@link java.net.JarURLConnection} used to support {@link JarFile#getUrl()}.
...@@ -43,6 +44,8 @@ class JarURLConnection extends java.net.JarURLConnection { ...@@ -43,6 +44,8 @@ class JarURLConnection extends java.net.JarURLConnection {
private String contentType; private String contentType;
private URL jarFileUrl;
protected JarURLConnection(URL url, JarFile jarFile) throws MalformedURLException { protected JarURLConnection(URL url, JarFile jarFile) throws MalformedURLException {
super(new URL(buildRootUrl(jarFile))); super(new URL(buildRootUrl(jarFile)));
this.jarFile = jarFile; this.jarFile = jarFile;
...@@ -53,9 +56,25 @@ class JarURLConnection extends java.net.JarURLConnection { ...@@ -53,9 +56,25 @@ class JarURLConnection extends java.net.JarURLConnection {
throw new MalformedURLException("no " + SEPARATOR + " found in url spec:" throw new MalformedURLException("no " + SEPARATOR + " found in url spec:"
+ spec); + spec);
} }
if (separator + 2 != spec.length()) { /*
* The superclass constructor creates a jarFileUrl which is equal to the root URL
* of the containing archive (therefore not unique if we are connecting to
* multiple nested jars in the same archive). Therefore we need to make something
* sensible for #getJarFileURL().
*/
if (separator + SEPARATOR.length() != spec.length()) {
this.jarFileUrl = new URL("jar:" + spec);
this.jarEntryName = spec.substring(separator + 2); this.jarEntryName = spec.substring(separator + 2);
} }
else {
// The root of the archive (!/)
this.jarFileUrl = new URL("jar:" + spec.substring(0, separator));
}
}
@Override
public URL getJarFileURL() {
return this.jarFileUrl;
} }
@Override @Override
...@@ -70,6 +89,16 @@ class JarURLConnection extends java.net.JarURLConnection { ...@@ -70,6 +89,16 @@ class JarURLConnection extends java.net.JarURLConnection {
this.connected = true; this.connected = true;
} }
@Override
public Manifest getManifest() throws IOException {
try {
return super.getManifest();
}
finally {
this.connected = false;
}
}
@Override @Override
public JarFile getJarFile() throws IOException { public JarFile getJarFile() throws IOException {
connect(); connect();
......
...@@ -150,6 +150,8 @@ public class JarFileTests { ...@@ -150,6 +150,8 @@ public class JarFileTests {
assertThat(jarURLConnection.getContentLength(), greaterThan(1)); assertThat(jarURLConnection.getContentLength(), greaterThan(1));
assertThat(jarURLConnection.getContent(), sameInstance((Object) this.jarFile)); assertThat(jarURLConnection.getContent(), sameInstance((Object) this.jarFile));
assertThat(jarURLConnection.getContentType(), equalTo("x-java/jar")); assertThat(jarURLConnection.getContentType(), equalTo("x-java/jar"));
assertThat(jarURLConnection.getJarFileURL().toString(), equalTo("jar:file:"
+ this.rootJarFile));
} }
@Test @Test
...@@ -212,8 +214,10 @@ public class JarFileTests { ...@@ -212,8 +214,10 @@ public class JarFileTests {
URL url = nestedJarFile.getUrl(); URL url = nestedJarFile.getUrl();
assertThat(url.toString(), equalTo("jar:file:" + this.rootJarFile.getPath() assertThat(url.toString(), equalTo("jar:file:" + this.rootJarFile.getPath()
+ "!/nested.jar!/")); + "!/nested.jar!/"));
assertThat(((JarURLConnection) url.openConnection()).getJarFile(), JarURLConnection conn = (JarURLConnection) url.openConnection();
sameInstance(nestedJarFile)); assertThat(conn.getJarFile(), sameInstance(nestedJarFile));
assertThat(conn.getJarFileURL().toString(), equalTo("jar:file:"
+ this.rootJarFile.getPath() + "!/nested.jar"));
} }
@Test @Test
...@@ -304,7 +308,6 @@ public class JarFileTests { ...@@ -304,7 +308,6 @@ public class JarFileTests {
} }
@Test @Test
@SuppressWarnings("resource")
public void verifySignedJar() throws Exception { public void verifySignedJar() throws Exception {
String classpath = System.getProperty("java.class.path"); String classpath = System.getProperty("java.class.path");
String[] entries = classpath.split(System.getProperty("path.separator")); String[] entries = classpath.split(System.getProperty("path.separator"));
......
...@@ -137,7 +137,13 @@ public class JettyEmbeddedServletContainerFactory extends ...@@ -137,7 +137,13 @@ public class JettyEmbeddedServletContainerFactory extends
File root = getValidDocumentRoot(); File root = getValidDocumentRoot();
if (root != null) { if (root != null) {
try { try {
handler.setBaseResource(Resource.newResource(root)); if (!root.isDirectory()) {
handler.setBaseResource(Resource.newResource("jar:" + root.toURI()
+ "!"));
}
else {
handler.setBaseResource(Resource.newResource(root));
}
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment