Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / HttpsAuthClient.java
index c2bfabf..f8f0b65 100644 (file)
@@ -24,15 +24,16 @@ import com.sun.jersey.api.client.Client;
 import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.api.client.config.ClientConfig;
 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.io.IOException;
-import java.security.*;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
 import java.security.cert.CertificateException;
 
 import javax.net.ssl.HostnameVerifier;
@@ -41,8 +42,15 @@ import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSession;
 
+import org.onap.aai.aailog.filter.RestControllerClientLoggingInterceptor;
+import org.onap.aai.exceptions.AAIException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 public class HttpsAuthClient {
 
+    private static final Logger logger = LoggerFactory.getLogger(HttpsAuthClient.class);
+
     /**
      * The main method.
      *
@@ -61,11 +69,12 @@ public class HttpsAuthClient {
             // System.out.println(res.getEntity(String.class).toString());
 
         } catch (KeyManagementException e) {
-            e.printStackTrace();
+            logger.debug("HttpsAuthClient KeyManagement error : {}", e.getMessage());
         } catch (Exception e) {
-            e.printStackTrace();
+            logger.debug("HttpsAuthClient error : {}", e.getMessage());
         }
     }
+
     /**
      * Gets the client.
      *
@@ -76,7 +85,9 @@ public class HttpsAuthClient {
      * @return the client
      * @throws KeyManagementException the key management exception
      */
-    public static Client getClient(String truststorePath, String truststorePassword, String keystorePath, String keystorePassword) throws KeyManagementException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
+    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);
@@ -93,33 +104,29 @@ public class HttpsAuthClient {
 
             ctx = SSLContext.getInstance("TLSv1.2");
             KeyManagerFactory kmf = null;
-            try {
+
+            try (FileInputStream fin = new FileInputStream(keystorePath)) {
                 kmf = KeyManagerFactory.getInstance("SunX509");
-                FileInputStream fin = new FileInputStream(keystorePath);
                 KeyStore ks = KeyStore.getInstance("PKCS12");
                 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.out.println("Error setting up kmf: exiting " + e.getMessage());
                 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.out.println("Error setting up config: exiting " + e.getMessage());
             throw e;
-            //System.exit(1);
         }
 
         Client client = Client.create(config);
@@ -129,28 +136,24 @@ public class HttpsAuthClient {
 
         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));
+    public static Client getClient() throws KeyManagementException, AAIException, UnrecoverableKeyException,
+            CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
+        String truststorePath = null;
+        String truststorePassword = null;
+        String keystorePath = null;
+        String keystorePassword = null;
+        truststorePath = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME);
+        truststorePassword = AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_PASSWD);
+        keystorePath = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_KEYSTORE_FILENAME);
+        keystorePassword = AAIConfig.get(AAIConstants.AAI_KEYSTORE_PASSWD);
+        return getClient(truststorePath, truststorePassword, keystorePath, keystorePassword);
     }
 
 }