Initial commit of the schema service
[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 package org.onap.aai.schemaservice.interceptors.post;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
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;
31
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;
41
42 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
43 public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
44
45     private static final EELFLogger TRANSACTION_LOGGER = EELFManager.getInstance().getLogger(ResponseTransactionLogging.class);
46
47     @Autowired
48     private HttpServletResponse httpServletResponse;
49
50     @Override
51     public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
52         throws IOException {
53
54         this.transLogging(requestContext, responseContext);
55
56     }
57
58     private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
59
60         String logValue;
61         String getValue;
62
63         try {
64             logValue = AAIConfig.get("aai.transaction.logging");
65             getValue = AAIConfig.get("aai.transaction.logging.get");
66         } catch (AAIException e) {
67             return;
68         }
69
70         String httpMethod = requestContext.getMethod();
71
72         if(Boolean.parseBoolean(logValue)){
73
74             if(!Boolean.parseBoolean(getValue) && HttpMethod.GET.equals(httpMethod)){
75                 return;
76             }
77
78             String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
79             String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
80             String fullUri = requestContext.getUriInfo().getRequestUri().toString();
81             String requestTs = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
82
83
84             String status = Integer.toString(responseContext.getStatus());
85
86             String request = (String) requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
87             String response = this.getResponseString(responseContext);
88
89
90             JsonObject logEntry = new JsonObject();
91             logEntry.addProperty("transactionId", transId);
92             logEntry.addProperty("status", status);
93             logEntry.addProperty("rqstDate", requestTs);
94             logEntry.addProperty("respDate", this.genDate());
95             logEntry.addProperty("sourceId", fromAppId + ":" + transId);
96             logEntry.addProperty("resourceId", fullUri);
97             logEntry.addProperty("resourceType", httpMethod);
98             logEntry.addProperty("rqstBuf", Objects.toString(request, ""));
99             logEntry.addProperty("respBuf", Objects.toString(response, ""));
100
101             try {
102                 TRANSACTION_LOGGER.debug(logEntry.toString());
103             } catch (Exception e) {
104                 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
105             }
106         }
107     }
108
109     private String getResponseString(ContainerResponseContext responseContext) {
110         JsonObject response = new JsonObject();
111         response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
112         response.addProperty("Content-Type", this.httpServletResponse.getContentType());
113         response.addProperty("Response-Code", responseContext.getStatus());
114         response.addProperty("Headers", responseContext.getHeaders().toString());
115         Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());
116         if (entityOptional.isPresent()) {
117             response.addProperty("Entity", entityOptional.get().toString());
118         } else {
119             response.addProperty("Entity", "");
120         }
121         return response.toString();
122     }
123
124 }