MicroserviceParameter class DB constraints
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / scheduler / client / HttpsBasicClient.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39 package org.onap.portalapp.portal.scheduler.client;
40
41 import java.io.File;
42 import java.text.DateFormat;
43 import java.text.SimpleDateFormat;
44 import java.util.Date;
45
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
53 import org.eclipse.jetty.util.security.Password;
54 import org.glassfish.jersey.client.ClientConfig;
55 import org.glassfish.jersey.client.ClientProperties;
56 import org.onap.portalapp.portal.scheduler.SchedulerProperties;
57 import org.onap.portalapp.portal.scheduler.util.CustomJacksonJaxBJsonProvider;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.onap.portalsdk.core.util.SystemProperties;
60
61  /**
62   *  General SSL client using the VID tomcat keystore. It doesn't use client certificates.
63   */
64  
65 public class HttpsBasicClient{
66         
67         /** The logger. */
68         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
69         
70         /** The Constant dateFormat. */
71         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
72         
73         /**
74          * Retrieve an SSL client.
75          *
76          * @return Client The SSL client
77          * @throws Exception the exception
78          */
79         public static Client getClient() throws Exception {
80                 String methodName = "getClient";
81                 ClientConfig config = new ClientConfig();
82                 //config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
83                 //config.getClasses().add(org.onap.aai.util.CustomJacksonJaxBJsonProvider.class);
84         
85                 SSLContext ctx = null;
86                 
87                 try {
88                         
89                         config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
90                         
91                         String truststore_path = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_FILENAME);
92                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " truststore_path=" + truststore_path);
93                         String truststore_password = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_PASSWD_X);
94                         
95                         
96                         String decrypted_truststore_password = Password.deobfuscate(truststore_password);
97                         //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
98                         
99                         File tr = new File (truststore_path);
100                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute truststore path=" + tr.getAbsolutePath());
101                         
102                         //String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
103                         //String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
104                         //String decrypted_keystore_password = EncryptedPropValue.decryptTriple(keystore_password);
105                         
106                     System.setProperty("javax.net.ssl.trustStore", truststore_path);
107                     System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
108                         HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
109                             public boolean verify(String string,SSLSession ssls) {
110                                 return true;
111                             }
112                         });
113         
114                         //May need to make the algorithm a parameter. MSO requires TLSv1.1      or TLSv1.2
115                         ctx = SSLContext.getInstance("TLSv1.2");
116                         
117                         /* 
118                         KeyManagerFactory kmf = null;
119                         try {
120                                 kmf = KeyManagerFactory.getInstance("SunX509");
121                                 FileInputStream fin = new FileInputStream(keystore_path);
122                                 KeyStore ks = KeyStore.getInstance("PKCS12");
123                                 char[] pwd = decrypted_keystore_password.toCharArray();
124                                 ks.load(fin, pwd);
125                                 kmf.init(ks, pwd);
126                         } catch (Exception e) {
127                                 System.out.println("Error setting up kmf: exiting");
128                                 e.printStackTrace();
129                                 System.exit(1);
130                         }
131
132                         ctx.init(kmf.getKeyManagers(), null, null);
133                         */
134                         ctx.init(null, null, null);
135                         //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, 
136                         //                                                      new HTTPSProperties( , ctx));
137                         
138                         return ClientBuilder.newBuilder()
139                                 .sslContext(ctx)
140                                 .hostnameVerifier(new HostnameVerifier() {
141                                         @Override
142                                         public boolean verify( String s, SSLSession sslSession ) {
143                                                 return true;
144                                         }
145                                 }).withConfig(config)
146                                 .build()
147                                 .register(CustomJacksonJaxBJsonProvider.class);
148                         
149                 } catch (Exception e) {
150                         logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
151                         //System.out.println("Error setting up config: exiting");
152                         e.printStackTrace();
153                         return null;
154                 }
155                         
156                 //Client client = ClientBuilder.newClient(config);
157                 // uncomment this line to get more logging for the request/response
158                 // client.addFilter(new LoggingFilter(System.out));
159                 
160                 //return client;
161         }
162 }