Commit 88423c50 authored by Andy Wilkinson's avatar Andy Wilkinson

Polish "Fix handling of static resource jars with spaces in their paths"

Closes gh-11991
parent 9cd1a4b0
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -40,7 +40,7 @@ import org.springframework.util.StringUtils; ...@@ -40,7 +40,7 @@ import org.springframework.util.StringUtils;
*/ */
class IdeApplicationLauncher extends AbstractApplicationLauncher { class IdeApplicationLauncher extends AbstractApplicationLauncher {
private final File exploded = new File("target/ide"); private final File exploded = new File("target/ide application");
IdeApplicationLauncher(ApplicationBuilder applicationBuilder) { IdeApplicationLauncher(ApplicationBuilder applicationBuilder) {
super(applicationBuilder); super(applicationBuilder);
......
...@@ -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.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
...@@ -97,7 +98,7 @@ public abstract class AbstractEmbeddedServletContainerFactory ...@@ -97,7 +98,7 @@ 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()) {
if (isStaticResource(url)) { if (isStaticResourceJar(url)) {
staticResourceUrls.add(url); staticResourceUrls.add(url);
} }
} }
...@@ -105,39 +106,45 @@ public abstract class AbstractEmbeddedServletContainerFactory ...@@ -105,39 +106,45 @@ public abstract class AbstractEmbeddedServletContainerFactory
return staticResourceUrls; return staticResourceUrls;
} }
protected boolean isStaticResource(URL url) { private boolean isStaticResourceJar(URL url) {
try { try {
if ("file".equals(url.getProtocol())) { if ("file".equals(url.getProtocol())) {
File file = new File(URLDecoder.decode(url.getFile(), "UTF-8")); File file = new File(getDecodedFile(url), "UTF-8");
if (file.isDirectory() return (file.isDirectory()
&& new File(file, "META-INF/resources").isDirectory()) { && new File(file, "META-INF/resources").isDirectory())
return true; || isResourcesJar(file);
}
else if (isResourcesJar(file)) {
return true;
}
} }
else { else {
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) { if (connection instanceof JarURLConnection
if (isResourcesJar((JarURLConnection) connection)) { && isResourcesJar((JarURLConnection) connection)) {
return true; return true;
} }
} }
} }
}
catch (IOException ex) { catch (IOException ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
} }
return false; return false;
} }
protected final String getDecodedFile(URL url) {
try {
return URLDecoder.decode(url.getFile(), "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException(
"Failed to decode '" + url.getFile() + "' using UTF-8");
}
}
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); this.logger.warn("Unable to open jar from connection '" + connection
+ "' to determine if it contains static resources", ex);
return false; return false;
} }
} }
...@@ -147,7 +154,8 @@ public abstract class AbstractEmbeddedServletContainerFactory ...@@ -147,7 +154,8 @@ public abstract class AbstractEmbeddedServletContainerFactory
return file.getName().endsWith(".jar") && 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); this.logger.warn("Unable to open jar '" + file
+ "' to determine if it contains static resources", ex);
return false; return false;
} }
} }
......
...@@ -433,7 +433,7 @@ public class JettyEmbeddedServletContainerFactory ...@@ -433,7 +433,7 @@ public class JettyEmbeddedServletContainerFactory
private Resource createResource(URL url) throws IOException { private Resource createResource(URL url) throws IOException {
if ("file".equals(url.getProtocol())) { if ("file".equals(url.getProtocol())) {
File file = new File(url.getFile()); File file = new File(getDecodedFile(url));
if (file.isFile()) { if (file.isFile()) {
return Resource.newResource("jar:" + url + "!/META-INF/resources"); return Resource.newResource("jar:" + url + "!/META-INF/resources");
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,9 +16,11 @@ ...@@ -16,9 +16,11 @@
package org.springframework.boot.context.embedded.tomcat; package org.springframework.boot.context.embedded.tomcat;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.util.List; import java.util.List;
import javax.naming.directory.DirContext; import javax.naming.directory.DirContext;
...@@ -47,7 +49,7 @@ abstract class TomcatResources { ...@@ -47,7 +49,7 @@ abstract class TomcatResources {
void addResourceJars(List<URL> resourceJarUrls) { void addResourceJars(List<URL> resourceJarUrls) {
for (URL url : resourceJarUrls) { for (URL url : resourceJarUrls) {
String file = url.getFile(); String file = getDecodedFile(url);
if (file.endsWith(".jar") || file.endsWith(".jar!/")) { if (file.endsWith(".jar") || file.endsWith(".jar!/")) {
String jar = url.toString(); String jar = url.toString();
if (!jar.startsWith("jar:")) { if (!jar.startsWith("jar:")) {
...@@ -62,6 +64,16 @@ abstract class TomcatResources { ...@@ -62,6 +64,16 @@ abstract class TomcatResources {
} }
} }
private String getDecodedFile(URL url) {
try {
return URLDecoder.decode(url.getFile(), "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException(
"Failed to decode '" + url.getFile() + "' using UTF-8");
}
}
protected final Context getContext() { protected final Context getContext() {
return this.context; return this.context;
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -494,7 +494,7 @@ public class UndertowEmbeddedServletContainerFactory ...@@ -494,7 +494,7 @@ public class UndertowEmbeddedServletContainerFactory
resourceManagers.add(rootResourceManager); resourceManagers.add(rootResourceManager);
for (URL url : metaInfResourceUrls) { for (URL url : metaInfResourceUrls) {
if ("file".equals(url.getProtocol())) { if ("file".equals(url.getProtocol())) {
File file = new File(url.getFile()); File file = new File(getDecodedFile(url));
if (file.isFile()) { if (file.isFile()) {
try { try {
resourceJarUrls.add(new URL("jar:" + url + "!/")); resourceJarUrls.add(new URL("jar:" + url + "!/"));
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,7 +18,6 @@ package org.springframework.boot.context.embedded; ...@@ -18,7 +18,6 @@ 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;
...@@ -50,8 +49,6 @@ import java.util.Set; ...@@ -50,8 +49,6 @@ 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;
...@@ -1038,54 +1035,6 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { ...@@ -1038,54 +1035,6 @@ 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