2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.logging.filter.spring;
23 import java.io.IOException;
24 import java.util.List;
25 import org.onap.logging.filter.base.AbstractMetricLogFilter;
26 import org.onap.logging.filter.base.Constants;
27 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpRequest;
31 import org.springframework.http.client.ClientHttpRequestExecution;
32 import org.springframework.http.client.ClientHttpRequestInterceptor;
33 import org.springframework.http.client.ClientHttpResponse;
35 public class SpringClientFilter extends AbstractMetricLogFilter<HttpRequest, ClientHttpResponse, HttpHeaders>
36 implements ClientHttpRequestInterceptor {
38 public SpringClientFilter() {
43 public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
45 pre(request, request.getHeaders());
46 ClientHttpResponse response = execution.execute(request, body);
47 post(request, response);
52 protected void addHeader(HttpHeaders requestHeaders, String headerName, String headerValue) {
53 requestHeaders.add(headerName, headerValue);
57 protected String getTargetServiceName(HttpRequest request) {
58 return request.getURI().toString();
62 protected String getServiceName(HttpRequest request) {
63 return request.getURI().getPath();
67 protected int getHttpStatusCode(ClientHttpResponse response) {
69 return response.getStatusCode().value();
70 } catch (IOException e) {
71 // TODO figure out the right thing to do here
77 protected String getResponseCode(ClientHttpResponse response) {
79 return response.getStatusCode().toString();
80 } catch (IOException e) {
86 protected String getTargetEntity(HttpRequest clientRequest) {
87 HttpHeaders headers = clientRequest.getHeaders();
88 String headerTargetEntity = null;
89 List<String> headerTargetEntityList = headers.get(Constants.HttpHeaders.TARGET_ENTITY_HEADER);
90 if (headerTargetEntityList != null && !headerTargetEntityList.isEmpty())
91 headerTargetEntity = headerTargetEntityList.get(0);
92 String targetEntity = MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY);
93 if (targetEntity != null && !targetEntity.isEmpty()) {
95 } else if (headerTargetEntity != null && !headerTargetEntity.isEmpty()) {
96 targetEntity = headerTargetEntity;
98 targetEntity = Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
99 logger.warn("Could not Target Entity: {}", clientRequest.getURI());