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.portalapp.portal.scheduler.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.openecomp.portalapp.portal.scheduler.properties.VidProperties;
39 import org.openecomp.portalapp.portal.scheduler.util.CustomJacksonJaxBJsonProvider;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.openecomp.portalsdk.core.util.SystemProperties;
44 * General SSL client using the VID tomcat keystore. It doesn't use client certificates.
47 public class HttpsBasicClient{
50 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
52 /** The Constant dateFormat. */
53 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
56 * Retrieve an SSL client.
58 * @return Client The SSL client
59 * @throws Exception the exception
61 public static Client getClient() throws Exception {
62 String methodName = "getClient";
63 ClientConfig config = new ClientConfig();
64 //config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
65 //config.getClasses().add(org.openecomp.aai.util.CustomJacksonJaxBJsonProvider.class);
67 SSLContext ctx = null;
71 config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
73 String truststore_path = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_FILENAME);
74 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " truststore_path=" + truststore_path);
75 String truststore_password = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_PASSWD_X);
78 String decrypted_truststore_password = Password.deobfuscate(truststore_password);
79 //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
81 File tr = new File (truststore_path);
82 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute truststore path=" + tr.getAbsolutePath());
84 //String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
85 //String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
86 //String decrypted_keystore_password = EncryptedPropValue.decryptTriple(keystore_password);
88 System.setProperty("javax.net.ssl.trustStore", truststore_path);
89 System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
90 HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
91 public boolean verify(String string,SSLSession ssls) {
96 //May need to make the algorithm a parameter. MSO requires TLSv1.1 or TLSv1.2
97 ctx = SSLContext.getInstance("TLSv1.2");
100 KeyManagerFactory kmf = null;
102 kmf = KeyManagerFactory.getInstance("SunX509");
103 FileInputStream fin = new FileInputStream(keystore_path);
104 KeyStore ks = KeyStore.getInstance("PKCS12");
105 char[] pwd = decrypted_keystore_password.toCharArray();
108 } catch (Exception e) {
109 System.out.println("Error setting up kmf: exiting");
114 ctx.init(kmf.getKeyManagers(), null, null);
116 ctx.init(null, null, null);
117 //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
118 // new HTTPSProperties( , ctx));
120 return ClientBuilder.newBuilder()
122 .hostnameVerifier(new HostnameVerifier() {
124 public boolean verify( String s, SSLSession sslSession ) {
127 }).withConfig(config)
129 .register(CustomJacksonJaxBJsonProvider.class);
131 } catch (Exception e) {
132 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
133 //System.out.println("Error setting up config: exiting");
138 //Client client = ClientBuilder.newClient(config);
139 // uncomment this line to get more logging for the request/response
140 // client.addFilter(new LoggingFilter(System.out));