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=========================================================
21 package org.onap.aai.schemaservice.interceptors.post;
23 import com.google.gson.JsonObject;
25 import java.io.IOException;
26 import java.util.Objects;
27 import java.util.Optional;
29 import javax.annotation.Priority;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.ws.rs.HttpMethod;
32 import javax.ws.rs.container.ContainerRequestContext;
33 import javax.ws.rs.container.ContainerResponseContext;
34 import javax.ws.rs.container.ContainerResponseFilter;
36 import org.onap.aai.logging.ErrorLogHelper;
37 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
38 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
39 import org.onap.aai.util.AAIConfig;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.web.context.request.RequestAttributes;
43 import org.springframework.web.context.request.RequestContextHolder;
44 import org.springframework.web.context.request.ServletRequestAttributes;
46 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
47 public class ResponseTransactionLogging extends AAIContainerFilter
48 implements ContainerResponseFilter {
50 private static final Logger TRANSACTION_LOGGER =
51 LoggerFactory.getLogger(ResponseTransactionLogging.class);
54 public void filter(ContainerRequestContext requestContext,
55 ContainerResponseContext responseContext) throws IOException {
57 this.transLogging(requestContext, responseContext);
61 private void transLogging(ContainerRequestContext requestContext,
62 ContainerResponseContext responseContext) {
64 String logValue = AAIConfig.get("aai.transaction.logging", "true");
65 String isGetTransactionResponseLoggingEnabled =
66 AAIConfig.get("aai.transaction.logging.get", "false");
68 String httpMethod = requestContext.getMethod();
70 if (Boolean.parseBoolean(logValue)) {
72 String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
73 String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
74 String fullUri = requestContext.getUriInfo().getRequestUri().toString();
76 (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
78 String status = Integer.toString(responseContext.getStatus());
80 String request = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
81 String response = this.getResponseString(responseContext);
83 JsonObject logEntry = new JsonObject();
84 logEntry.addProperty("transactionId", transId);
85 logEntry.addProperty("status", status);
86 logEntry.addProperty("rqstDate", requestTs);
87 logEntry.addProperty("respDate", this.genDate());
88 logEntry.addProperty("sourceId", fromAppId + ":" + transId);
89 logEntry.addProperty("resourceId", fullUri);
90 logEntry.addProperty("resourceType", httpMethod);
91 logEntry.addProperty("rqstBuf", Objects.toString(request, ""));
92 if (Boolean.parseBoolean(isGetTransactionResponseLoggingEnabled)
93 || (!HttpMethod.GET.equals(httpMethod))) {
94 logEntry.addProperty("respBuf", Objects.toString(response, ""));
98 TRANSACTION_LOGGER.debug(logEntry.toString());
99 } catch (Exception e) {
100 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
105 private String getHttpServletResponseContentType() {
106 final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
107 if (requestAttributes != null) {
108 HttpServletResponse response =
109 ((ServletRequestAttributes) requestAttributes).getResponse();
110 return response == null ? null : response.getContentType();
115 private String getResponseString(ContainerResponseContext responseContext) {
116 JsonObject response = new JsonObject();
117 response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
118 response.addProperty("Content-Type", getHttpServletResponseContentType());
119 response.addProperty("Response-Code", responseContext.getStatus());
120 response.addProperty("Headers", responseContext.getHeaders().toString());
121 Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());
122 if (entityOptional.isPresent()) {
123 response.addProperty("Entity", entityOptional.get().toString());
125 response.addProperty("Entity", "");
127 return response.toString();