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.vid.client;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
28 import javax.net.ssl.HostnameVerifier;
29 import javax.net.ssl.HttpsURLConnection;
30 import javax.net.ssl.SSLContext;
31 import javax.net.ssl.SSLSession;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
35 import org.eclipse.jetty.util.security.Password;
36 import org.glassfish.jersey.client.ClientConfig;
37 import org.glassfish.jersey.client.ClientProperties;
38 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.onap.portalsdk.core.util.SystemProperties;
40 import org.onap.vid.properties.VidProperties;
43 * General SSL client using the VID tomcat keystore. It doesn't use client certificates.
46 public class HttpsBasicClient{
49 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
51 /** The Constant dateFormat. */
52 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
55 * Retrieve an SSL client.
57 * @return Client The SSL client
58 * @throws Exception the exception
60 public static Client getClient() throws Exception {
61 String methodName = "getClient";
62 ClientConfig config = new ClientConfig();
63 //config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
64 //config.getClasses().add(org.onap.aai.util.CustomJacksonJaxBJsonProvider.class);
66 SSLContext ctx = null;
70 config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
72 String truststore_path = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_FILENAME);
73 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " truststore_path=" + truststore_path);
74 String truststore_password = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_PASSWD_X);
77 String decrypted_truststore_password = Password.deobfuscate(truststore_password);
78 //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
80 File tr = new File (truststore_path);
81 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute truststore path=" + tr.getAbsolutePath());
83 //String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
84 //String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
85 //String decrypted_keystore_password = EncryptedPropValue.decryptTriple(keystore_password);
87 System.setProperty("javax.net.ssl.trustStore", truststore_path);
88 System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
89 HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
90 public boolean verify(String string,SSLSession ssls) {
95 //May need to make the algorithm a parameter. MSO requires TLSv1.1 or TLSv1.2
96 ctx = SSLContext.getInstance("TLSv1.2");
99 KeyManagerFactory kmf = null;
101 kmf = KeyManagerFactory.getInstance("SunX509");
102 FileInputStream fin = new FileInputStream(keystore_path);
103 KeyStore ks = KeyStore.getInstance("PKCS12");
104 char[] pwd = decrypted_keystore_password.toCharArray();
107 } catch (Exception e) {
108 System.out.println("Error setting up kmf: exiting");
113 ctx.init(kmf.getKeyManagers(), null, null);
115 ctx.init(null, null, null);
116 //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
117 // new HTTPSProperties( , ctx));
119 return ClientBuilder.newBuilder()
121 .hostnameVerifier(new HostnameVerifier() {
123 public boolean verify( String s, SSLSession sslSession ) {
126 }).withConfig(config)
128 .register(org.onap.vid.aai.util.CustomJacksonJaxBJsonProvider.class);
130 } catch (Exception e) {
131 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
132 //System.out.println("Error setting up config: exiting");
137 //Client client = ClientBuilder.newClient(config);
138 // uncomment this line to get more logging for the request/response
139 // client.addFilter(new LoggingFilter(System.out));