Commit 1a76b512 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '1.5.x'

parents 72b14b8a 3cc75098
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.springframework.boot.web.servlet.server; package org.springframework.boot.web.servlet.server;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
...@@ -93,23 +92,29 @@ class DocumentRoot { ...@@ -93,23 +92,29 @@ class DocumentRoot {
} }
private File getCodeSourceArchive() { private File getCodeSourceArchive() {
return getCodeSourceArchive(getClass().getProtectionDomain().getCodeSource());
}
File getCodeSourceArchive(CodeSource codeSource) {
try { try {
CodeSource codeSource = getClass().getProtectionDomain().getCodeSource();
URL location = (codeSource == null ? null : codeSource.getLocation()); URL location = (codeSource == null ? null : codeSource.getLocation());
if (location == null) { if (location == null) {
return null; return null;
} }
String path = location.getPath(); String path;
URLConnection connection = location.openConnection(); URLConnection connection = location.openConnection();
if (connection instanceof JarURLConnection) { if (connection instanceof JarURLConnection) {
path = ((JarURLConnection) connection).getJarFile().getName(); path = ((JarURLConnection) connection).getJarFile().getName();
} }
if (path.indexOf("!/") != -1) { else {
path = location.toURI().getPath();
}
if (path.contains("!/")) {
path = path.substring(0, path.indexOf("!/")); path = path.substring(0, path.indexOf("!/"));
} }
return new File(path); return new File(path);
} }
catch (IOException ex) { catch (Exception ex) {
return null; return null;
} }
} }
......
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
package org.springframework.boot.web.servlet.server; package org.springframework.boot.web.servlet.server;
import java.io.File; import java.io.File;
import java.net.URL;
import java.security.CodeSource;
import java.security.cert.Certificate;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.junit.Rule; import org.junit.Rule;
...@@ -53,4 +56,20 @@ public class DocumentRootTests { ...@@ -53,4 +56,20 @@ public class DocumentRootTests {
assertThat(directory).isNull(); assertThat(directory).isNull();
} }
@Test
public void codeSourceArchivePath() throws Exception {
CodeSource codeSource = new CodeSource(new URL("file", "", "/some/test/path/"),
(Certificate[]) null);
File codeSourceArchive = this.documentRoot.getCodeSourceArchive(codeSource);
assertThat(codeSourceArchive).isEqualTo(new File("/some/test/path/"));
}
@Test
public void codeSourceArchivePathContainingSpaces() throws Exception {
CodeSource codeSource = new CodeSource(
new URL("file", "", "/test/path/with%20space/"), (Certificate[]) null);
File codeSourceArchive = this.documentRoot.getCodeSourceArchive(codeSource);
assertThat(codeSourceArchive).isEqualTo(new File("/test/path/with space/"));
}
} }
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