2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6 * file except in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * SPDX-License-Identifier: Apache-2.0
16 * ============LICENSE_END=========================================================
19 package org.onap.appc.listener.util;
21 import java.io.IOException;
22 import java.net.Socket;
23 import java.net.UnknownHostException;
24 import java.security.KeyManagementException;
25 import java.security.KeyStore;
26 import java.security.KeyStoreException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.UnrecoverableKeyException;
29 import java.security.cert.CertificateException;
30 import java.security.cert.X509Certificate;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.TrustManager;
33 import javax.net.ssl.X509TrustManager;
34 import org.apache.http.HttpVersion;
35 import org.apache.http.client.HttpClient;
36 import org.apache.http.conn.ClientConnectionManager;
37 import org.apache.http.conn.scheme.PlainSocketFactory;
38 import org.apache.http.conn.scheme.Scheme;
39 import org.apache.http.conn.scheme.SchemeRegistry;
40 import org.apache.http.conn.ssl.SSLSocketFactory;
41 import org.apache.http.impl.client.DefaultHttpClient;
42 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
43 import org.apache.http.params.BasicHttpParams;
44 import org.apache.http.params.HttpParams;
45 import org.apache.http.params.HttpProtocolParams;
46 import org.apache.http.protocol.HTTP;
47 import org.onap.appc.exceptions.APPCException;
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFManager;
51 @SuppressWarnings("deprecation")
52 public class HttpClientUtil {
54 private static final EELFLogger log = EELFManager.getInstance().getLogger(HttpClientUtil.class);
56 public static HttpClient getHttpClient(String protocol) throws APPCException {
58 if ("https".equals(protocol)) {
60 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
61 trustStore.load(null, null);
62 MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
63 sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
65 HttpParams params = new BasicHttpParams();
66 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
67 HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
69 SchemeRegistry registry = new SchemeRegistry();
70 registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
71 registry.register(new Scheme("https", sf, 443));
72 registry.register(new Scheme("https", sf, 8443));
73 registry.register(new Scheme("http", sf, 8181));
75 ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
76 client = new DefaultHttpClient(ccm, params);
77 } catch (Exception e) {
78 log.info("Creating Default Http Client with no params"+e.getMessage());
79 client = new DefaultHttpClient();
81 } else if ("http".equals(protocol)) {
82 client = new DefaultHttpClient();
84 throw new APPCException(
85 "The provider.topology.url property is invalid. The url did not start with http[s]");
90 private static class MySSLSocketFactory extends SSLSocketFactory {
91 private SSLContext sslContext = SSLContext.getInstance("TLS");
93 public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException,
94 KeyManagementException, KeyStoreException, UnrecoverableKeyException {
97 TrustManager tm = new X509TrustManager() {
99 public void checkClientTrusted(X509Certificate[] chain, String authType)
100 throws CertificateException {}
103 public void checkServerTrusted(X509Certificate[] chain, String authType)
104 throws CertificateException {}
107 public X509Certificate[] getAcceptedIssuers() {
112 sslContext.init(null, new TrustManager[] {tm}, null);
116 public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
117 throws IOException, UnknownHostException {
118 return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
122 public Socket createSocket() throws IOException {
123 return sslContext.getSocketFactory().createSocket();