2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.util;
23 import java.io.FileInputStream;
24 import java.security.KeyManagementException;
25 import java.security.KeyStore;
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;
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;
42 public class HttpsAuthExternalClient {
47 * @param args the arguments
49 public static void main(String[] args) {
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);
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);
64 // System.out.println("Jersey result: ");
65 // System.out.println(res.getEntity(String.class).toString());
67 Customers customers = res.getEntity(Customers.class);
68 System.out.println("Jersey result: ");
69 System.out.println("Number of customers: " + customers.getCustomer().size());
71 } catch (KeyManagementException e) {
73 } catch (Exception e) {
82 * @param keystoreFileName the keystore file name
83 * @param keystorePassword the keystore password
85 * @throws Exception the exception
87 public static Client getClient ( String keystoreFileName, String keystorePassword ) throws Exception {
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);
93 SSLContext ctx = null;
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;
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) {
109 ctx = SSLContext.getInstance("TLS");
110 KeyManagerFactory kmf = null;
113 /**** kmf = KeyManagerFactory.getInstance("SunX509");
114 FileInputStream fin = new FileInputStream(keystore_path);
115 KeyStore ks = KeyStore.getInstance("PKCS12");
116 char[] pwd = keystore_password.toCharArray();
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();
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);
133 config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
134 new HTTPSProperties( new HostnameVerifier() {
136 public boolean verify( String s, SSLSession sslSession ) {
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) {