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
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.schemaservice.interceptors.post;
22
23 import com.google.gson.JsonObject;
24
25 import java.io.IOException;
26 import java.util.Objects;
27 import java.util.Optional;
28
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;
35
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;
45
46 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
47 public class ResponseTransactionLogging extends AAIContainerFilter
48     implements ContainerResponseFilter {
49
50     private static final Logger TRANSACTION_LOGGER =
51         LoggerFactory.getLogger(ResponseTransactionLogging.class);
52
53     @Override
54     public void filter(ContainerRequestContext requestContext,
55         ContainerResponseContext responseContext) throws IOException {
56
57         this.transLogging(requestContext, responseContext);
58
59     }
60
61     private void transLogging(ContainerRequestContext requestContext,
62         ContainerResponseContext responseContext) {
63
64         String logValue = AAIConfig.get("aai.transaction.logging", "true");
65         String isGetTransactionResponseLoggingEnabled =
66             AAIConfig.get("aai.transaction.logging.get", "false");
67
68         String httpMethod = requestContext.getMethod();
69
70         if (Boolean.parseBoolean(logValue)) {
71
72             String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
73             String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
74             String fullUri = requestContext.getUriInfo().getRequestUri().toString();
75             String requestTs =
76                 (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
77
78             String status = Integer.toString(responseContext.getStatus());
79
80             String request = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
81             String response = this.getResponseString(responseContext);
82
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, ""));
95             }
96
97             try {
98                 TRANSACTION_LOGGER.debug(logEntry.toString());
99             } catch (Exception e) {
100                 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
101             }
102         }
103     }
104
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();
111         }
112         return null;
113     }
114
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());
124         } else {
125             response.addProperty("Entity", "");
126         }
127         return response.toString();
128     }
129
130 }