1 package org.onap.aai.interceptors.post;
3 import java.io.IOException;
4 import java.util.Objects;
5 import java.util.Optional;
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;
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;
20 import com.att.eelf.configuration.EELFLogger;
21 import com.att.eelf.configuration.EELFManager;
22 import com.google.gson.JsonObject;
24 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
25 public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
27 private static final EELFLogger TRANSACTION_LOGGER = EELFManager.getInstance().getLogger(ResponseTransactionLogging.class);
30 private HttpServletResponse httpServletResponse;
33 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
36 this.transLogging(requestContext, responseContext);
40 private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
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) {
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);
59 String httpMethod = requestContext.getMethod();
61 String status = Integer.toString(responseContext.getStatus());
63 String request = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
64 String response = this.getResponseString(responseContext);
66 if (!Boolean.parseBoolean(logValue)) {
67 } else if (!Boolean.parseBoolean(getValue) && "GET".equals(httpMethod)) {
68 } else if (!Boolean.parseBoolean(postValue) && "POST".equals(httpMethod)) {
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, ""));
83 TRANSACTION_LOGGER.debug(logEntry.toString());
84 } catch (Exception e) {
85 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
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());
101 response.addProperty("Entity", "");
103 return response.toString();