fixed sonar issues in AAIClientRESTExecutor
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / AAIClientRESTExecutor.java
index 906cd83..dd39ba9 100755 (executable)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017 AT&T Intellectual Property. All rights
  *                         reserved.
  * ================================================================================
+ * Modifications Copyright (C) 2018 IBM.
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -48,7 +50,6 @@ import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSession;
 import javax.net.ssl.SSLSocketFactory;
-import javax.ws.rs.HttpMethod;
 
 import org.apache.commons.codec.binary.Base64;
 import org.onap.ccsdk.sli.adaptors.aai.AAIService.TransactionIdTracker;
@@ -63,14 +64,13 @@ import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.sun.jersey.api.client.config.DefaultClientConfig;
-import com.sun.jersey.client.urlconnection.HTTPSProperties;
+import org.apache.http.impl.EnglishReasonPhraseCatalog;
 
 /**
  * The AAIClientRESTExecutor class provides CRUD API for AAI Client service.
  * @author  richtabedzki
  */
-public     class AAIClientRESTExecutor implements AAIExecutorInterface {
+public class AAIClientRESTExecutor implements AAIExecutorInterface {
 
     private final String truststorePath;
     private final String truststorePassword;
@@ -81,6 +81,7 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
     private String userName;
     private String userPassword;
     private final String applicationId;
+    private static final String HTTP_URL_CONNECTION_RESULT="HttpURLConnection result: {} : {}";
 
     /**
      * class Constructor
@@ -118,10 +119,9 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
 
         ignoreCertificateHostError = host_error;
 
-        HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
-            public boolean verify(String string,SSLSession ssls) {
-                return ignoreCertificateHostError;
-            }
+        HttpsURLConnection.setDefaultHostnameVerifier( (String string,SSLSession ssls)  -> {
+             return ignoreCertificateHostError;
+            
         });
 
         if(truststorePath != null && truststorePassword != null && (new File(truststorePath)).exists()) {
@@ -131,18 +131,16 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
 
         if(keystorePath != null && keystorePassword != null && (new File(keystorePath)).exists())
         {
-            DefaultClientConfig config = new DefaultClientConfig();
             //both jersey and HttpURLConnection can use this
             SSLContext ctx = null;
             try {
                 ctx = SSLContext.getInstance("TLS");
 
                 KeyManagerFactory kmf = null;
-                try {
+                try (FileInputStream fin = new FileInputStream(keystorePath)){
                     String storeType = "PKCS12";
                     String def = KeyStore.getDefaultType();
                     kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
-                    FileInputStream fin = new FileInputStream(keystorePath);
 
                     String extension = keystorePath.substring(keystorePath.lastIndexOf(".") + 1);
 
@@ -159,12 +157,6 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
                 }
 
                 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 ignoreCertificateHostError;
-                        }
-                }, ctx));
 
                 CTX = ctx;
                 LOG.debug("SSLContext created");
@@ -200,6 +192,7 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
     }
 
     private static final Logger LOG = LoggerFactory.getLogger(AAIService.class);
+    private static final String NOT_PROVIDED = "NOT PROVIDED";
     private final MetricLogger ml = new MetricLogger();
 
     private SSLContext CTX;
@@ -249,16 +242,24 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
             }
 
             // Check for errors
-            String responseMessage = con.getResponseMessage();
             int responseCode = con.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK) {
                 inputStream = con.getInputStream();
             } else {
                 inputStream = con.getErrorStream();
             }
+            String responseMessage = null;
+            try {
+               responseMessage = con.getResponseMessage();
+            } catch(Exception exc) {
+               responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
+            } finally {
+               if(responseMessage == null)
+                       responseMessage = NOT_PROVIDED;
+            }
 
             // Process the response
-            LOG.debug("HttpURLConnection result:" + responseCode + " : " + responseMessage);
+            LOG.info(HTTP_URL_CONNECTION_RESULT, responseCode, responseMessage);
             logMetricResponse(responseCode, responseMessage);
 
             if(inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
@@ -383,15 +384,23 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
             osw.flush();
 
             // Check for errors
-            String responseMessage = con.getResponseMessage();
             int responseCode = con.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
                 inputStream = con.getInputStream();
             } else {
                 inputStream = con.getErrorStream();
             }
+            String responseMessage = null;
+            try {
+               responseMessage = con.getResponseMessage();
+            } catch(Exception exc) {
+               responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
+            } finally {
+               if(responseMessage == null)
+                       responseMessage = NOT_PROVIDED;
+            }
 
-            LOG.debug("HttpURLConnection result:" + responseCode + " : " + responseMessage);
+            LOG.info(HTTP_URL_CONNECTION_RESULT, responseCode, responseMessage);
             logMetricResponse(responseCode, responseMessage);
 
             // Process the response
@@ -457,16 +466,24 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
             conn.setDoOutput(true);
 
             // Check for errors
-            String responseMessage = conn.getResponseMessage();
             int responseCode = conn.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
                 inputStream = conn.getInputStream();
             } else {
                 inputStream = conn.getErrorStream();
             }
+            String responseMessage = null;
+            try {
+               responseMessage = conn.getResponseMessage();
+            } catch(Exception exc) {
+               responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
+            } finally {
+               if(responseMessage == null)
+                       responseMessage = NOT_PROVIDED;
+            }
 
             // Process the response
-            LOG.debug("HttpURLConnection result:" + responseCode + " : " + responseMessage);
+            LOG.info(HTTP_URL_CONNECTION_RESULT, responseCode, responseMessage);
             logMetricResponse(responseCode, responseMessage);
 
             if(inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
@@ -531,14 +548,23 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
             logMetricRequest("GET "+requestUrl.getPath(), "", requestUrl.getPath());
 
             // Check for errors
-            String responseMessage = con.getResponseMessage();
             int responseCode = con.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK) {
                 inputStream = con.getInputStream();
             } else {
                 inputStream = con.getErrorStream();
             }
+            String responseMessage = null;
+            try {
+               responseMessage = con.getResponseMessage();
+            } catch(Exception exc) {
+               responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
+            } finally {
+               if(responseMessage == null)
+                       responseMessage = NOT_PROVIDED;
+            }
 
+            LOG.info(HTTP_URL_CONNECTION_RESULT, responseCode, responseMessage);
             logMetricResponse(responseCode, responseMessage);
             ObjectMapper mapper = AAIService.getObjectMapper();
 
@@ -597,15 +623,23 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
             osw.flush();
 
             // Check for errors
-            String responseMessage = con.getResponseMessage();
             int responseCode = con.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
                 inputStream = con.getInputStream();
             } else {
                 inputStream = con.getErrorStream();
             }
+            String responseMessage = null;
+            try {
+               responseMessage = con.getResponseMessage();
+            } catch(Exception exc) {
+               responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
+            } finally {
+               if(responseMessage == null)
+                       responseMessage = NOT_PROVIDED;
+            }
 
-            LOG.info("HttpURLConnection result: " + responseCode + " : " + responseMessage);
+            LOG.info(HTTP_URL_CONNECTION_RESULT, responseCode, responseMessage);
             logMetricResponse(responseCode, responseMessage);
 
             // Process the response
@@ -722,7 +756,7 @@ public     class AAIClientRESTExecutor implements AAIExecutorInterface {
     }
 
     public void logMetricResponse(int responseCode, String responseDescription){
-        ml.logResponse(responseCode < 400 ? "SUCCESS" : "FAILURE", Integer.toString(responseCode), responseDescription);
+        ml.logResponse(responseCode < 400 ? "COMPLETE" : "ERROR", Integer.toString(responseCode), responseDescription);
     }
 
     protected void LOGwriteFirstTrace(String method, String url) {