org.onap migration
[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 java.io.File;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27
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;
34
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.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.onap.vid.properties.VidProperties;
41
42  /**
43   *  General SSL client using the VID tomcat keystore. It doesn't use client certificates.
44   */
45  
46 public class HttpsBasicClient{
47         
48         /** The logger. */
49         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
50         
51         /** The Constant dateFormat. */
52         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
53         
54         /**
55          * Retrieve an SSL client.
56          *
57          * @return Client The SSL client
58          * @throws Exception the exception
59          */
60         public static Client getClient() throws Exception {
61                 String methodName = "getClient";
62                 ClientConfig config = new ClientConfig();
63                 //config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
64                 //config.getClasses().add(org.openecomp.aai.util.CustomJacksonJaxBJsonProvider.class);
65         
66                 SSLContext ctx = null;
67                 
68                 try {
69                         
70                         config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
71                         
72                         String truststore_path = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_FILENAME);
73                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " truststore_path=" + truststore_path);
74                         String truststore_password = SystemProperties.getProperty(VidProperties.VID_TRUSTSTORE_PASSWD_X);
75                         
76                         
77                         String decrypted_truststore_password = Password.deobfuscate(truststore_password);
78                         //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
79                         
80                         File tr = new File (truststore_path);
81                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute truststore path=" + tr.getAbsolutePath());
82                         
83                         //String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
84                         //String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
85                         //String decrypted_keystore_password = EncryptedPropValue.decryptTriple(keystore_password);
86                         
87                     System.setProperty("javax.net.ssl.trustStore", truststore_path);
88                     System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
89                         HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
90                             public boolean verify(String string,SSLSession ssls) {
91                                 return true;
92                             }
93                         });
94         
95                         //May need to make the algorithm a parameter. MSO requires TLSv1.1      or TLSv1.2
96                         ctx = SSLContext.getInstance("TLSv1.2");
97                         
98                         /*
99                         KeyManagerFactory kmf = null;
100                         try {
101                                 kmf = KeyManagerFactory.getInstance("SunX509");
102                                 FileInputStream fin = new FileInputStream(keystore_path);
103                                 KeyStore ks = KeyStore.getInstance("PKCS12");
104                                 char[] pwd = decrypted_keystore_password.toCharArray();
105                                 ks.load(fin, pwd);
106                                 kmf.init(ks, pwd);
107                         } catch (Exception e) {
108                                 System.out.println("Error setting up kmf: exiting");
109                                 e.printStackTrace();
110                                 System.exit(1);
111                         }
112
113                         ctx.init(kmf.getKeyManagers(), null, null);
114                         */
115                         ctx.init(null, null, null);
116                         //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
117                         //                                                      new HTTPSProperties( , ctx));
118                         
119                         return ClientBuilder.newBuilder()
120                                 .sslContext(ctx)
121                                 .hostnameVerifier(new HostnameVerifier() {
122                                         @Override
123                                         public boolean verify( String s, SSLSession sslSession ) {
124                                                 return true;
125                                         }
126                                 }).withConfig(config)
127                                 .build()
128                                 .register(org.onap.vid.aai.util.CustomJacksonJaxBJsonProvider.class);
129                         
130                 } catch (Exception e) {
131                         logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
132                         //System.out.println("Error setting up config: exiting");
133                         e.printStackTrace();
134                         return null;
135                 }
136                         
137                 //Client client = ClientBuilder.newClient(config);
138                 // uncomment this line to get more logging for the request/response
139                 // client.addFilter(new LoggingFilter(System.out));
140                 
141                 //return client;
142         }
143 }