be24486512491cbd2d9975f26f1d5812ded87131
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / scheduler / 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.openecomp.portalapp.portal.scheduler.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.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;
42
43  /**
44   *  General SSL client using the VID tomcat keystore. It doesn't use client certificates.
45   */
46  
47 public class HttpsBasicClient{
48         
49         /** The logger. */
50         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
51         
52         /** The Constant dateFormat. */
53         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
54         
55         /**
56          * Retrieve an SSL client.
57          *
58          * @return Client The SSL client
59          * @throws Exception the exception
60          */
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);
66         
67                 SSLContext ctx = null;
68                 
69                 try {
70                         
71                         config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
72                         
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);
76                         
77                         
78                         String decrypted_truststore_password = Password.deobfuscate(truststore_password);
79                         //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
80                         
81                         File tr = new File (truststore_path);
82                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute truststore path=" + tr.getAbsolutePath());
83                         
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);
87                         
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) {
92                                 return true;
93                             }
94                         });
95         
96                         //May need to make the algorithm a parameter. MSO requires TLSv1.1      or TLSv1.2
97                         ctx = SSLContext.getInstance("TLSv1.2");
98                         
99                         /* 
100                         KeyManagerFactory kmf = null;
101                         try {
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();
106                                 ks.load(fin, pwd);
107                                 kmf.init(ks, pwd);
108                         } catch (Exception e) {
109                                 System.out.println("Error setting up kmf: exiting");
110                                 e.printStackTrace();
111                                 System.exit(1);
112                         }
113
114                         ctx.init(kmf.getKeyManagers(), null, null);
115                         */
116                         ctx.init(null, null, null);
117                         //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, 
118                         //                                                      new HTTPSProperties( , ctx));
119                         
120                         return ClientBuilder.newBuilder()
121                                 .sslContext(ctx)
122                                 .hostnameVerifier(new HostnameVerifier() {
123                                         @Override
124                                         public boolean verify( String s, SSLSession sslSession ) {
125                                                 return true;
126                                         }
127                                 }).withConfig(config)
128                                 .build()
129                                 .register(CustomJacksonJaxBJsonProvider.class);
130                         
131                 } catch (Exception e) {
132                         logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
133                         //System.out.println("Error setting up config: exiting");
134                         e.printStackTrace();
135                         return null;
136                 }
137                         
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));
141                 
142                 //return client;
143         }
144 }