org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / HttpsComponentsClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.aai.util;
22
23 import java.io.FileInputStream;
24 import java.security.KeyManagementException;
25 import java.security.KeyStore;
26
27 import javax.net.ssl.SSLContext;
28
29 import org.apache.http.conn.ssl.SSLContextBuilder;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
32 import org.apache.http.impl.client.HttpClients;
33 import org.eclipse.jetty.util.security.Password;
34 import org.openecomp.portalsdk.core.util.SystemProperties;
35
36
37 /**
38  * The Class HttpsComponentsClient.
39  */
40 public class HttpsComponentsClient{
41         
42         /**
43          * Gets the client.
44          *
45          * @param certFilePath the cert file path
46          * @return the client
47          * @throws KeyManagementException the key management exception
48          */
49         public static CloseableHttpClient getClient(String certFilePath) throws Exception {
50                 CloseableHttpClient httpclient = null;
51                 try {
52                         
53                         String truststore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_FILENAME);
54                         String truststore_password = SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_PASSWD_X);
55                         String decrypted_truststore_password = Password.deobfuscate(truststore_password);
56                         String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
57                         String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
58                         String decrypted_keystore_password = Password.deobfuscate(keystore_password);
59                         
60                         SSLContextBuilder sslContextB = new SSLContextBuilder();
61                         
62                         KeyStore ks = KeyStore.getInstance("PKCS12");
63                         FileInputStream fin = new FileInputStream(keystore_path);
64                         char[] pwd = decrypted_keystore_password.toCharArray();
65                         ks.load(fin, pwd);
66                         
67                         sslContextB.loadKeyMaterial(ks, pwd);
68                         
69                         KeyStore ts = KeyStore.getInstance("JKS");
70                         FileInputStream fin1 = new FileInputStream(truststore_path);
71                         char[] pwd1 = decrypted_truststore_password.toCharArray();
72                         ts.load(fin1, pwd1);
73                         
74                         sslContextB.loadTrustMaterial(ts);
75                         sslContextB.loadKeyMaterial(ks, pwd);
76                         sslContextB.useTLS();
77                         
78                         SSLContext sslcontext = sslContextB.build();
79                         
80                         SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(
81                         sslcontext,
82                         new String[] { "TLSv1.1", "TLSv1.2" },
83                         null,
84                         SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
85                 
86                         httpclient = HttpClients.custom()
87                         .setSSLSocketFactory(sslFactory)
88                         .build();
89
90
91                 } catch (Exception e) {
92                         throw e;
93                 }
94                 return httpclient;
95         }
96
97
98         
99 }