From 8031a1bdb4146f4b74e799bc60670a770ffb68e3 Mon Sep 17 00:00:00 2001 From: Ali Kord Date: Sun, 12 Nov 2017 22:39:13 +1100 Subject: [PATCH 1/2] Fix handling of spaces in container's document root See gh-10706 --- ...stractEmbeddedServletContainerFactory.java | 22 ++++++++++++++----- ...tEmbeddedServletContainerFactoryTests.java | 18 +++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java index 77e9c9357e..65798ab3fa 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java @@ -19,6 +19,7 @@ package org.springframework.boot.context.embedded; import java.io.File; import java.io.IOException; import java.net.JarURLConnection; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; @@ -87,7 +88,7 @@ public abstract class AbstractEmbeddedServletContainerFactory } private File getExplodedWarFileDocumentRoot() { - return getExplodedWarFileDocumentRoot(getCodeSourceArchive()); + return getExplodedWarFileDocumentRoot(getCodeSourceArchive(getCodeSource())); } protected List getUrlsOfJarsWithMetaInfResources() { @@ -172,7 +173,7 @@ public abstract class AbstractEmbeddedServletContainerFactory } private File getArchiveFileDocumentRoot(String extension) { - File file = getCodeSourceArchive(); + File file = getCodeSourceArchive(getCodeSource()); if (this.logger.isDebugEnabled()) { this.logger.debug("Code archive: " + file); } @@ -193,19 +194,21 @@ public abstract class AbstractEmbeddedServletContainerFactory return null; } - private File getCodeSourceArchive() { + 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); @@ -213,6 +216,13 @@ public abstract class AbstractEmbeddedServletContainerFactory catch (IOException ex) { return null; } + catch (URISyntaxException e) { + return null; + } + } + + private CodeSource getCodeSource() { + return getClass().getProtectionDomain().getCodeSource(); } protected final File getValidSessionStoreDir() { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 7305b07a47..2042d12f74 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -30,9 +30,11 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.Charset; +import java.security.CodeSource; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; +import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Arrays; @@ -673,6 +675,22 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); } + @Test + public void codeSourceArchivePath() throws Exception { + AbstractEmbeddedServletContainerFactory factory = getFactory(); + final CodeSource codeSource = new CodeSource(new URL("file", "", "/some/test/path/"), (Certificate[]) null); + final File codeSourceArchive = factory.getCodeSourceArchive(codeSource); + assertThat(codeSourceArchive).isEqualTo(new File("/some/test/path/")); + } + + @Test + public void codeSourceArchivePathContainingSpaces() throws Exception { + AbstractEmbeddedServletContainerFactory factory = getFactory(); + final CodeSource codeSource = new CodeSource(new URL("file", "", "/test/path/with%20space/"), (Certificate[]) null); + final File codeSourceArchive = factory.getCodeSourceArchive(codeSource); + assertThat(codeSourceArchive).isEqualTo(new File("/test/path/with space/")); + } + protected Ssl getSsl(ClientAuth clientAuth, String keyPassword, String keyStore) { return getSsl(clientAuth, keyPassword, keyStore, null, null, null); } From c14f5fb6adfd1e41ec2ee74089afffe7b8235a03 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 23 Nov 2017 14:02:02 +0000 Subject: [PATCH 2/2] Polish "Fix handling of spaces in container's document root" Closes gh-10706 --- ...bstractEmbeddedServletContainerFactory.java | 18 +++++++----------- ...ctEmbeddedServletContainerFactoryTests.java | 10 ++++++---- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java index 65798ab3fa..c8abfb69dc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java @@ -19,7 +19,6 @@ package org.springframework.boot.context.embedded; import java.io.File; import java.io.IOException; import java.net.JarURLConnection; -import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; @@ -88,7 +87,7 @@ public abstract class AbstractEmbeddedServletContainerFactory } private File getExplodedWarFileDocumentRoot() { - return getExplodedWarFileDocumentRoot(getCodeSourceArchive(getCodeSource())); + return getExplodedWarFileDocumentRoot(getCodeSourceArchive()); } protected List getUrlsOfJarsWithMetaInfResources() { @@ -173,7 +172,7 @@ public abstract class AbstractEmbeddedServletContainerFactory } private File getArchiveFileDocumentRoot(String extension) { - File file = getCodeSourceArchive(getCodeSource()); + File file = getCodeSourceArchive(); if (this.logger.isDebugEnabled()) { this.logger.debug("Code archive: " + file); } @@ -194,6 +193,10 @@ public abstract class AbstractEmbeddedServletContainerFactory return null; } + private File getCodeSourceArchive() { + return getCodeSourceArchive(getClass().getProtectionDomain().getCodeSource()); + } + File getCodeSourceArchive(CodeSource codeSource) { try { URL location = (codeSource == null ? null : codeSource.getLocation()); @@ -213,16 +216,9 @@ public abstract class AbstractEmbeddedServletContainerFactory } return new File(path); } - catch (IOException ex) { + catch (Exception ex) { return null; } - catch (URISyntaxException e) { - return null; - } - } - - private CodeSource getCodeSource() { - return getClass().getProtectionDomain().getCodeSource(); } protected final File getValidSessionStoreDir() { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 2042d12f74..26bccff169 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -678,16 +678,18 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { @Test public void codeSourceArchivePath() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); - final CodeSource codeSource = new CodeSource(new URL("file", "", "/some/test/path/"), (Certificate[]) null); - final File codeSourceArchive = factory.getCodeSourceArchive(codeSource); + CodeSource codeSource = new CodeSource(new URL("file", "", "/some/test/path/"), + (Certificate[]) null); + File codeSourceArchive = factory.getCodeSourceArchive(codeSource); assertThat(codeSourceArchive).isEqualTo(new File("/some/test/path/")); } @Test public void codeSourceArchivePathContainingSpaces() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); - final CodeSource codeSource = new CodeSource(new URL("file", "", "/test/path/with%20space/"), (Certificate[]) null); - final File codeSourceArchive = factory.getCodeSourceArchive(codeSource); + CodeSource codeSource = new CodeSource( + new URL("file", "", "/test/path/with%20space/"), (Certificate[]) null); + File codeSourceArchive = factory.getCodeSourceArchive(codeSource); assertThat(codeSourceArchive).isEqualTo(new File("/test/path/with space/")); }