Fix all blocker sonar issues and some checkstyle
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / aailog / filter / RestClientLoggingInterceptor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright Â© 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
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 package org.onap.aai.aailog.filter;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.UUID;
25 import org.onap.aai.aailog.logs.ServiceName;
26 import org.onap.logging.filter.base.AbstractMetricLogFilter;
27 import org.onap.logging.filter.base.Constants;
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.slf4j.MDC;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpRequest;
32 import org.springframework.http.client.ClientHttpRequestExecution;
33 import org.springframework.http.client.ClientHttpRequestInterceptor;
34 import org.springframework.http.client.ClientHttpResponse;
35
36 public class RestClientLoggingInterceptor extends AbstractMetricLogFilter<HttpRequest, ClientHttpResponse, HttpHeaders> implements ClientHttpRequestInterceptor {
37     @Override
38     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
39         throws IOException
40     {
41         this.setInvocationId(request.getHeaders());
42         pre(request, request.getHeaders());
43         ClientHttpResponse resp = execution.execute(request, body);
44         post(request, resp);
45         return resp;
46
47     }
48     protected void pre(HttpRequest request, HttpHeaders requestHeaders) {
49         try {
50             setupMDC(request);
51             setupHeaders(request, requestHeaders);
52             super.logInvoke();
53         } catch (Exception e) {
54             logger.warn("Error in RestClientLoggingInterceptor pre", e);
55         }
56     }
57     protected void setupHeaders(HttpRequest clientRequest, HttpHeaders requestHeaders) {
58         String requestId = extractRequestID(requestHeaders);
59         addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId);
60         addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
61         String transactionId = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID);
62         if (transactionId == null || transactionId.isEmpty()) {
63             addHeader(requestHeaders, Constants.HttpHeaders.TRANSACTION_ID, requestId);
64         }
65         addHeader(requestHeaders, Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
66         addHeader(requestHeaders, ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
67         String pName = getProperty(Constants.Property.PARTNER_NAME);
68         if (pName != null && (!pName.isEmpty())) {
69             addHeader(requestHeaders, ONAPLogConstants.Headers.PARTNER_NAME, pName);
70         }
71     }
72     protected String extractRequestID(HttpHeaders requestHeaders) {
73         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
74         if (requestId == null || requestId.isEmpty()) {
75             requestId = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID);
76             if (requestId == null || requestId.isEmpty()) {
77                 requestId = UUID.randomUUID().toString();
78             }
79             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
80         }
81         return requestId;
82     }
83     public void setInvocationId(HttpHeaders headers) {
84         String invocationId = null;
85
86         List<String> headerList = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
87         if (headerList != null && (!headerList.isEmpty())) {
88             for (String h : headerList) {
89                 if ( h != null && (!h.isEmpty()) ) {
90                     invocationId = h;
91                     break;
92                 }
93             }
94             headers.remove(ONAPLogConstants.Headers.INVOCATION_ID);
95         }
96         if (invocationId == null) {
97             invocationId = UUID.randomUUID().toString();
98         }
99         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
100     }
101     @Override
102     protected void addHeader(HttpHeaders requestHeaders, String headerName, String headerValue) {
103         requestHeaders.add(headerName, headerValue);
104     }
105
106     protected String getTargetServiceName(HttpRequest request) {
107         return (getServiceName(request));
108     }
109     protected String getServiceName(HttpRequest request){
110         String path = request.getURI().getRawPath();
111         return(ServiceName.extractServiceName(path));
112     }
113
114     protected int getHttpStatusCode(ClientHttpResponse response) {
115         int result = 0;
116         if (response != null ) {
117             try {
118                 result = response.getStatusCode().value();
119             }
120             catch (IOException e) {
121                 logger.warn("Error in RestClientLoggingInterceptor getHttpStatusCode {}", e.getMessage());
122             }
123         }
124         return result;
125     }
126
127     protected String getResponseCode(ClientHttpResponse response) {
128         String result = "";
129         if (response != null ) {
130             try {
131                 result = response.getStatusCode().toString();
132             }
133             catch (IOException e) {
134                 logger.warn("Error in RestClientLoggingInterceptor getResponseCode {}", e.getMessage());
135             }
136         }
137         return result;
138     }
139
140     protected String getTargetEntity(HttpRequest request) {
141         //TODO where do we get this from?
142         return Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
143     }
144 }