Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / interceptors / post / ResponseTransactionLogging.java
index cd6706d..f89440a 100644 (file)
@@ -8,7 +8,7 @@
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *    http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.aai.schemaservice.interceptors.post;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
 import com.google.gson.JsonObject;
-import org.onap.aai.exceptions.AAIException;
-import org.onap.aai.logging.ErrorLogHelper;
-import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
-import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
-import org.onap.aai.util.AAIConfig;
-import org.springframework.beans.factory.annotation.Autowired;
+
+import java.io.IOException;
+import java.util.Objects;
+import java.util.Optional;
 
 import javax.annotation.Priority;
 import javax.servlet.http.HttpServletResponse;
@@ -35,58 +32,54 @@ import javax.ws.rs.HttpMethod;
 import javax.ws.rs.container.ContainerRequestContext;
 import javax.ws.rs.container.ContainerResponseContext;
 import javax.ws.rs.container.ContainerResponseFilter;
-import java.io.IOException;
-import java.util.Objects;
-import java.util.Optional;
 
-@Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
-public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
+import org.onap.aai.logging.ErrorLogHelper;
+import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
+import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
+import org.onap.aai.util.AAIConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
-    private static final EELFLogger TRANSACTION_LOGGER = EELFManager.getInstance().getLogger(ResponseTransactionLogging.class);
+@Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
+public class ResponseTransactionLogging extends AAIContainerFilter
+    implements ContainerResponseFilter {
 
-    @Autowired
-    private HttpServletResponse httpServletResponse;
+    private static final Logger TRANSACTION_LOGGER =
+        LoggerFactory.getLogger(ResponseTransactionLogging.class);
 
     @Override
-    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
-        throws IOException {
+    public void filter(ContainerRequestContext requestContext,
+        ContainerResponseContext responseContext) throws IOException {
 
         this.transLogging(requestContext, responseContext);
 
     }
 
-    private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
+    private void transLogging(ContainerRequestContext requestContext,
+        ContainerResponseContext responseContext) {
 
-        String logValue;
-        String getValue;
-
-        try {
-            logValue = AAIConfig.get("aai.transaction.logging");
-            getValue = AAIConfig.get("aai.transaction.logging.get");
-        } catch (AAIException e) {
-            return;
-        }
+        String logValue = AAIConfig.get("aai.transaction.logging", "true");
+        String isGetTransactionResponseLoggingEnabled =
+            AAIConfig.get("aai.transaction.logging.get", "false");
 
         String httpMethod = requestContext.getMethod();
 
-        if(Boolean.parseBoolean(logValue)){
-
-            if(!Boolean.parseBoolean(getValue) && HttpMethod.GET.equals(httpMethod)){
-                return;
-            }
+        if (Boolean.parseBoolean(logValue)) {
 
             String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
             String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
             String fullUri = requestContext.getUriInfo().getRequestUri().toString();
-            String requestTs = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
-
+            String requestTs =
+                (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
 
             String status = Integer.toString(responseContext.getStatus());
 
             String request = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
             String response = this.getResponseString(responseContext);
 
-
             JsonObject logEntry = new JsonObject();
             logEntry.addProperty("transactionId", transId);
             logEntry.addProperty("status", status);
@@ -96,7 +89,10 @@ public class ResponseTransactionLogging extends AAIContainerFilter implements Co
             logEntry.addProperty("resourceId", fullUri);
             logEntry.addProperty("resourceType", httpMethod);
             logEntry.addProperty("rqstBuf", Objects.toString(request, ""));
-            logEntry.addProperty("respBuf", Objects.toString(response, ""));
+            if (Boolean.parseBoolean(isGetTransactionResponseLoggingEnabled)
+                || (!HttpMethod.GET.equals(httpMethod))) {
+                logEntry.addProperty("respBuf", Objects.toString(response, ""));
+            }
 
             try {
                 TRANSACTION_LOGGER.debug(logEntry.toString());
@@ -106,10 +102,20 @@ public class ResponseTransactionLogging extends AAIContainerFilter implements Co
         }
     }
 
+    private String getHttpServletResponseContentType() {
+        final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+        if (requestAttributes != null) {
+            HttpServletResponse response =
+                ((ServletRequestAttributes) requestAttributes).getResponse();
+            return response == null ? null : response.getContentType();
+        }
+        return null;
+    }
+
     private String getResponseString(ContainerResponseContext responseContext) {
         JsonObject response = new JsonObject();
         response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
-        response.addProperty("Content-Type", this.httpServletResponse.getContentType());
+        response.addProperty("Content-Type", getHttpServletResponseContentType());
         response.addProperty("Response-Code", responseContext.getStatus());
         response.addProperty("Headers", responseContext.getHeaders().toString());
         Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());