updating logging filters
[logging-analytics.git] / reference / logging-filter / logging-filter-spring / src / main / java / org / onap / logging / filter / spring / SpringClientFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.logging.filter.spring;
22
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;
28 import org.slf4j.MDC;
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;
34
35 public class SpringClientFilter extends AbstractMetricLogFilter<HttpRequest, ClientHttpResponse, HttpHeaders>
36         implements ClientHttpRequestInterceptor {
37
38     public SpringClientFilter() {
39
40     }
41
42     @Override
43     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
44             throws IOException {
45         pre(request, request.getHeaders());
46         ClientHttpResponse response = execution.execute(request, body);
47         post(request, response);
48         return response;
49     }
50
51     @Override
52     protected void addHeader(HttpHeaders requestHeaders, String headerName, String headerValue) {
53         requestHeaders.add(headerName, headerValue);
54     }
55
56     @Override
57     protected String getTargetServiceName(HttpRequest request) {
58         return request.getURI().toString();
59     }
60
61     @Override
62     protected String getServiceName(HttpRequest request) {
63         return request.getURI().getPath();
64     }
65
66     @Override
67     protected int getHttpStatusCode(ClientHttpResponse response) {
68         try {
69             return response.getStatusCode().value();
70         } catch (IOException e) {
71             // TODO figure out the right thing to do here
72             return 500;
73         }
74     }
75
76     @Override
77     protected String getResponseCode(ClientHttpResponse response) {
78         try {
79             return response.getStatusCode().toString();
80         } catch (IOException e) {
81             return "500";
82         }
83     }
84
85     @Override
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()) {
94             return targetEntity;
95         } else if (headerTargetEntity != null && !headerTargetEntity.isEmpty()) {
96             targetEntity = headerTargetEntity;
97         } else {
98             targetEntity = Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
99             logger.warn("Could not Target Entity: {}", clientRequest.getURI());
100         }
101         return targetEntity;
102     }
103
104 }