dea692910ad611f05ccb1a663cddaeba7fff37bf
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / util / HttpClientUtil.java
1 /*-
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.appc.listener.util;
20
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;
50
51 @SuppressWarnings("deprecation")
52 public class HttpClientUtil {
53   
54   private static final EELFLogger log = EELFManager.getInstance().getLogger(HttpClientUtil.class);
55
56   public static HttpClient getHttpClient(String protocol) throws APPCException {
57     HttpClient client;
58     if ("https".equals(protocol)) {
59       try {
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);
64
65         HttpParams params = new BasicHttpParams();
66         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
67         HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
68
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));
74
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();
80       }
81     } else if ("http".equals(protocol)) {
82       client = new DefaultHttpClient();
83     } else {
84       throw new APPCException(
85           "The provider.topology.url property is invalid. The url did not start with http[s]");
86     }
87     return client;
88   }
89
90   private static class MySSLSocketFactory extends SSLSocketFactory {
91     private SSLContext sslContext = SSLContext.getInstance("TLS");
92
93     public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException,
94         KeyManagementException, KeyStoreException, UnrecoverableKeyException {
95       super(truststore);
96
97       TrustManager tm = new X509TrustManager() {
98         @Override
99         public void checkClientTrusted(X509Certificate[] chain, String authType)
100             throws CertificateException {}
101
102         @Override
103         public void checkServerTrusted(X509Certificate[] chain, String authType)
104             throws CertificateException {}
105
106         @Override
107         public X509Certificate[] getAcceptedIssuers() {
108           return null;
109         }
110       };
111
112       sslContext.init(null, new TrustManager[] {tm}, null);
113     }
114
115     @Override
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);
119     }
120
121     @Override
122     public Socket createSocket() throws IOException {
123       return sslContext.getSocketFactory().createSocket();
124     }
125   }
126 }