Update traversal from AJSC 2 to Spring Boot
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / interceptors / post / ResponseTransactionLogging.java
1 package org.onap.aai.interceptors.post;
2
3 import java.io.IOException;
4 import java.util.Objects;
5 import java.util.Optional;
6
7 import javax.annotation.Priority;
8 import javax.servlet.http.HttpServletResponse;
9 import javax.ws.rs.container.ContainerRequestContext;
10 import javax.ws.rs.container.ContainerResponseContext;
11 import javax.ws.rs.container.ContainerResponseFilter;
12
13 import org.onap.aai.exceptions.AAIException;
14 import org.onap.aai.interceptors.AAIContainerFilter;
15 import org.onap.aai.interceptors.AAIHeaderProperties;
16 import org.onap.aai.logging.ErrorLogHelper;
17 import org.onap.aai.util.AAIConfig;
18 import org.springframework.beans.factory.annotation.Autowired;
19
20 import com.att.eelf.configuration.EELFLogger;
21 import com.att.eelf.configuration.EELFManager;
22 import com.google.gson.JsonObject;
23
24 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
25 public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
26
27         private static final EELFLogger TRANSACTION_LOGGER = EELFManager.getInstance().getLogger(ResponseTransactionLogging.class);
28
29         @Autowired
30         private HttpServletResponse httpServletResponse;
31
32         @Override
33         public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
34                         throws IOException {
35
36                 this.transLogging(requestContext, responseContext);
37
38         }
39
40         private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
41
42                 String logValue;
43                 String getValue;
44                 String postValue;
45                 
46                 try {
47                         logValue = AAIConfig.get("aai.transaction.logging");
48                         getValue = AAIConfig.get("aai.transaction.logging.get");
49                         postValue = AAIConfig.get("aai.transaction.logging.post");
50                 } catch (AAIException e) {
51                         return;
52                 }
53
54                 String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
55                 String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
56                 String fullUri = requestContext.getUriInfo().getRequestUri().toString();
57                 String requestTs = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
58
59                 String httpMethod = requestContext.getMethod();
60
61                 String status = Integer.toString(responseContext.getStatus());
62                 
63                 String request = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
64                 String response = this.getResponseString(responseContext);
65
66                 if (!Boolean.parseBoolean(logValue)) {
67                 } else if (!Boolean.parseBoolean(getValue) && "GET".equals(httpMethod)) {
68                 } else if (!Boolean.parseBoolean(postValue) && "POST".equals(httpMethod)) {
69                 } else {
70                         
71                         JsonObject logEntry = new JsonObject();
72                         logEntry.addProperty("transactionId", transId);
73                         logEntry.addProperty("status", status);
74                         logEntry.addProperty("rqstDate", requestTs);
75                         logEntry.addProperty("respDate", this.genDate());
76                         logEntry.addProperty("sourceId", fromAppId + ":" + transId);
77                         logEntry.addProperty("resourceId", fullUri);
78                         logEntry.addProperty("resourceType", httpMethod);
79                         logEntry.addProperty("rqstBuf", Objects.toString(request, ""));
80                         logEntry.addProperty("respBuf", Objects.toString(response, ""));
81                         
82                         try {
83                                 TRANSACTION_LOGGER.debug(logEntry.toString());
84                         } catch (Exception e) {
85                                 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
86                         }
87                 }
88
89         }
90
91         private String getResponseString(ContainerResponseContext responseContext) {
92                 JsonObject response = new JsonObject();
93                 response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
94                 response.addProperty("Content-Type", this.httpServletResponse.getContentType());
95                 response.addProperty("Response-Code", responseContext.getStatus());
96                 response.addProperty("Headers", responseContext.getHeaders().toString());
97                 Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());
98                 if(entityOptional.isPresent()){
99                         response.addProperty("Entity", entityOptional.get().toString());
100                 } else {
101                         response.addProperty("Entity", "");
102                 }
103                 return response.toString();
104         }
105
106 }