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.openecomp.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.SSLContext;
31 import javax.net.ssl.SSLSession;
33 import org.openecomp.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.client.filter.LoggingFilter;
39 import com.sun.jersey.api.json.JSONConfiguration;
40 import com.sun.jersey.client.urlconnection.HTTPSProperties;
42 public class HttpsAuthClient{
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 Client client = HttpsAuthClient.getClient();
55 ClientResponse res = client.resource(url)
56 .accept("application/json")
57 .header("X-TransactionId", "PROV001")
58 .header("X-FromAppId", "AAI")
59 .type("application/json")
60 .get(ClientResponse.class);
62 // System.out.println("Jersey result: ");
63 // System.out.println(res.getEntity(String.class).toString());
65 Customers customers = res.getEntity(Customers.class);
66 System.out.println("Jersey result: ");
67 System.out.println("Number of customers: " + customers.getCustomer().size());
69 } catch (KeyManagementException e) {
71 } catch (Exception e) {
81 * @throws KeyManagementException the key management exception
83 public static Client getClient() throws KeyManagementException {
85 ClientConfig config = new DefaultClientConfig();
86 config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
87 config.getClasses().add(org.openecomp.aai.restcore.CustomJacksonJaxBJsonProvider.class);
89 SSLContext ctx = null;
91 String truststore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME);
92 String truststore_password = AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_PASSWD);
93 String keystore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_KEYSTORE_FILENAME);
94 String keystore_password = AAIConfig.get(AAIConstants.AAI_KEYSTORE_PASSWD);
96 System.setProperty("javax.net.ssl.trustStore", truststore_path);
97 System.setProperty("javax.net.ssl.trustStorePassword", truststore_password);
98 HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
99 public boolean verify(String string,SSLSession ssls) {
104 ctx = SSLContext.getInstance("TLSv1.2");
105 KeyManagerFactory kmf = null;
107 kmf = KeyManagerFactory.getInstance("SunX509");
108 FileInputStream fin = new FileInputStream(keystore_path);
109 KeyStore ks = KeyStore.getInstance("PKCS12");
110 char[] pwd = keystore_password.toCharArray();
113 } catch (Exception e) {
114 System.out.println("Error setting up kmf: exiting");
119 ctx.init(kmf.getKeyManagers(), null, null);
120 config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
121 new HTTPSProperties( new HostnameVerifier() {
123 public boolean verify( String s, SSLSession sslSession ) {
127 } catch (Exception e) {
128 System.out.println("Error setting up config: exiting");
133 Client client = Client.create(config);
134 // uncomment this line to get more logging for the request/response
135 // client.addFilter(new LoggingFilter(System.out));