Merge "Logging improvements"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / HttpsComponentsClient.java
index 1002221..d1f1cfc 100644 (file)
 
 package org.onap.vid.aai.util;
 
-import java.io.FileInputStream;
-import java.security.KeyManagementException;
-import java.security.KeyStore;
-
-import javax.net.ssl.SSLContext;
-
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.conn.ssl.SSLContextBuilder;
 import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.impl.client.HttpClients;
 import org.eclipse.jetty.util.security.Password;
+import org.onap.vid.exceptions.GenericUncheckedException;
+import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.onap.portalsdk.core.util.SystemProperties;
 
+import javax.net.ssl.SSLContext;
+import java.io.FileInputStream;
+import java.security.GeneralSecurityException;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+
 
 /**
  * The Class HttpsComponentsClient.
  */
 public class HttpsComponentsClient{
-       
+
+       static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsComponentsClient.class);
+
        /**
         * Gets the client.
         *
@@ -46,10 +50,10 @@ public class HttpsComponentsClient{
         * @return the client
         * @throws KeyManagementException the key management exception
         */
-       public static CloseableHttpClient getClient(String certFilePath) throws Exception {
+       public static CloseableHttpClient getClient(String certFilePath) {
                CloseableHttpClient httpclient = null;
                try {
-                       
+
                        String truststore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_FILENAME);
                        String truststore_password = SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_PASSWD_X);
                        String decrypted_truststore_password = Password.deobfuscate(truststore_password);
@@ -60,17 +64,31 @@ public class HttpsComponentsClient{
                        SSLContextBuilder sslContextB = new SSLContextBuilder();
                        
                        KeyStore ks = KeyStore.getInstance("PKCS12");
-                       FileInputStream fin = new FileInputStream(keystore_path);
                        char[] pwd = decrypted_keystore_password.toCharArray();
-                       ks.load(fin, pwd);
-                       
+
+                       try(FileInputStream fin = new FileInputStream(keystore_path)) {
+                               ks.load(fin, pwd);
+                       }
+                       catch (Exception e) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up keystore");
+                               logger.error(EELFLoggerDelegate.errorLogger, "Error loading  keystore materials: (keystore path: {}, obfuascated keystore password: {})", keystore_path, keystore_password);
+                               throw new GenericUncheckedException(e);
+                       }
+
                        sslContextB.loadKeyMaterial(ks, pwd);
                        
                        KeyStore ts = KeyStore.getInstance("JKS");
-                       FileInputStream fin1 = new FileInputStream(truststore_path);
                        char[] pwd1 = decrypted_truststore_password.toCharArray();
-                       ts.load(fin1, pwd1);
-                       
+
+                       try(FileInputStream fin1 = new FileInputStream(truststore_path)) {
+                               ts.load(fin1, pwd1);
+                       }
+                       catch (Exception e) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up truststore");
+                               logger.error(EELFLoggerDelegate.errorLogger, "Error loading  truststore materials: (truststore path: {}, obfuascated truststore password: {})", truststore_path, truststore_password);
+                               throw new GenericUncheckedException(e);
+                       }
+
                        sslContextB.loadTrustMaterial(ts);
                        sslContextB.loadKeyMaterial(ks, pwd);
                        sslContextB.useTLS();
@@ -88,8 +106,8 @@ public class HttpsComponentsClient{
                        .build();
 
 
-               } catch (Exception e) {
-                       throw e;
+               } catch (GeneralSecurityException e) {
+                       throw new GenericUncheckedException(e);
                }
                return httpclient;
        }