a984c1e6ccb80f82535c382fe8b76281d3215b12
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / HttpsAuthExternalClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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.aai.util;
22
23 import java.io.FileInputStream;
24 import java.security.KeyManagementException;
25 import java.security.KeyStore;
26
27 import javax.net.ssl.HostnameVerifier;
28 import javax.net.ssl.HttpsURLConnection;
29 import javax.net.ssl.KeyManagerFactory;
30 import javax.net.ssl.TrustManagerFactory;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.SSLSession;
33
34 import org.onap.aai.domain.yang.Customers;
35 import com.sun.jersey.api.client.Client;
36 import com.sun.jersey.api.client.ClientResponse;
37 import com.sun.jersey.api.client.config.ClientConfig;
38 import com.sun.jersey.api.client.config.DefaultClientConfig;
39 import com.sun.jersey.api.json.JSONConfiguration;
40 import com.sun.jersey.client.urlconnection.HTTPSProperties;
41
42 public class HttpsAuthExternalClient {
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                         String keystore = args[0];
54                         String keypasswd = args[1];
55                         Client client = HttpsAuthExternalClient.getClient(keystore, keypasswd);
56                 
57                         ClientResponse res = client.resource(url)
58                                         .accept("application/json")
59                                         .header("X-TransactionId", "PROV001")
60                                         .header("X-FromAppId",  "AAI")
61                                         .type("application/json")
62                                         .get(ClientResponse.class);
63                         
64 //                      System.out.println("Jersey result: ");
65 //                      System.out.println(res.getEntity(String.class).toString());
66                         
67                         Customers customers = res.getEntity(Customers.class);
68                         System.out.println("Jersey result: ");
69                         System.out.println("Number of customers: " + customers.getCustomer().size());   
70                         
71                 } catch (KeyManagementException e) {
72                         e.printStackTrace();
73                 } catch (Exception e) {
74                         e.printStackTrace();
75                 }
76         }
77
78         
79         /**
80          * Gets the client.
81          *
82          * @param keystoreFileName the keystore file name
83          * @param keystorePassword the keystore password
84          * @return the client
85          * @throws Exception the exception
86          */
87         public static Client getClient ( String keystoreFileName, String keystorePassword ) throws Exception {
88                 
89                 ClientConfig config = new DefaultClientConfig();
90                 config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
91                 config.getClasses().add(org.onap.aai.restcore.CustomJacksonJaxBJsonProvider.class);
92                 Client client = null;
93                 SSLContext ctx = null;
94                 
95                 try {
96                         String truststore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME);
97                         String truststore_password = AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_PASSWD);
98                         String keystore_path = AAIConstants.AAI_HOME_ETC_AUTH + keystoreFileName;
99                         String keystore_password = keystorePassword;
100
101                     //System.setProperty("javax.net.ssl.trustStore", truststore_path);
102                     //System.setProperty("javax.net.ssl.trustStorePassword", truststore_password);
103                         HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
104                             public boolean verify(String string,SSLSession ssls) {
105                                 return true;
106                             }
107                         });
108                                                 
109                         ctx = SSLContext.getInstance("TLS");
110                         KeyManagerFactory kmf = null;
111
112                         
113                         /**** kmf = KeyManagerFactory.getInstance("SunX509");
114                         FileInputStream fin = new FileInputStream(keystore_path);
115                         KeyStore ks = KeyStore.getInstance("PKCS12");
116                         char[] pwd = keystore_password.toCharArray();
117                         ks.load(fin, pwd);
118                         kmf.init(ks, pwd);
119                         ***/
120                         
121                         String alg = TrustManagerFactory.getDefaultAlgorithm();
122                         TrustManagerFactory tmf = TrustManagerFactory.getInstance(alg);
123                         FileInputStream tin = new FileInputStream(truststore_path);
124                         KeyStore ts = KeyStore.getInstance("PKCS12");
125                         char[] tpwd = truststore_password.toCharArray();
126                         ts.load(tin, tpwd);
127                         tmf.init(ts);
128         
129                         //ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
130                         // Updating key manager to null, to disable two way SSL
131                         ctx.init(null, tmf.getTrustManagers(), null);
132                         
133                         config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, 
134                                                                         new HTTPSProperties( new HostnameVerifier() {
135                                 @Override
136                                 public boolean verify( String s, SSLSession sslSession ) {
137                                         return true;
138                                 }
139                         }, ctx));
140                         
141                         client = Client.create(config);
142                         // uncomment this line to get more logging for the request/response
143                         // client.addFilter(new LoggingFilter(System.out));
144                 } catch (Exception e) {
145                         throw e;
146                 }
147                 return client;
148         }
149         
150 }