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 org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import com.google.gson.JsonObject;
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.logging.ErrorLogHelper;
27 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
28 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
29 import org.onap.aai.util.AAIConfig;
30 import org.springframework.beans.factory.annotation.Autowired;
32 import javax.annotation.Priority;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.ws.rs.HttpMethod;
35 import javax.ws.rs.container.ContainerRequestContext;
36 import javax.ws.rs.container.ContainerResponseContext;
37 import javax.ws.rs.container.ContainerResponseFilter;
38 import java.io.IOException;
39 import java.util.Objects;
40 import java.util.Optional;
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 private HttpServletResponse httpServletResponse;
51 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
54 this.transLogging(requestContext, responseContext);
58 private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
60 String logValue = AAIConfig.get("aai.transaction.logging", "true");
61 String isGetTransactionResponseLoggingEnabled = AAIConfig.get("aai.transaction.logging.get", "false");
63 String httpMethod = requestContext.getMethod();
65 if(Boolean.parseBoolean(logValue)){
67 String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
68 String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
69 String fullUri = requestContext.getUriInfo().getRequestUri().toString();
70 String requestTs = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
73 String status = Integer.toString(responseContext.getStatus());
75 String request = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
76 String response = this.getResponseString(responseContext);
79 JsonObject logEntry = new JsonObject();
80 logEntry.addProperty("transactionId", transId);
81 logEntry.addProperty("status", status);
82 logEntry.addProperty("rqstDate", requestTs);
83 logEntry.addProperty("respDate", this.genDate());
84 logEntry.addProperty("sourceId", fromAppId + ":" + transId);
85 logEntry.addProperty("resourceId", fullUri);
86 logEntry.addProperty("resourceType", httpMethod);
87 logEntry.addProperty("rqstBuf", Objects.toString(request, ""));
88 if(Boolean.parseBoolean(isGetTransactionResponseLoggingEnabled) || (!HttpMethod.GET.equals(httpMethod))) {
89 logEntry.addProperty("respBuf", Objects.toString(response, ""));
93 TRANSACTION_LOGGER.debug(logEntry.toString());
94 } catch (Exception e) {
95 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
100 private String getResponseString(ContainerResponseContext responseContext) {
101 JsonObject response = new JsonObject();
102 response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
103 response.addProperty("Content-Type", this.httpServletResponse.getContentType());
104 response.addProperty("Response-Code", responseContext.getStatus());
105 response.addProperty("Headers", responseContext.getHeaders().toString());
106 Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());
107 if (entityOptional.isPresent()) {
108 response.addProperty("Entity", entityOptional.get().toString());
110 response.addProperty("Entity", "");
112 return response.toString();