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