blob: b0fceb74578bbdb0b6b5983dd38c281cd6361119 [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20
21
22
23package org.onap.osam.client;
24
25import org.eclipse.jetty.util.security.Password;
26import org.glassfish.jersey.client.ClientConfig;
27import org.glassfish.jersey.client.ClientProperties;
28import org.onap.osam.properties.VidProperties;
29import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30import org.onap.portalsdk.core.util.SystemProperties;
31
32import javax.net.ssl.HostnameVerifier;
33import javax.net.ssl.HttpsURLConnection;
34import javax.net.ssl.SSLContext;
35import javax.net.ssl.SSLSession;
36import javax.ws.rs.client.Client;
37import javax.ws.rs.client.ClientBuilder;
38import java.io.File;
39import java.text.DateFormat;
40import java.text.SimpleDateFormat;
41import java.util.Date;
42
43
44public class HttpsBasicClient{
45
46 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
47
48 /** The Constant dateFormat. */
49 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
50
51 /**
52 * Retrieve an SSL client.
53 *
54 * @return Client The SSL client
55 * @throws Exception the exception
56 */
57 public static Client getClient() {
58 String methodName = "getClient";
59 ClientConfig config = new ClientConfig();
60
61 SSLContext ctx = null;
62
63 try {
64
65 config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
66
67 String truststore_path = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_FILENAME);
68 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " truststore_path=" + truststore_path);
69 String truststore_password = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_PASSWD_X);
70
71
72 String decrypted_truststore_password = Password.deobfuscate(truststore_password);
73 //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
74
75 File tr = new File (truststore_path);
76 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute truststore path=" + tr.getAbsolutePath());
77
78 System.setProperty("javax.net.ssl.trustStore", truststore_path);
79 System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
80 HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
81 public boolean verify(String string,SSLSession ssls) {
82 return true;
83 }
84 });
85
86 //May need to make the algorithm a parameter. MSO requires TLSv1.1 or TLSv1.2
87 ctx = SSLContext.getInstance("TLSv1.2");
88
89 ctx.init(null, null, null);
90 //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
91 // new HTTPSProperties( , ctx));
92
93 return ClientBuilder.newBuilder()
94 .sslContext(ctx)
95 .hostnameVerifier(new HostnameVerifier() {
96 @Override
97 public boolean verify( String s, SSLSession sslSession ) {
98 return true;
99 }
100 }).withConfig(config)
101 .build()
102 .register(org.onap.osam.aai.util.CustomJacksonJaxBJsonProvider.class);
103
104 } catch (Exception e) {
105 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting", e);
106 return null;
107 }
108 }
109}