2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.vid.client;
\r
23 import java.io.File;
\r
24 import java.text.DateFormat;
\r
25 import java.text.SimpleDateFormat;
\r
26 import java.util.Date;
\r
28 import javax.net.ssl.HostnameVerifier;
\r
29 import javax.net.ssl.HttpsURLConnection;
\r
30 import javax.net.ssl.SSLContext;
\r
31 import javax.net.ssl.SSLSession;
\r
32 import javax.ws.rs.client.Client;
\r
33 import javax.ws.rs.client.ClientBuilder;
\r
35 import org.eclipse.jetty.util.security.Password;
\r
36 import org.glassfish.jersey.client.ClientConfig;
\r
37 import org.glassfish.jersey.client.ClientProperties;
\r
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
39 import org.openecomp.portalsdk.core.util.SystemProperties;
\r
40 import org.openecomp.vid.properties.VidProperties;
\r
43 * General SSL client using the VID tomcat keystore. It doesn't use client certificates.
\r
46 public class HttpsBasicClient{
\r
49 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
\r
51 /** The Constant dateFormat. */
\r
52 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
\r
55 * Retrieve an SSL client.
\r
57 * @return Client The SSL client
\r
58 * @throws Exception the exception
\r
60 public static Client getClient() throws Exception {
\r
61 String methodName = "getClient";
\r
62 ClientConfig config = new ClientConfig();
\r
63 //config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
\r
64 //config.getClasses().add(org.openecomp.aai.util.CustomJacksonJaxBJsonProvider.class);
\r
66 SSLContext ctx = null;
\r
70 config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
\r
72 String truststore_path = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_FILENAME);
\r
73 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " truststore_path=" + truststore_path);
\r
74 String truststore_password = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_PASSWD_X);
\r
77 String decrypted_truststore_password = Password.deobfuscate(truststore_password);
\r
78 //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
\r
80 File tr = new File (truststore_path);
\r
81 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute truststore path=" + tr.getAbsolutePath());
\r
83 //String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
\r
84 //String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
\r
85 //String decrypted_keystore_password = EncryptedPropValue.decryptTriple(keystore_password);
\r
87 System.setProperty("javax.net.ssl.trustStore", truststore_path);
\r
88 System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
\r
89 HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
\r
90 public boolean verify(String string,SSLSession ssls) {
\r
95 //May need to make the algorithm a parameter. MSO requires TLSv1.1 or TLSv1.2
\r
96 ctx = SSLContext.getInstance("TLSv1.2");
\r
99 KeyManagerFactory kmf = null;
\r
101 kmf = KeyManagerFactory.getInstance("SunX509");
\r
102 FileInputStream fin = new FileInputStream(keystore_path);
\r
103 KeyStore ks = KeyStore.getInstance("PKCS12");
\r
104 char[] pwd = decrypted_keystore_password.toCharArray();
\r
107 } catch (Exception e) {
\r
108 System.out.println("Error setting up kmf: exiting");
\r
109 e.printStackTrace();
\r
113 ctx.init(kmf.getKeyManagers(), null, null);
\r
115 ctx.init(null, null, null);
\r
116 //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
\r
117 // new HTTPSProperties( , ctx));
\r
119 return ClientBuilder.newBuilder()
\r
121 .hostnameVerifier(new HostnameVerifier() {
\r
123 public boolean verify( String s, SSLSession sslSession ) {
\r
126 }).withConfig(config)
\r
128 .register(org.openecomp.aai.util.CustomJacksonJaxBJsonProvider.class);
\r
130 } catch (Exception e) {
\r
131 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
\r
132 //System.out.println("Error setting up config: exiting");
\r
133 e.printStackTrace();
\r
137 //Client client = ClientBuilder.newClient(config);
\r
138 // uncomment this line to get more logging for the request/response
\r
139 // client.addFilter(new LoggingFilter(System.out));
\r