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