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