Merge "SSL_BASIC getting 403 error"
[aai/rest-client.git] / src / main / java / org / onap / aai / restclient / client / RestClient.java
index a98f710..a00c0ef 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017 Amdocs
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Amdocs
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,8 +17,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.aai.restclient.client;
 
@@ -39,12 +37,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 +65,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. */
@@ -326,10 +324,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;
   }
@@ -568,8 +570,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) {
@@ -594,9 +596,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);
@@ -611,7 +611,9 @@ public class RestClient {
         builder.header(header.getKey(), String.join(";",header.getValue()));
       }
       
-      if (clientBuilder.getAuthenticationMode() == RestAuthenticationMode.SSL_BASIC) {
+      //Added additional check to prevent adding duplicate authorization header if client is already sending the authorization header 
+      // AAI-1097 - For AAI calls when Rest authentication mode is selected as SSL_BASIC getting 403 error
+      if (clientBuilder.getAuthenticationMode() == RestAuthenticationMode.SSL_BASIC && headers.get(Headers.AUTHORIZATION) == null) {
         builder = builder.header(Headers.AUTHORIZATION,
             clientBuilder.getBasicAuthenticationCredentials());
       }
@@ -623,43 +625,58 @@ public class RestClient {
 
   private void debugRequest(String url, String payload, Map<String, List<String>> headers,
       MediaType responseType) {
-    if (logger.isDebugEnabled()) {
-      StringBuilder debugRequest = new StringBuilder("REQUEST:\n");
-      debugRequest.append("URL: ").append(url).append("\n");
-      debugRequest.append("Payload: ").append(payload).append("\n");
-      debugRequest.append("Response Type: ").append(responseType).append("\n");
-      if (headers != null) {
-        debugRequest.append("Headers: ");
-        for (Entry<String, List<String>> header : headers.entrySet()) {
-          debugRequest.append("\n\t").append(header.getKey()).append(":");
-          for (String headerEntry : header.getValue()) {
-            debugRequest.append("\"").append(headerEntry).append("\" ");
-          }
-        }
-      }
+    if (!logger.isDebugEnabled()) {
+      return;
+    }
+
+    StringBuilder debugRequest = new StringBuilder("REQUEST:\n");
+    debugRequest.append("URL: ").append(url).append("\n");
+    debugRequest.append("Payload: ").append(payload).append("\n");
+    debugRequest.append("Response Type: ").append(responseType).append("\n");
+
+    if (headers == null) {
       logger.debug(debugRequest.toString());
+      return;
+    }
+
+    debugRequest.append("Headers: ");
+    for (Entry<String, List<String>> header : headers.entrySet()) {
+      debugRequest.append("\n\t").append(header.getKey()).append(":");
+      for (String headerEntry : header.getValue()) {
+        debugRequest.append("\"").append(headerEntry).append("\" ");
+      }
     }
+
+    logger.debug(debugRequest.toString());
+
   }
 
   private void debugResponse(OperationResult operationResult,
       MultivaluedMap<String, String> headers) {
-    if (logger.isDebugEnabled()) {
-      StringBuilder debugResponse = new StringBuilder("RESPONSE:\n");
-      debugResponse.append("Result: ").append(operationResult.getResultCode()).append("\n");
-      debugResponse.append("Failure Cause: ").append(operationResult.getFailureCause())
-          .append("\n");
-      debugResponse.append("Payload: ").append(operationResult.getResult()).append("\n");
-      if (headers != null) {
-        debugResponse.append("Headers: ");
-        for (Entry<String, List<String>> header : headers.entrySet()) {
-          debugResponse.append("\n\t").append(header.getKey()).append(":");
-          for (String headerEntry : header.getValue()) {
-            debugResponse.append("\"").append(headerEntry).append("\" ");
-          }
-        }
-      }
+
+    if (!logger.isDebugEnabled()) {
+      return;
+    }
+
+    StringBuilder debugResponse = new StringBuilder("RESPONSE:\n");
+    debugResponse.append("Result: ").append(operationResult.getResultCode()).append("\n");
+    debugResponse.append("Failure Cause: ").append(operationResult.getFailureCause()).append("\n");
+    debugResponse.append("Payload: ").append(operationResult.getResult()).append("\n");
+
+    if (headers == null) {
       logger.debug(debugResponse.toString());
+      return;
     }
+
+    debugResponse.append("Headers: ");
+    for (Entry<String, List<String>> header : headers.entrySet()) {
+      debugResponse.append("\n\t").append(header.getKey()).append(":");
+      for (String headerEntry : header.getValue()) {
+        debugResponse.append("\"").append(headerEntry).append("\" ");
+      }
+    }
+
+    logger.debug(debugResponse.toString());
   }
 
   /**
@@ -708,7 +725,7 @@ public class RestClient {
     
     try {
       initClient.setClient(clientBuilder.getClient());
-    } catch ( Throwable error ) {
+    } catch ( Exception error) {
       initClient.setCaughtException(error);
     }