24ba1ba8a7d9dd41dff21c23ad27f78ea47c5c9f
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / HttpsAuthClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.util;
23
24 import java.io.FileInputStream;
25 import java.security.KeyManagementException;
26 import java.security.KeyStore;
27
28 import javax.net.ssl.HostnameVerifier;
29 import javax.net.ssl.HttpsURLConnection;
30 import javax.net.ssl.KeyManagerFactory;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.SSLSession;
33
34 import com.sun.jersey.api.client.Client;
35 import com.sun.jersey.api.client.ClientResponse;
36 import com.sun.jersey.api.client.config.ClientConfig;
37 import com.sun.jersey.api.client.config.DefaultClientConfig;
38 import com.sun.jersey.api.client.filter.LoggingFilter;
39 import com.sun.jersey.api.json.JSONConfiguration;
40 import com.sun.jersey.client.urlconnection.HTTPSProperties;
41
42 public class HttpsAuthClient{
43
44         /**
45          * The main method.
46          *
47          * @param args the arguments
48          */
49         public static void main(String[] args) {
50                 try {
51                         String url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + "business/customers";
52                         System.out.println("Making Jersey https call...");
53                         Client client = HttpsAuthClient.getClient();
54                 
55                         ClientResponse res = client.resource(url)
56                                         .accept("application/json")
57                                         .header("X-TransactionId", "PROV001")
58                                         .header("X-FromAppId",  "AAI")
59                                         .type("application/json")
60                                         .get(ClientResponse.class);
61                         
62 //                      System.out.println("Jersey result: ");
63 //                      System.out.println(res.getEntity(String.class).toString());
64                         
65                 } catch (KeyManagementException e) {
66                         e.printStackTrace();
67                 } catch (Exception e) {
68                         e.printStackTrace();
69                 }
70         }
71
72         
73         /**
74          * Gets the client.
75          *
76          * @return the client
77          * @throws KeyManagementException the key management exception
78          */
79         public static Client getClient() throws KeyManagementException {
80                 
81                 ClientConfig config = new DefaultClientConfig();
82                 config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
83                 config.getClasses().add(org.onap.aai.restcore.CustomJacksonJaxBJsonProvider.class);
84         
85                 SSLContext ctx = null;
86                 try {
87                         String truststore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME);
88                         String truststore_password = AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_PASSWD);
89                         String keystore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_KEYSTORE_FILENAME);
90                         String keystore_password = AAIConfig.get(AAIConstants.AAI_KEYSTORE_PASSWD);
91
92                     System.setProperty("javax.net.ssl.trustStore", truststore_path);
93                     System.setProperty("javax.net.ssl.trustStorePassword", truststore_password);
94                         HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
95                             public boolean verify(String string,SSLSession ssls) {
96                                 return true;
97                             }
98                         });
99                                                 
100                         ctx = SSLContext.getInstance("TLSv1.2");
101                         KeyManagerFactory kmf = null;
102                         try {
103                                 kmf = KeyManagerFactory.getInstance("SunX509");
104                                 FileInputStream fin = new FileInputStream(keystore_path);
105                                 KeyStore ks = KeyStore.getInstance("PKCS12");
106                                 char[] pwd = keystore_password.toCharArray();
107                                 ks.load(fin, pwd);
108                                 kmf.init(ks, pwd);
109                         } catch (Exception e) {
110                                 System.out.println("Error setting up kmf: exiting");
111                                 e.printStackTrace();
112                                 System.exit(1);
113                         }
114
115                         ctx.init(kmf.getKeyManagers(), null, null);
116                         config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, 
117                                                                                 new HTTPSProperties( new HostnameVerifier() {
118                                 @Override
119                                 public boolean verify( String s, SSLSession sslSession ) {
120                                         return true;
121                                 }
122                         }, ctx));
123                 } catch (Exception e) {
124                         System.out.println("Error setting up config: exiting");
125                         e.printStackTrace();
126                         System.exit(1);
127                 }
128                         
129                 Client client = Client.create(config);
130                 // uncomment this line to get more logging for the request/response
131                 // client.addFilter(new LoggingFilter(System.out));
132                 
133                 return client;
134         }
135         
136 }