Merge branch '1.5.x'

pull/11126/merge
Andy Wilkinson 7 years ago
commit 1a76b5122d

@ -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;
}
}

@ -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/"));
}
}

Loading…
Cancel
Save