Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / HttpsAuthClient.java
index 133c26a..c2bfabf 100644 (file)
@@ -27,10 +27,13 @@ import com.sun.jersey.api.client.config.DefaultClientConfig;
 import com.sun.jersey.api.client.filter.LoggingFilter;
 import com.sun.jersey.api.json.JSONConfiguration;
 import com.sun.jersey.client.urlconnection.HTTPSProperties;
+import org.onap.aai.aailog.filter.RestControllerClientLoggingInterceptor;
+import org.onap.aai.exceptions.AAIException;
 
 import java.io.FileInputStream;
-import java.security.KeyManagementException;
-import java.security.KeyStore;
+import java.io.IOException;
+import java.security.*;
+import java.security.cert.CertificateException;
 
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
@@ -63,29 +66,25 @@ public class HttpsAuthClient {
             e.printStackTrace();
         }
     }
-
     /**
      * Gets the client.
      *
+     * @param truststorePath the truststore path
+     * @param truststorePassword the truststore password
+     * @param keystorePath the keystore path
+     * @param keystorePassword the keystore password
      * @return the client
      * @throws KeyManagementException the key management exception
      */
-    public static Client getClient() throws KeyManagementException {
+    public static Client getClient(String truststorePath, String truststorePassword, String keystorePath, String keystorePassword) throws KeyManagementException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
 
         ClientConfig config = new DefaultClientConfig();
         config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
         config.getClasses().add(org.onap.aai.restcore.CustomJacksonJaxBJsonProvider.class);
-
         SSLContext ctx = null;
         try {
-            String truststore_path =
-                    AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME);
-            String truststore_password = AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_PASSWD);
-            String keystore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_KEYSTORE_FILENAME);
-            String keystore_password = AAIConfig.get(AAIConstants.AAI_KEYSTORE_PASSWD);
-
-            System.setProperty("javax.net.ssl.trustStore", truststore_path);
-            System.setProperty("javax.net.ssl.trustStorePassword", truststore_password);
+            System.setProperty("javax.net.ssl.trustStore", truststorePath);
+            System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
             HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                 public boolean verify(String string, SSLSession ssls) {
                     return true;
@@ -96,36 +95,62 @@ public class HttpsAuthClient {
             KeyManagerFactory kmf = null;
             try {
                 kmf = KeyManagerFactory.getInstance("SunX509");
-                FileInputStream fin = new FileInputStream(keystore_path);
+                FileInputStream fin = new FileInputStream(keystorePath);
                 KeyStore ks = KeyStore.getInstance("PKCS12");
-                char[] pwd = keystore_password.toCharArray();
+                char[] pwd = keystorePassword.toCharArray();
                 ks.load(fin, pwd);
                 kmf.init(ks, pwd);
             } catch (Exception e) {
                 System.out.println("Error setting up kmf: exiting");
                 e.printStackTrace();
-                System.exit(1);
+                throw e;
+                //System.exit(1);
             }
 
             ctx.init(kmf.getKeyManagers(), null, null);
             config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
-                    new HTTPSProperties(new HostnameVerifier() {
-                        @Override
-                        public boolean verify(String s, SSLSession sslSession) {
-                            return true;
-                        }
-                    }, ctx));
+                new HTTPSProperties(new HostnameVerifier() {
+                    @Override
+                    public boolean verify(String s, SSLSession sslSession) {
+                        return true;
+                    }
+                }, ctx));
         } catch (Exception e) {
             System.out.println("Error setting up config: exiting");
             e.printStackTrace();
-            System.exit(1);
+            throw e;
+            //System.exit(1);
         }
 
         Client client = Client.create(config);
+        client.addFilter(new RestControllerClientLoggingInterceptor());
         // uncomment this line to get more logging for the request/response
         // client.addFilter(new LoggingFilter(System.out));
 
         return client;
     }
+    /**
+     * Gets the client.
+     *
+     * @return the client
+     * @throws KeyManagementException the key management exception
+     */
+    public static Client getClient() throws KeyManagementException, AAIException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
+        String truststore_path = null;
+        String truststore_password = null;
+        String keystore_path = null;
+        String keystore_password = null;
+        try {
+            truststore_path =
+                AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME);
+            truststore_password = AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_PASSWD);
+            keystore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_KEYSTORE_FILENAME);
+            keystore_password = AAIConfig.get(AAIConstants.AAI_KEYSTORE_PASSWD);
+        }
+        catch (AAIException e) {
+            throw e;
+        }
+        return(getClient(truststore_path, truststore_password, keystore_path, keystore_password));
+    }
 
 }