aboutsummaryrefslogtreecommitdiffstats
path: root/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestClientLoggingInterceptor.java
blob: 629587d5750abf620000ce1cffd7e6ee5ec3a6c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.aailog.filter;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import org.onap.aai.aailog.logs.ServiceName;
import org.onap.logging.filter.base.AbstractMetricLogFilter;
import org.onap.logging.filter.base.Constants;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.slf4j.MDC;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

public class RestClientLoggingInterceptor extends AbstractMetricLogFilter<HttpRequest, ClientHttpResponse, HttpHeaders> implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
        throws IOException
    {
        this.setInvocationId(request.getHeaders());
        pre(request, request.getHeaders());
        ClientHttpResponse resp = execution.execute(request, body);
        post(request, resp);
        return resp;

    }
    protected void pre(HttpRequest request, HttpHeaders requestHeaders) {
        try {
            setupMDC(request);
            setupHeaders(request, requestHeaders);
            super.logInvoke();
        } catch (Exception e) {
            logger.warn("Error in RestClientLoggingInterceptor pre", e);
        }
    }
    protected void setupHeaders(HttpRequest clientRequest, HttpHeaders requestHeaders) {
        String requestId = extractRequestID(requestHeaders);
        addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId);
        addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
        String transactionId = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID);
        if (transactionId == null || transactionId.isEmpty()) {
            addHeader(requestHeaders, Constants.HttpHeaders.TRANSACTION_ID, requestId);
        }
        addHeader(requestHeaders, Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
        addHeader(requestHeaders, ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
        String pName = getProperty(Constants.Property.PARTNER_NAME);
        if (pName != null && (!pName.isEmpty())) {
            addHeader(requestHeaders, ONAPLogConstants.Headers.PARTNER_NAME, pName);
        }
    }
    protected String extractRequestID(HttpHeaders requestHeaders) {
        String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
        if (requestId == null || requestId.isEmpty()) {
            requestId = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID);
            if (requestId == null || requestId.isEmpty()) {
                requestId = UUID.randomUUID().toString();
            }
            MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
        }
        return requestId;
    }
    public void setInvocationId(HttpHeaders headers) {
        String invocationId = null;

        List<String> headerList = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
        if (headerList != null && (!headerList.isEmpty())) {
            for (String h : headerList) {
                if ( h != null && (!h.isEmpty()) ) {
                    invocationId = h;
                    break;
                }
            }
            headers.remove(ONAPLogConstants.Headers.INVOCATION_ID);
        }
        if (invocationId == null) {
            invocationId = UUID.randomUUID().toString();
        }
        MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
    }
    @Override
    protected void addHeader(HttpHeaders requestHeaders, String headerName, String headerValue) {
        requestHeaders.add(headerName, headerValue);
    }

    protected String getTargetServiceName(HttpRequest request) {
        return (getServiceName(request));
    }
    protected String getServiceName(HttpRequest request){
        String path = request.getURI().getRawPath();
        return(ServiceName.extractServiceName(path));
    }

    protected int getHttpStatusCode(ClientHttpResponse response) {
        int result = 0;
        if (response != null ) {
            try {
                result = response.getStatusCode().value();
            }
            catch (IOException e) {
                logger.warn("Error in RestClientLoggingInterceptor getHttpStatusCode {}", e.getMessage());
            }
        }
        return result;
    }

    protected String getResponseCode(ClientHttpResponse response) {
        String result = "";
        if (response != null ) {
            try {
                result = response.getStatusCode().toString();
            }
            catch (IOException e) {
                logger.warn("Error in RestClientLoggingInterceptor getResponseCode {}", e.getMessage());
            }
        }
        return result;
    }

    protected String getTargetEntity(HttpRequest request) {
        //TODO where do we get this from?
        return Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
    }
}