metric log updates and util fix
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / FilteredMetricLogClientFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 2020 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.base;
22
23 import java.util.Map;
24
25 import javax.ws.rs.client.ClientRequestContext;
26 import javax.ws.rs.client.ClientResponseContext;
27
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.slf4j.MDC;
30
31 public class FilteredMetricLogClientFilter extends MetricLogClientFilter {
32     protected CustomFilter<ClientRequestContext, ClientResponseContext> customFilter;
33     private static final String REQUEST_STATE_PROPERTY_NAME = "org.onap.logging.filter.base.RequestState";
34
35     public FilteredMetricLogClientFilter(CustomFilter<ClientRequestContext, ClientResponseContext> f) {
36         customFilter = f;
37     }
38
39     protected void additionalPre(ClientRequestContext request) {
40         request.setProperty(REQUEST_STATE_PROPERTY_NAME, MDC.getCopyOfContextMap());
41     }
42
43     protected void additionalPost(ClientRequestContext request, ClientResponseContext response) {
44         if (customFilter.shouldLog(request, response)) {
45             Map<String, String> responseState = MDC.getCopyOfContextMap();
46             Map<String, String> requestState = (Map<String, String>) request.getProperty(REQUEST_STATE_PROPERTY_NAME);
47             MDC.setContextMap(requestState);
48             logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
49             MDC.setContextMap(responseState);
50             logger.info(INVOKE_RETURN, "InvokeReturn");
51         }
52     }
53
54     protected void logRequest() {
55         // override with empty so log entries are not duplicated
56     }
57
58     protected void logResponse() {
59         // override with empty so log entries are not duplicated
60     }
61
62 }