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