Commit 9cd1a4b0 authored by Rupert Madden-Abbott's avatar Rupert Madden-Abbott Committed by Andy Wilkinson

Fix handling of static resource jars with spaces in their paths

See gh-11991
parent 647c6c45
...@@ -22,6 +22,7 @@ import java.net.JarURLConnection; ...@@ -22,6 +22,7 @@ import java.net.JarURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLDecoder;
import java.security.CodeSource; import java.security.CodeSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -96,56 +97,64 @@ public abstract class AbstractEmbeddedServletContainerFactory ...@@ -96,56 +97,64 @@ public abstract class AbstractEmbeddedServletContainerFactory
List<URL> staticResourceUrls = new ArrayList<URL>(); List<URL> staticResourceUrls = new ArrayList<URL>();
if (classLoader instanceof URLClassLoader) { if (classLoader instanceof URLClassLoader) {
for (URL url : ((URLClassLoader) classLoader).getURLs()) { for (URL url : ((URLClassLoader) classLoader).getURLs()) {
try { if (isStaticResource(url)) {
if ("file".equals(url.getProtocol())) { staticResourceUrls.add(url);
File file = new File(url.getFile());
if (file.isDirectory()
&& new File(file, "META-INF/resources").isDirectory()) {
staticResourceUrls.add(url);
}
else if (isResourcesJar(file)) {
staticResourceUrls.add(url);
}
}
else {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
if (isResourcesJar((JarURLConnection) connection)) {
staticResourceUrls.add(url);
}
}
}
}
catch (IOException ex) {
throw new IllegalStateException(ex);
} }
} }
} }
return staticResourceUrls; return staticResourceUrls;
} }
protected boolean isStaticResource(URL url) {
try {
if ("file".equals(url.getProtocol())) {
File file = new File(URLDecoder.decode(url.getFile(), "UTF-8"));
if (file.isDirectory()
&& new File(file, "META-INF/resources").isDirectory()) {
return true;
}
else if (isResourcesJar(file)) {
return true;
}
}
else {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
if (isResourcesJar((JarURLConnection) connection)) {
return true;
}
}
}
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
return false;
}
private boolean isResourcesJar(JarURLConnection connection) { private boolean isResourcesJar(JarURLConnection connection) {
try { try {
return isResourcesJar(connection.getJarFile()); return isResourcesJar(connection.getJarFile());
} }
catch (IOException ex) { catch (IOException ex) {
logger.warn("Unable to open jar to determine if it contains static resources", ex);
return false; return false;
} }
} }
private boolean isResourcesJar(File file) { private boolean isResourcesJar(File file) {
try { try {
return isResourcesJar(new JarFile(file)); return file.getName().endsWith(".jar") && isResourcesJar(new JarFile(file));
} }
catch (IOException ex) { catch (IOException ex) {
logger.warn("Unable to open jar to determine if it contains static resources", ex);
return false; return false;
} }
} }
private boolean isResourcesJar(JarFile jar) throws IOException { private boolean isResourcesJar(JarFile jar) throws IOException {
try { try {
return jar.getName().endsWith(".jar") return jar.getJarEntry("META-INF/resources") != null;
&& (jar.getJarEntry("META-INF/resources") != null);
} }
finally { finally {
jar.close(); jar.close();
......
...@@ -18,6 +18,7 @@ package org.springframework.boot.context.embedded; ...@@ -18,6 +18,7 @@ package org.springframework.boot.context.embedded;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
...@@ -49,6 +50,8 @@ import java.util.Set; ...@@ -49,6 +50,8 @@ import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
...@@ -1035,6 +1038,54 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { ...@@ -1035,6 +1038,54 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
assertThat(documentRoot).isNull(); assertThat(documentRoot).isNull();
} }
@Test
public void includeJarWithStaticResources() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
File jarFile = this.temporaryFolder.newFile("test.jar");
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(jarFile));
JarEntry jarEntry = new JarEntry("META-INF/resources");
jarOutputStream.putNextEntry(jarEntry);
jarOutputStream.closeEntry();
jarOutputStream.close();
String path = "file:" + jarFile.getAbsolutePath();
boolean isStaticResource = factory.isStaticResource(new URL(path));
assertThat(isStaticResource).isTrue();
}
@Test
public void includeJarWithStaticResourcesWithUrlEncodedSpaces() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
this.temporaryFolder.newFolder("test parent");
File jarFile = this.temporaryFolder.newFile("test parent/test.jar");
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(jarFile));
JarEntry jarEntry = new JarEntry("META-INF/resources");
jarOutputStream.putNextEntry(jarEntry);
jarOutputStream.closeEntry();
jarOutputStream.close();
String path = "file:" + jarFile.getAbsolutePath().replaceAll(" ", "%20");
boolean isStaticResource = factory.isStaticResource(new URL(path));
assertThat(isStaticResource).isTrue();
}
@Test
public void excludeJarWithoutStaticResources() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
File jarFile = this.temporaryFolder.newFile("test.jar");
JarOutputStream jarOutputStream = new JarOutputStream(
new FileOutputStream(jarFile));
jarOutputStream.closeEntry();
jarOutputStream.close();
String path = "file:" + jarFile.getAbsolutePath();
boolean isStaticResource = factory.isStaticResource(new URL(path));
assertThat(isStaticResource).isFalse();
}
protected abstract void addConnector(int port, protected abstract void addConnector(int port,
AbstractEmbeddedServletContainerFactory factory); AbstractEmbeddedServletContainerFactory factory);
......
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