diff --git a/oap-stdlib/src/main/java/oap/http/server/nio/NioHttpServer.java b/oap-stdlib/src/main/java/oap/http/server/nio/NioHttpServer.java index 78e060f158..491632a788 100644 --- a/oap-stdlib/src/main/java/oap/http/server/nio/NioHttpServer.java +++ b/oap-stdlib/src/main/java/oap/http/server/nio/NioHttpServer.java @@ -61,8 +61,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; -import java.nio.file.Files; -import java.nio.file.Path; +import java.net.URL; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -343,10 +342,10 @@ public enum PortType { public static class DefaultPort { public final int httpPort; public final int httpsPort; - public final Path keyStore; + public final URL keyStore; public final String password; - public DefaultPort( int httpPort, int httpsPort, Path keyStore, String password ) { + public DefaultPort( int httpPort, int httpsPort, URL keyStore, String password ) { this.httpPort = httpPort; this.httpsPort = httpsPort; this.keyStore = keyStore; @@ -359,7 +358,7 @@ public DefaultPort( int httpPort ) { } @SneakyThrows - private static KeyStore loadKeyStore( Path keyStoreLocation, String storePassword ) { + private static KeyStore loadKeyStore( URL keyStoreLocation, String storePassword ) { final KeyStore loadedKeystore; final String type = "JKS"; try { @@ -368,7 +367,7 @@ private static KeyStore loadKeyStore( Path keyStoreLocation, String storePasswor log.error( "loadKeyStore KeyStore.getInstance({}) exception: {}", type, ex.toString() ); throw ex; } - try( InputStream stream = Files.newInputStream( keyStoreLocation ) ) { + try( InputStream stream = keyStoreLocation.openStream() ) { loadedKeystore.load( stream, storePassword.toCharArray() ); return loadedKeystore; } catch( NoSuchAlgorithmException | CertificateException | IOException ex ) { @@ -397,7 +396,7 @@ private static KeyManager[] getKeyManagers( KeyStore keyStore, final String stor } @SneakyThrows - private static KeyManager[] makeKeyManagers( Path keyStoreLocation, final String password ) { + private static KeyManager[] makeKeyManagers( URL keyStoreLocation, final String password ) { KeyStore keyStore = loadKeyStore( keyStoreLocation, password ); KeyManager[] managers = getKeyManagers( keyStore, password ); log.info( "makeKeyManagers({}, {}) created KeyManagers {}", keyStoreLocation, password.length(), managers ); diff --git a/oap-stdlib/src/main/java/oap/reflect/Coercions.java b/oap-stdlib/src/main/java/oap/reflect/Coercions.java index 63d846e921..45edaf98e3 100644 --- a/oap-stdlib/src/main/java/oap/reflect/Coercions.java +++ b/oap-stdlib/src/main/java/oap/reflect/Coercions.java @@ -392,22 +392,28 @@ public Object apply( Object value ) { private static class URLConvertor implements Function { @Override public Object apply( Object value ) { - if( value instanceof URL ) return value; - else if( value instanceof String ) { - try { - - return new URL( ( String ) value ); - } catch( MalformedURLException e ) { - var url = getClass().getResource( ( String ) value ); - if( url != null ) return url; - + return switch( value ) { + case URL url -> url; + case String str -> { try { - return Paths.get( ( String ) value ).toUri().toURL(); - } catch( MalformedURLException malformedURLException ) { - throw new ReflectException( "cannot cast " + value + " to URL.class" ); + yield new URL( str ); + } catch( MalformedURLException e ) { + if( str.startsWith( "classpath(" ) ) { + str = str.substring( 10, str.length() - 1 ); + } + + var url = getClass().getResource( str ); + if( url != null ) yield url; + + try { + yield Paths.get( str ).toUri().toURL(); + } catch( MalformedURLException malformedURLException ) { + throw new ReflectException( "cannot cast " + value + " to URL.class" ); + } } } - } else throw new ReflectException( "cannot cast " + value + " to URL.class" ); + default -> throw new ReflectException( "cannot cast " + value + " to URL.class" ); + }; } } diff --git a/oap-stdlib/src/test/java/oap/http/server/nio/NioHttpServerTest.java b/oap-stdlib/src/test/java/oap/http/server/nio/NioHttpServerTest.java index ab5d8149b1..9f76e4b753 100644 --- a/oap-stdlib/src/test/java/oap/http/server/nio/NioHttpServerTest.java +++ b/oap-stdlib/src/test/java/oap/http/server/nio/NioHttpServerTest.java @@ -27,12 +27,15 @@ import oap.application.testng.KernelFixture; import oap.http.Client; import oap.http.Http; +import oap.io.Resources; import oap.testng.EnvFixture; import oap.testng.Fixtures; import oap.testng.TestDirectoryFixture; import org.testng.annotations.Test; import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Paths; import java.util.List; import static oap.http.Http.Headers.CONNECTION; @@ -102,7 +105,7 @@ public void testBindToSpecificPort() throws IOException { * keytool -genkey -alias ssl -keyalg RSA -keysize 2048 -dname "CN=localhost,OU=IT" -keystore master.jks -storepass 1234567 -keypass 1234567 */ @Test - public void testHttps() { + public void testHttps() throws URISyntaxException { TestDirectoryFixture.deployTestData( getClass() ); var kernelFixture = new KernelFixture( urlOfTestResource( getClass(), "test-application.conf" ), @@ -115,7 +118,7 @@ public void testHttps() { int httpsPort = kernelFixture.portFor( "TEST_HTTPS_PORT" ); try( Client client = Client - .custom( TestDirectoryFixture.testPath( "master.jks" ), "1234567", 10000, 10000 ) + .custom( Paths.get(Resources.url( getClass(), "/oap/http/test_https.jks" ).get().toURI()), "1234567", 10000, 10000 ) .build() ) { kernelFixture.beforeMethod(); diff --git a/oap-stdlib/src/test/java/oap/reflect/CoercionsTest.java b/oap-stdlib/src/test/java/oap/reflect/CoercionsTest.java index cf14334dea..fce44ee6f3 100644 --- a/oap-stdlib/src/test/java/oap/reflect/CoercionsTest.java +++ b/oap-stdlib/src/test/java/oap/reflect/CoercionsTest.java @@ -105,6 +105,8 @@ public void testUrl() throws MalformedURLException { assertThat( coercions.cast( Reflect.reflect( URL.class ), "/oap/reflect/CoercionsTest.class" ) ) .isEqualTo( Coercions.class.getResource( "/oap/reflect/CoercionsTest.class" ) ); + assertThat( coercions.cast( Reflect.reflect( URL.class ), "classpath(/oap/reflect/CoercionsTest.class)" ) ) + .isEqualTo( Coercions.class.getResource( "/oap/reflect/CoercionsTest.class" ) ); } @ToString diff --git a/oap-stdlib/src/test/resources/oap/http/server/nio/NioHttpServerTest/test-application.conf b/oap-stdlib/src/test/resources/oap/http/server/nio/NioHttpServerTest/test-application.conf index 652bad9155..957f033a82 100644 --- a/oap-stdlib/src/test/resources/oap/http/server/nio/NioHttpServerTest/test-application.conf +++ b/oap-stdlib/src/test/resources/oap/http/server/nio/NioHttpServerTest/test-application.conf @@ -8,7 +8,7 @@ services { defaultPort { httpPort = ${TEST_HTTP_PORT} httpsPort = ${TEST_HTTPS_PORT} - keyStore = ${TEST_DIRECTORY}/master.jks + keyStore = classpath(/oap/http/test_https.jks) password = ${TEST_PASSWORD} } additionalHttpPorts.httpprivate = ${TEST_HTTP_PORT}