2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.interceptors.post;
24 import java.io.IOException;
25 import java.util.Objects;
26 import java.util.Optional;
28 import javax.annotation.Priority;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.ws.rs.container.ContainerRequestContext;
31 import javax.ws.rs.container.ContainerResponseContext;
32 import javax.ws.rs.container.ContainerResponseFilter;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.interceptors.AAIContainerFilter;
36 import org.onap.aai.interceptors.AAIHeaderProperties;
37 import org.onap.aai.logging.ErrorLogHelper;
38 import org.onap.aai.util.AAIConfig;
39 import org.springframework.beans.factory.annotation.Autowired;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.google.gson.JsonObject;
45 @Priority(AAIResponseFilterPriority.RESPONSE_TRANS_LOGGING)
46 public class ResponseTransactionLogging extends AAIContainerFilter implements ContainerResponseFilter {
48 private static final EELFLogger TRANSACTION_LOGGER = EELFManager.getInstance().getLogger(ResponseTransactionLogging.class);
51 private HttpServletResponse httpServletResponse;
54 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
57 this.transLogging(requestContext, responseContext);
61 private void transLogging(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
68 logValue = AAIConfig.get("aai.transaction.logging");
69 getValue = AAIConfig.get("aai.transaction.logging.get");
70 postValue = AAIConfig.get("aai.transaction.logging.post");
71 } catch (AAIException e) {
75 String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
76 String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
77 String fullUri = requestContext.getUriInfo().getRequestUri().toString();
78 String requestTs = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS);
80 String httpMethod = requestContext.getMethod();
82 String status = Integer.toString(responseContext.getStatus());
84 String request = (String)requestContext.getProperty(AAIHeaderProperties.AAI_REQUEST);
85 String response = this.getResponseString(responseContext);
87 if (!Boolean.parseBoolean(logValue)) {
88 } else if (!Boolean.parseBoolean(getValue) && "GET".equals(httpMethod)) {
89 } else if (!Boolean.parseBoolean(postValue) && "POST".equals(httpMethod)) {
92 JsonObject logEntry = new JsonObject();
93 logEntry.addProperty("transactionId", transId);
94 logEntry.addProperty("status", status);
95 logEntry.addProperty("rqstDate", requestTs);
96 logEntry.addProperty("respDate", this.genDate());
97 logEntry.addProperty("sourceId", fromAppId + ":" + transId);
98 logEntry.addProperty("resourceId", fullUri);
99 logEntry.addProperty("resourceType", httpMethod);
100 logEntry.addProperty("rqstBuf", Objects.toString(request, ""));
101 logEntry.addProperty("respBuf", Objects.toString(response, ""));
104 TRANSACTION_LOGGER.debug(logEntry.toString());
105 } catch (Exception e) {
106 ErrorLogHelper.logError("AAI_4000", "Exception writing transaction log.");
112 private String getResponseString(ContainerResponseContext responseContext) {
113 JsonObject response = new JsonObject();
114 response.addProperty("ID", responseContext.getHeaderString(AAIHeaderProperties.AAI_TX_ID));
115 response.addProperty("Content-Type", this.httpServletResponse.getContentType());
116 response.addProperty("Response-Code", responseContext.getStatus());
117 response.addProperty("Headers", responseContext.getHeaders().toString());
118 Optional<Object> entityOptional = Optional.ofNullable(responseContext.getEntity());
119 if(entityOptional.isPresent()){
120 response.addProperty("Entity", entityOptional.get().toString());
122 response.addProperty("Entity", "");
124 return response.toString();