Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / 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.interceptors.post;
21
22 import java.io.IOException;
23 import java.util.Objects;
24 import java.util.Optional;
25
26 import javax.annotation.Priority;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.container.ContainerRequestContext;
29 import javax.ws.rs.container.ContainerResponseContext;
30 import javax.ws.rs.container.ContainerResponseFilter;
31
32 import org.onap.aai.exceptions.AAIException;
33 import org.onap.aai.interceptors.AAIContainerFilter;
34 import org.onap.aai.interceptors.AAIHeaderProperties;
35 import org.onap.aai.logging.ErrorLogHelper;
36 import org.onap.aai.util.AAIConfig;
37 import org.springframework.beans.factory.annotation.Autowired;
38
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.google.gson.JsonObject;
42
43 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
44 public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
45
46         private static final EELFLogger TRANSACTION_LOGGER = EELFManager.getInstance().getLogger(ResponseTransactionLogging.class);
47
48         @Autowired
49         private HttpServletResponse httpServletResponse;
50
51         @Override
52         public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
53                         throws IOException {
54
55                 this.transLogging(requestContext, responseContext);
56
57         }
58
59         private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
60
61                 String logValue;
62                 String getValue;
63                 String postValue;
64                 
65                 try {
66                         logValue = AAIConfig.get("aai.transaction.logging");
67                         getValue = AAIConfig.get("aai.transaction.logging.get");
68                         postValue = AAIConfig.get("aai.transaction.logging.post");
69                 } catch (AAIException e) {
70                         return;
71                 }
72
73                 String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
74                 String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
75                 String fullUri = requestContext.getUriInfo().getRequestUri().toString();
76                 String requestTs = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
77
78                 String httpMethod = requestContext.getMethod();
79
80                 String status = Integer.toString(responseContext.getStatus());
81                 
82                 String request = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
83                 String response = this.getResponseString(responseContext);
84
85                 if (!Boolean.parseBoolean(logValue)) {
86                 } else if (!Boolean.parseBoolean(getValue) && "GET".equals(httpMethod)) {
87                 } else if (!Boolean.parseBoolean(postValue) && "POST".equals(httpMethod)) {
88                 } else {
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
110         private String getResponseString(ContainerResponseContext responseContext) {
111                 JsonObject response = new JsonObject();
112                 response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
113                 response.addProperty("Content-Type", this.httpServletResponse.getContentType());
114                 response.addProperty("Response-Code", responseContext.getStatus());
115                 response.addProperty("Headers", responseContext.getHeaders().toString());
116                 Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());
117                 if(entityOptional.isPresent()){
118                         response.addProperty("Entity", entityOptional.get().toString());
119                 } else {
120                         response.addProperty("Entity", "");
121                 }
122                 return response.toString();
123         }
124
125 }