Fixed null pointer Blocker/Critical sonar issues
[aai/rest-client.git] / src / main / java / org / onap / aai / restclient / client / RestClient.java
index 02f12b1..9e74c95 100644 (file)
@@ -39,12 +39,12 @@ import javax.ws.rs.core.Response;
 import org.onap.aai.restclient.enums.RestAuthenticationMode;
 import org.onap.aai.restclient.logging.RestClientMsgs;
 import org.onap.aai.restclient.rest.RestClientBuilder;
-import org.openecomp.cl.api.LogFields;
-import org.openecomp.cl.api.LogLine;
-import org.openecomp.cl.api.Logger;
-import org.openecomp.cl.eelf.LoggerFactory;
-import org.openecomp.cl.mdc.MdcContext;
-import org.openecomp.cl.mdc.MdcOverride;
+import org.onap.aai.cl.api.LogFields;
+import org.onap.aai.cl.api.LogLine;
+import org.onap.aai.cl.api.Logger;
+import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aai.cl.mdc.MdcContext;
+import org.onap.aai.cl.mdc.MdcOverride;
 
 import com.sun.jersey.api.client.Client;
 import com.sun.jersey.api.client.ClientResponse;
@@ -67,7 +67,7 @@ public class RestClient {
    */
   private RestClientBuilder clientBuilder;
   
-  private final ConcurrentMap<String,InitializedClient> CLIENT_CACHE = new ConcurrentHashMap<String,InitializedClient>();
+  private final ConcurrentMap<String,InitializedClient> CLIENT_CACHE = new ConcurrentHashMap<>();
   private static final String REST_CLIENT_INSTANCE = "REST_CLIENT_INSTANCE";
 
   /** Standard logger for producing log statements. */
@@ -236,6 +236,19 @@ public class RestClient {
     clientBuilder.setReadTimeoutInMs(timeout);
     return this;
   }
+  
+  /**
+   * Configures the client for a specific SSL protocol
+   *
+   * @param sslProtocol - protocol string constant such as TLS, TLSv1, TLSv1.1, TLSv1.2
+   *
+   * @return The AAIRESTClient instance. 
+   */
+  public RestClient sslProtocol(String sslProtocol) {
+    logger.debug("Set sslProtocol = " + sslProtocol);
+    clientBuilder.setSslProtocol(sslProtocol);
+    return this;
+  }
 
   private boolean shouldRetry(OperationResult operationResult) {
 
@@ -313,10 +326,14 @@ public class RestClient {
     }
 
     // If we've gotten this far, then we failed all of our retries.
+    if (result == null) {
+        result = new OperationResult();
+    }
+
     result.setNumRetries(numRetries);
     result.setResultCode(504);
-    result.setFailureCause(
-        "Failed to get a successful result after multiple retries to target server.");
+    result.setFailureCause("Failed to get a successful result after multiple retries to target server.");
+
 
     return result;
   }
@@ -555,8 +572,8 @@ public class RestClient {
         logger.info(RestClientMsgs.HEALTH_CHECK_SUCCESS, destAppName, url);
         return true;
       } else {
-        logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url,
-            result.getFailureCause());
+        logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url, result != null ? result.getFailureCause()
+                                                                                           : null);
         return false;
       }
     } catch (Exception e) {
@@ -581,9 +598,7 @@ public class RestClient {
       Map<String, List<String>> headers, MediaType contentType, MediaType responseType) {
 
     WebResource resource = client.resource(url);
-    Builder builder = null;
-
-    builder = resource.accept(responseType);
+    Builder builder = resource.accept(responseType);
 
     if (contentType != null) {
       builder.type(contentType);
@@ -595,7 +610,7 @@ public class RestClient {
 
     if (headers != null) {
       for (Entry<String, List<String>> header : headers.entrySet()) {
-        builder.header(header.getKey(), header.getValue());
+        builder.header(header.getKey(), String.join(";",header.getValue()));
       }
       
       if (clientBuilder.getAuthenticationMode() == RestAuthenticationMode.SSL_BASIC) {