Skip to content

Commit 3cc7509

Browse files
committed
Merge pull request #10706 from Ali Kord
* gh-10706: Polish "Fix handling of spaces in container's document root" Fix handling of spaces in container's document root
2 parents 276a9a0 + c14f5fb commit 3cc7509

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,29 @@ private File getCommonDocumentRoot() {
194194
}
195195

196196
private File getCodeSourceArchive() {
197+
return getCodeSourceArchive(getClass().getProtectionDomain().getCodeSource());
198+
}
199+
200+
File getCodeSourceArchive(CodeSource codeSource) {
197201
try {
198-
CodeSource codeSource = getClass().getProtectionDomain().getCodeSource();
199202
URL location = (codeSource == null ? null : codeSource.getLocation());
200203
if (location == null) {
201204
return null;
202205
}
203-
String path = location.getPath();
206+
String path;
204207
URLConnection connection = location.openConnection();
205208
if (connection instanceof JarURLConnection) {
206209
path = ((JarURLConnection) connection).getJarFile().getName();
207210
}
208-
if (path.indexOf("!/") != -1) {
211+
else {
212+
path = location.toURI().getPath();
213+
}
214+
if (path.contains("!/")) {
209215
path = path.substring(0, path.indexOf("!/"));
210216
}
211217
return new File(path);
212218
}
213-
catch (IOException ex) {
219+
catch (Exception ex) {
214220
return null;
215221
}
216222
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import java.net.URISyntaxException;
3131
import java.net.URL;
3232
import java.nio.charset.Charset;
33+
import java.security.CodeSource;
3334
import java.security.KeyStore;
3435
import java.security.KeyStoreException;
3536
import java.security.NoSuchAlgorithmException;
37+
import java.security.cert.Certificate;
3638
import java.security.cert.CertificateException;
3739
import java.security.cert.X509Certificate;
3840
import java.util.Arrays;
@@ -673,6 +675,24 @@ public void cannotReadClassPathFiles() throws Exception {
673675
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
674676
}
675677

678+
@Test
679+
public void codeSourceArchivePath() throws Exception {
680+
AbstractEmbeddedServletContainerFactory factory = getFactory();
681+
CodeSource codeSource = new CodeSource(new URL("file", "", "/some/test/path/"),
682+
(Certificate[]) null);
683+
File codeSourceArchive = factory.getCodeSourceArchive(codeSource);
684+
assertThat(codeSourceArchive).isEqualTo(new File("/some/test/path/"));
685+
}
686+
687+
@Test
688+
public void codeSourceArchivePathContainingSpaces() throws Exception {
689+
AbstractEmbeddedServletContainerFactory factory = getFactory();
690+
CodeSource codeSource = new CodeSource(
691+
new URL("file", "", "/test/path/with%20space/"), (Certificate[]) null);
692+
File codeSourceArchive = factory.getCodeSourceArchive(codeSource);
693+
assertThat(codeSourceArchive).isEqualTo(new File("/test/path/with space/"));
694+
}
695+
676696
protected Ssl getSsl(ClientAuth clientAuth, String keyPassword, String keyStore) {
677697
return getSsl(clientAuth, keyPassword, keyStore, null, null, null);
678698
}

0 commit comments

Comments
 (0)