2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.schemaservice.interceptors.post;
22 import com.google.gson.JsonObject;
23 import java.io.IOException;
24 import java.util.Objects;
25 import java.util.Optional;
26 import javax.annotation.Priority;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.HttpMethod;
29 import javax.ws.rs.container.ContainerRequestContext;
30 import javax.ws.rs.container.ContainerResponseContext;
31 import javax.ws.rs.container.ContainerResponseFilter;
32 import org.onap.aai.logging.ErrorLogHelper;
33 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
34 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
35 import org.onap.aai.util.AAIConfig;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.web.context.request.RequestAttributes;
39 import org.springframework.web.context.request.RequestContextHolder;
40 import org.springframework.web.context.request.ServletRequestAttributes;
42 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
43 public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
45 private static final Logger TRANSACTION_LOGGER = LoggerFactory.getLogger(ResponseTransactionLogging.class);
48 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
51 this.transLogging(requestContext, responseContext);
55 private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
57 String logValue = AAIConfig.get("aai.transaction.logging", "true");
58 String isGetTransactionResponseLoggingEnabled = AAIConfig.get("aai.transaction.logging.get", "false");
60 String httpMethod = requestContext.getMethod();
62 if(Boolean.parseBoolean(logValue)){
64 String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
65 String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
66 String fullUri = requestContext.getUriInfo().getRequestUri().toString();
67 String requestTs = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
70 String status = Integer.toString(responseContext.getStatus());
72 String request = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
73 String response = this.getResponseString(responseContext);
76 JsonObject logEntry = new JsonObject();
77 logEntry.addProperty("transactionId", transId);
78 logEntry.addProperty("status", status);
79 logEntry.addProperty("rqstDate", requestTs);
80 logEntry.addProperty("respDate", this.genDate());
81 logEntry.addProperty("sourceId", fromAppId + ":" + transId);
82 logEntry.addProperty("resourceId", fullUri);
83 logEntry.addProperty("resourceType", httpMethod);
84 logEntry.addProperty("rqstBuf", Objects.toString(request, ""));
85 if(Boolean.parseBoolean(isGetTransactionResponseLoggingEnabled) || (!HttpMethod.GET.equals(httpMethod))) {
86 logEntry.addProperty("respBuf", Objects.toString(response, ""));
90 TRANSACTION_LOGGER.debug(logEntry.toString());
91 } catch (Exception e) {
92 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
97 private String getHttpServletResponseContentType() {
98 final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
99 if (requestAttributes != null) {
100 HttpServletResponse response = ((ServletRequestAttributes) requestAttributes).getResponse();
101 return response == null ? null : response.getContentType();
106 private String getResponseString(ContainerResponseContext responseContext) {
107 JsonObject response = new JsonObject();
108 response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
109 response.addProperty("Content-Type", getHttpServletResponseContentType());
110 response.addProperty("Response-Code", responseContext.getStatus());
111 response.addProperty("Headers", responseContext.getHeaders().toString());
112 Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());
113 if (entityOptional.isPresent()) {
114 response.addProperty("Entity", entityOptional.get().toString());
116 response.addProperty("Entity", "");
118 return response.toString();