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