Skip to content

Commit eadfa10

Browse files
committed
Improve classpath resource not supported message
Update TomcatEmbeddedServletContainerFactory to provide a better error message when attempting to load classpath resources. Fixes gh-2635
1 parent d01bc41 commit eadfa10

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,13 @@ else if (ssl.getClientAuth() == ClientAuth.WANT) {
325325

326326
private void configureSslKeyStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) {
327327
try {
328+
assertNotClasspathResource(ssl.getKeyStore());
328329
File file = ResourceUtils.getFile(ssl.getKeyStore());
329330
protocol.setKeystoreFile(file.getAbsolutePath());
330331
}
331332
catch (FileNotFoundException ex) {
332-
throw new EmbeddedServletContainerException("Could not find key store "
333-
+ ssl.getKeyStore(), ex);
333+
throw new EmbeddedServletContainerException("Could load key store: "
334+
+ ex.getMessage(), ex);
334335
}
335336
if (ssl.getKeyStoreType() != null) {
336337
protocol.setKeystoreType(ssl.getKeyStoreType());
@@ -343,12 +344,13 @@ private void configureSslKeyStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ss
343344
private void configureSslTrustStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) {
344345
if (ssl.getTrustStore() != null) {
345346
try {
347+
assertNotClasspathResource(ssl.getTrustStore());
346348
File file = ResourceUtils.getFile(ssl.getTrustStore());
347349
protocol.setTruststoreFile(file.getAbsolutePath());
348350
}
349351
catch (FileNotFoundException ex) {
350-
throw new EmbeddedServletContainerException("Could not find trust store "
351-
+ ssl.getTrustStore(), ex);
352+
throw new EmbeddedServletContainerException("Could load trust store: "
353+
+ ex.getMessage(), ex);
352354
}
353355
}
354356
protocol.setTruststorePass(ssl.getTrustStorePassword());
@@ -360,6 +362,13 @@ private void configureSslTrustStore(AbstractHttp11JsseProtocol<?> protocol, Ssl
360362
}
361363
}
362364

365+
private void assertNotClasspathResource(String resource) throws FileNotFoundException {
366+
if (resource.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
367+
throw new FileNotFoundException("Unable to load '" + resource
368+
+ "' since Tomcat doesn't support classpath resources");
369+
}
370+
}
371+
363372
/**
364373
* Configure the Tomcat {@link Context}.
365374
* @param context the Tomcat context

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.junit.Test;
4040
import org.mockito.InOrder;
4141
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
42+
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
4243
import org.springframework.boot.context.embedded.Ssl;
4344
import org.springframework.util.SocketUtils;
4445

@@ -314,6 +315,13 @@ public void run() {
314315

315316
}
316317

318+
@Test
319+
public void basicSslClasspathKeyStore() throws Exception {
320+
this.thrown.expect(EmbeddedServletContainerException.class);
321+
this.thrown.expectMessage("Tomcat doesn't support classpath resources");
322+
testBasicSslWithKeyStore("classpath:test.jks");
323+
}
324+
317325
@Test
318326
public void jspServletInitParameters() {
319327
Map<String, String> initParameters = new HashMap<String, String>();

0 commit comments

Comments
 (0)