diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/DocumentRoot.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/DocumentRoot.java index 484cf19804..fc1f21f7f4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/DocumentRoot.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/DocumentRoot.java @@ -17,7 +17,6 @@ package org.springframework.boot.web.servlet.server; import java.io.File; -import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.net.URLConnection; @@ -93,23 +92,29 @@ class DocumentRoot { } private File getCodeSourceArchive() { + return getCodeSourceArchive(getClass().getProtectionDomain().getCodeSource()); + } + + File getCodeSourceArchive(CodeSource codeSource) { try { - CodeSource codeSource = getClass().getProtectionDomain().getCodeSource(); URL location = (codeSource == null ? null : codeSource.getLocation()); if (location == null) { return null; } - String path = location.getPath(); + String path; URLConnection connection = location.openConnection(); if (connection instanceof JarURLConnection) { path = ((JarURLConnection) connection).getJarFile().getName(); } - if (path.indexOf("!/") != -1) { + else { + path = location.toURI().getPath(); + } + if (path.contains("!/")) { path = path.substring(0, path.indexOf("!/")); } return new File(path); } - catch (IOException ex) { + catch (Exception ex) { return null; } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/DocumentRootTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/DocumentRootTests.java index 42b061c701..18686cf1ab 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/DocumentRootTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/DocumentRootTests.java @@ -17,6 +17,9 @@ package org.springframework.boot.web.servlet.server; 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.junit.Rule; @@ -53,4 +56,20 @@ public class DocumentRootTests { 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/")); + } + }