Improve classpath resource not supported message
Update TomcatEmbeddedServletContainerFactory to provide a better error message when attempting to load classpath resources. Fixes gh-2635
This commit is contained in:
		
							parent
							
								
									d01bc41e1e
								
							
						
					
					
						commit
						eadfa109d5
					
				| 
						 | 
				
			
			@ -325,12 +325,13 @@ public class TomcatEmbeddedServletContainerFactory extends
 | 
			
		|||
 | 
			
		||||
	private void configureSslKeyStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) {
 | 
			
		||||
		try {
 | 
			
		||||
			assertNotClasspathResource(ssl.getKeyStore());
 | 
			
		||||
			File file = ResourceUtils.getFile(ssl.getKeyStore());
 | 
			
		||||
			protocol.setKeystoreFile(file.getAbsolutePath());
 | 
			
		||||
		}
 | 
			
		||||
		catch (FileNotFoundException ex) {
 | 
			
		||||
			throw new EmbeddedServletContainerException("Could not find key store "
 | 
			
		||||
					+ ssl.getKeyStore(), ex);
 | 
			
		||||
			throw new EmbeddedServletContainerException("Could load key store: "
 | 
			
		||||
					+ ex.getMessage(), ex);
 | 
			
		||||
		}
 | 
			
		||||
		if (ssl.getKeyStoreType() != null) {
 | 
			
		||||
			protocol.setKeystoreType(ssl.getKeyStoreType());
 | 
			
		||||
| 
						 | 
				
			
			@ -343,12 +344,13 @@ public class TomcatEmbeddedServletContainerFactory extends
 | 
			
		|||
	private void configureSslTrustStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) {
 | 
			
		||||
		if (ssl.getTrustStore() != null) {
 | 
			
		||||
			try {
 | 
			
		||||
				assertNotClasspathResource(ssl.getTrustStore());
 | 
			
		||||
				File file = ResourceUtils.getFile(ssl.getTrustStore());
 | 
			
		||||
				protocol.setTruststoreFile(file.getAbsolutePath());
 | 
			
		||||
			}
 | 
			
		||||
			catch (FileNotFoundException ex) {
 | 
			
		||||
				throw new EmbeddedServletContainerException("Could not find trust store "
 | 
			
		||||
						+ ssl.getTrustStore(), ex);
 | 
			
		||||
				throw new EmbeddedServletContainerException("Could load trust store: "
 | 
			
		||||
						+ ex.getMessage(), ex);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		protocol.setTruststorePass(ssl.getTrustStorePassword());
 | 
			
		||||
| 
						 | 
				
			
			@ -360,6 +362,13 @@ public class TomcatEmbeddedServletContainerFactory extends
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void assertNotClasspathResource(String resource) throws FileNotFoundException {
 | 
			
		||||
		if (resource.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
 | 
			
		||||
			throw new FileNotFoundException("Unable to load '" + resource
 | 
			
		||||
					+ "' since Tomcat doesn't support classpath resources");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Configure the Tomcat {@link Context}.
 | 
			
		||||
	 * @param context the Tomcat context
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,6 +39,7 @@ import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
 | 
			
		|||
import org.junit.Test;
 | 
			
		||||
import org.mockito.InOrder;
 | 
			
		||||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
 | 
			
		||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
 | 
			
		||||
import org.springframework.boot.context.embedded.Ssl;
 | 
			
		||||
import org.springframework.util.SocketUtils;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -314,6 +315,13 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
 | 
			
		|||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void basicSslClasspathKeyStore() throws Exception {
 | 
			
		||||
		this.thrown.expect(EmbeddedServletContainerException.class);
 | 
			
		||||
		this.thrown.expectMessage("Tomcat doesn't support classpath resources");
 | 
			
		||||
		testBasicSslWithKeyStore("classpath:test.jks");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void jspServletInitParameters() {
 | 
			
		||||
		Map<String, String> initParameters = new HashMap<String, String>();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue