2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.onap.so.logging.jaxrs.filter;
23 import org.onap.logging.ref.slf4j.ONAPLogConstants;
24 import org.onap.so.logger.LogConstants;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.HttpRequest;
30 import org.springframework.http.client.ClientHttpRequestExecution;
31 import org.springframework.http.client.ClientHttpRequestInterceptor;
32 import org.springframework.http.client.ClientHttpResponse;
33 import org.springframework.util.StreamUtils;
35 import java.io.IOException;
36 import java.nio.charset.Charset;
37 import java.time.ZoneOffset;
38 import java.time.ZonedDateTime;
39 import java.time.format.DateTimeFormatter;
40 import java.util.List;
41 import java.util.UUID;
42 import javax.ws.rs.core.Response;
44 public class SpringClientFilter implements ClientHttpRequestInterceptor {
46 private final Logger log = LoggerFactory.getLogger(this.getClass());
48 private static final String TRACE = "trace-#";
49 private static final String SO = "SO";
52 public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
53 processRequest(request, body);
54 ClientHttpResponse response = execution.execute(request, body);
55 processResponse(response);
59 private void processRequest(HttpRequest request, byte[] body) throws IOException {
60 setupHeaders(request);
62 if (log.isDebugEnabled()) {
63 log.debug("===========================request begin================================================");
64 log.debug("URI : {}", request.getURI());
65 log.debug("Method : {}", request.getMethod());
66 log.debug("Headers : {}", request.getHeaders());
67 log.debug("Request body: {}", new String(body, "UTF-8"));
68 log.debug("==========================request end================================================");
72 private void setupHeaders(HttpRequest clientRequest) {
73 HttpHeaders headers = clientRequest.getHeaders();
74 headers.add(ONAPLogConstants.Headers.REQUEST_ID, extractRequestID(clientRequest));
75 headers.add(ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
76 headers.add(ONAPLogConstants.Headers.PARTNER_NAME, SO);
79 private String extractRequestID(HttpRequest clientRequest) {
80 String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
81 if(requestId == null || requestId.isEmpty() || requestId.equals(TRACE)){
82 requestId = UUID.randomUUID().toString();
83 log.warn("Could not Find Request ID Generating New One: {}",clientRequest.getURI());
88 private void setupMDC(HttpRequest clientRequest) {
89 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
90 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, clientRequest.getURI().toString());
91 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
93 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY,extractTargetEntity(clientRequest));
96 private String extractTargetEntity(HttpRequest clientRequest) {
97 HttpHeaders headers = clientRequest.getHeaders();
98 String headerTargetEntity = null;
99 List<String> headerTargetEntityList = headers.get(LogConstants.TARGET_ENTITY_HEADER);
100 if(headerTargetEntityList!= null && !headerTargetEntityList.isEmpty())
101 headerTargetEntity = headerTargetEntityList.get(0);
102 String targetEntity = MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY);
103 if(targetEntity != null &&
104 !targetEntity.isEmpty() ){
106 }else if(headerTargetEntity != null &&
107 !headerTargetEntity.isEmpty()){
108 targetEntity = headerTargetEntity;
110 targetEntity = LogConstants.UNKNOWN_TARGET_ENTITY;
111 log.warn("Could not Target Entity: {}",clientRequest.getURI());
116 private void setInvocationId() {
117 String invocationId = MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID);
118 if(invocationId == null || invocationId.isEmpty())
119 invocationId =UUID.randomUUID().toString();
120 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
124 private void processResponse(ClientHttpResponse response) throws IOException {
125 if (log.isDebugEnabled()) {
126 log.debug("============================response begin==========================================");
127 log.debug("Status code : {}", response.getStatusCode());
128 log.debug("Status text : {}", response.getStatusText());
129 log.debug("Headers : {}", response.getHeaders());
130 log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
131 log.debug("=======================response end=================================================");
134 if(Response.Status.Family.familyOf(response.getRawStatusCode()).equals(Response.Status.Family.SUCCESSFUL)){
135 statusCode=ONAPLogConstants.ResponseStatus.COMPLETED.toString();
137 statusCode=ONAPLogConstants.ResponseStatus.ERROR.toString();
139 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(response.getRawStatusCode()));
140 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION,"");
141 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
142 log.info(ONAPLogConstants.Markers.INVOKE_RETURN, "InvokeReturn");
146 private void clearClientMDCs() {
147 MDC.remove(ONAPLogConstants.MDCs.INVOCATION_ID);
148 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
149 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
150 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
151 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
152 MDC.remove(ONAPLogConstants.MDCs.TARGET_ENTITY);
153 MDC.remove(ONAPLogConstants.MDCs.PARTNER_NAME);
154 MDC.remove(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
155 MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP);