SchedulerController up
[portal.git] / portal-BE / src / main / java / org / onap / portal / scheduler / client / HttpsBasicClient.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.scheduler.client;
42
43 import java.io.File;
44 import java.text.SimpleDateFormat;
45 import java.util.Date;
46 import javax.net.ssl.HostnameVerifier;
47 import javax.net.ssl.HttpsURLConnection;
48 import javax.net.ssl.SSLContext;
49 import javax.net.ssl.SSLSession;
50 import javax.ws.rs.client.Client;
51 import javax.ws.rs.client.ClientBuilder;
52 import org.glassfish.jersey.client.ClientConfig;
53 import org.glassfish.jersey.client.ClientProperties;
54 import org.onap.portal.scheduler.SchedulerProperties;
55 import org.eclipse.jetty.util.security.Password;
56 import org.onap.portal.scheduler.util.CustomJacksonJaxBJsonProvider;
57 import org.onap.portal.utils.DateUtil;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59
60 public class HttpsBasicClient {
61
62     static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
63
64     public static Client getClient() throws Exception {
65         String methodName = "getClient";
66         ClientConfig config = new ClientConfig();
67
68         SSLContext ctx = null;
69
70         try {
71
72             SimpleDateFormat dateFormat = DateUtil.getDateFormat();
73             config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
74
75             String truststore_path = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_FILENAME);
76             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " "
77                 + "truststore_path=" +
78                 truststore_path);
79             String truststore_password = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_PASSWD_X);
80
81             String decrypted_truststore_password = Password.deobfuscate(truststore_password);
82
83             File tr = new File(truststore_path);
84             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute "
85                 + "truststore path=" + tr.getAbsolutePath());
86             System.setProperty("javax.net.ssl.trustStore", truststore_path);
87             System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
88             HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
89                 public boolean verify(String string, SSLSession ssls) {
90                     return true;
91                 }
92             });
93             ctx = SSLContext.getInstance("TLSv1.2");
94             ctx.init(null, null, null);
95
96             return ClientBuilder.newBuilder()
97                 .sslContext(ctx)
98                 .hostnameVerifier(new HostnameVerifier() {
99                     @Override
100                     public boolean verify(String s, SSLSession sslSession) {
101                         return true;
102                     }
103                 }).withConfig(config)
104                 .build()
105                 .register(CustomJacksonJaxBJsonProvider.class);
106
107         } catch (Exception e) {
108             logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
109             e.printStackTrace();
110             return null;
111         }
112     }
113 }