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