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;
34 import java.io.IOException;
35 import java.nio.charset.Charset;
36 import java.time.ZoneOffset;
37 import java.time.ZonedDateTime;
38 import java.time.format.DateTimeFormatter;
39 import java.util.List;
40 import java.util.UUID;
41 import javax.ws.rs.core.Response;
43 public class SpringClientFilter implements ClientHttpRequestInterceptor {
45 private final Logger log = LoggerFactory.getLogger(this.getClass());
47 private static final String TRACE = "trace-#";
48 private static final String SO = "SO";
51 public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
53 processRequest(request, body);
54 ClientHttpResponse response = execution.execute(request, body);
55 processResponse(response);
59 private void processRequest(HttpRequest request, byte[] body) throws IOException {
61 setupHeaders(request);
63 if (log.isDebugEnabled()) {
64 log.debug("===========================request begin================================================");
65 log.debug("URI : {}", request.getURI());
66 log.debug("Method : {}", request.getMethod());
67 log.debug("Headers : {}", request.getHeaders());
68 log.debug("Request body: {}", new String(body, "UTF-8"));
69 log.debug("==========================request end================================================");
73 private void setupHeaders(HttpRequest clientRequest) {
74 HttpHeaders headers = clientRequest.getHeaders();
75 headers.add(ONAPLogConstants.Headers.REQUEST_ID, extractRequestID(clientRequest));
76 headers.add(ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
77 headers.add(ONAPLogConstants.Headers.PARTNER_NAME, SO);
80 private String extractRequestID(HttpRequest clientRequest) {
81 String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
82 if (requestId == null || requestId.isEmpty() || requestId.equals(TRACE)) {
83 requestId = UUID.randomUUID().toString();
84 log.warn("Could not Find Request ID Generating New One: {}", clientRequest.getURI());
89 private void setupMDC(HttpRequest clientRequest) {
90 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
91 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
92 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, clientRequest.getURI().toString());
93 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
94 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, extractTargetEntity(clientRequest));
97 private String extractTargetEntity(HttpRequest clientRequest) {
98 HttpHeaders headers = clientRequest.getHeaders();
99 String headerTargetEntity = null;
100 List<String> headerTargetEntityList = headers.get(LogConstants.TARGET_ENTITY_HEADER);
101 if (headerTargetEntityList != null && !headerTargetEntityList.isEmpty())
102 headerTargetEntity = headerTargetEntityList.get(0);
103 String targetEntity = MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY);
104 if (targetEntity != null && !targetEntity.isEmpty()) {
106 } else if (headerTargetEntity != null && !headerTargetEntity.isEmpty()) {
107 targetEntity = headerTargetEntity;
109 targetEntity = LogConstants.UNKNOWN_TARGET_ENTITY;
110 log.warn("Could not Target Entity: {}", clientRequest.getURI());
115 private void setInvocationId() {
116 String invocationId = MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID);
117 if (invocationId == null || invocationId.isEmpty())
118 invocationId = UUID.randomUUID().toString();
119 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
123 private void processResponse(ClientHttpResponse response) throws IOException {
124 if (log.isDebugEnabled()) {
125 log.debug("============================response begin==========================================");
126 log.debug("Status code : {}", response.getStatusCode());
127 log.debug("Status text : {}", response.getStatusText());
128 log.debug("Headers : {}", response.getHeaders());
129 log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
130 log.debug("=======================response end=================================================");
133 if (Response.Status.Family.familyOf(response.getRawStatusCode()).equals(Response.Status.Family.SUCCESSFUL)) {
134 statusCode = ONAPLogConstants.ResponseStatus.COMPLETED.toString();
136 statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
138 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(response.getRawStatusCode()));
139 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, "");
140 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
141 log.info(ONAPLogConstants.Markers.INVOKE_RETURN, "InvokeReturn");
145 private void clearClientMDCs() {
146 MDC.remove(ONAPLogConstants.MDCs.INVOCATION_ID);
147 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
148 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
149 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
150 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
151 MDC.remove(ONAPLogConstants.MDCs.TARGET_ENTITY);
152 MDC.remove(ONAPLogConstants.MDCs.PARTNER_NAME);
153 MDC.remove(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
154 MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP);