2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.interceptors.post;
22 import java.io.IOException;
23 import java.util.Objects;
24 import java.util.Optional;
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;
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;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.google.gson.JsonObject;
43 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
44 public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
46 private static final EELFLogger TRANSACTION_LOGGER = EELFManager.getInstance().getLogger(ResponseTransactionLogging.class);
49 private HttpServletResponse httpServletResponse;
52 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
55 this.transLogging(requestContext, responseContext);
59 private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
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) {
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);
78 String httpMethod = requestContext.getMethod();
80 String status = Integer.toString(responseContext.getStatus());
82 String request = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
83 String response = this.getResponseString(responseContext);
85 if (!Boolean.parseBoolean(logValue)) {
86 } else if (!Boolean.parseBoolean(getValue) && "GET".equals(httpMethod)) {
87 } else if (!Boolean.parseBoolean(postValue) && "POST".equals(httpMethod)) {
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, ""));
102 TRANSACTION_LOGGER.debug(logEntry.toString());
103 } catch (Exception e) {
104 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
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());
120 response.addProperty("Entity", "");
122 return response.toString();