Java tutorial
/** * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * * Copyright 2016-2016 SpectoLabs Ltd. */ /* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Copyright 2016-2016 SpectoLabs Ltd. */ package io.specto.hoverfly.junit; import org.apache.commons.lang3.SystemUtils; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Paths; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; class HoverflyRuleUtils { private static final String OSX = "OSX"; private static final String WINDOWS = "windows"; private static final String LINUX = "linux"; private static final String ARCH_AMD64 = "amd64"; private static final String ARCH_386 = "386"; static URI findResourceOnClasspath(String resourceName) throws URISyntaxException { final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final URL resource = classLoader.getResource(resourceName); if (resource == null) { throw new IllegalArgumentException("Resource not found with name: " + resourceName); } return resource.toURI(); } static URI createFileRelativeToClasspath(String resourceName) throws IOException { final File file = Paths.get(new File("").getAbsolutePath(), resourceName).toFile(); file.mkdirs(); if (!file.exists()) { file.delete(); } file.createNewFile(); return file.toURI(); } static String getOs() { if (SystemUtils.IS_OS_MAC) { return OSX; } else if (SystemUtils.IS_OS_WINDOWS) { return WINDOWS; } else if (SystemUtils.IS_OS_LINUX) { return LINUX; } else { throw new UnsupportedOperationException(SystemUtils.OS_NAME + " is not currently supported"); } } static String getArchitectureType() { return SystemUtils.OS_ARCH.contains("64") ? ARCH_AMD64 : ARCH_386; } static int findUnusedPort() { try (final ServerSocket serverSocket = new ServerSocket(0)) { return serverSocket.getLocalPort(); } catch (IOException e) { throw new RuntimeException("Cannot find available port", e); } } static void setHoverflyTrustStore() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, KeyManagementException, URISyntaxException { // load your key store as a stream and initialize a KeyStore InputStream trustStream = findResourceOnClasspath("hoverfly.jks").toURL().openStream(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); // load the stream to your store trustStore.load(trustStream, "hoverfly".toCharArray()); // initialize a trust manager factory with the trusted store TrustManagerFactory trustFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(trustStore); // get the trust managers from the factory TrustManager[] trustManagers = trustFactory.getTrustManagers(); // initialize an ssl context to use these managers and set as default SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustManagers, null); SSLContext.setDefault(sslContext); } }