2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.pnfsimulator.simulator.client.utils.ssl;
23 import org.apache.http.client.HttpClient;
24 import org.apache.http.client.config.RequestConfig;
25 import org.apache.http.conn.ssl.NoopHostnameVerifier;
26 import org.apache.http.conn.ssl.TrustAllStrategy;
27 import org.apache.http.conn.ssl.TrustStrategy;
28 import org.apache.http.impl.client.HttpClientBuilder;
29 import org.apache.http.impl.client.HttpClients;
30 import org.apache.http.ssl.SSLContextBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import javax.net.ssl.SSLContext;
35 import java.net.MalformedURLException;
37 import java.security.KeyManagementException;
38 import java.security.KeyStoreException;
39 import java.security.NoSuchAlgorithmException;
41 public enum SslSupportLevel {
44 public HttpClient getClient(RequestConfig requestConfig) {
45 LOGGER.info("<!-----IN SslSupportLevel.NONE, Creating BasicHttpClient for http protocol----!>");
46 return HttpClientBuilder
48 .setDefaultRequestConfig(requestConfig)
53 public HttpClient getClient(RequestConfig requestConfig) {
54 LoggerFactory.getLogger(SslSupportLevel.class).info("<!-----IN SslSupportLevel.ALWAYS_TRUST, Creating client with SSL support for https protocol----!>");
57 SSLContext alwaysTrustSslContext = SSLContextBuilder.create().loadTrustMaterial(TRUST_STRATEGY_ALWAYS).build();
58 client = HttpClients.custom()
59 .setSSLContext(alwaysTrustSslContext)
60 .setSSLHostnameVerifier(new NoopHostnameVerifier())
61 .setDefaultRequestConfig(requestConfig)
64 } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
65 LOGGER.error("Could not initialize client due to SSL exception: {}. Default client without SSL support will be used instead.\nCause: {}", e.getMessage(), e.getCause());
66 client = NONE.getClient(requestConfig);
72 private static final Logger LOGGER = LoggerFactory.getLogger(SslSupportLevel.class);
73 private static final TrustStrategy TRUST_STRATEGY_ALWAYS = new TrustAllStrategy();
75 public static SslSupportLevel getSupportLevelBasedOnProtocol(String url) throws MalformedURLException {
76 return "https".equals(new URL(url).getProtocol()) ? SslSupportLevel.ALWAYS_TRUST : SslSupportLevel.NONE;
79 public abstract HttpClient getClient(RequestConfig requestConfig);