Spring-boot 3.1 update
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / logging / filter / base / AbstractAuditLogFilter.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.base;
22
23 import jakarta.servlet.http.HttpServletRequest;
24
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.slf4j.MDC;
29
30 public abstract class AbstractAuditLogFilter<GenericRequest, GenericResponse> extends MDCSetup {
31     protected static final Logger logger = LoggerFactory.getLogger(AbstractAuditLogFilter.class);
32
33     protected void pre(SimpleMap headers, GenericRequest request, HttpServletRequest httpServletRequest) {
34         try {
35             String requestId = getRequestId(headers);
36             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
37             setInvocationId(headers);
38             setServiceName(request);
39             setMDCPartnerName(headers);
40             setServerFQDN();
41             setClientIPAddress(httpServletRequest);
42             setInstanceID();
43             setEntryTimeStamp();
44             MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
45             additionalPreHandling(request);
46             setLogTimestamp();
47             setElapsedTime();
48         } catch (Exception e) {
49             logger.warn("Error in AbstractInboundFilter pre", e);
50         }
51     }
52
53     protected void post(GenericResponse response) {
54         try {
55             int responseCode = getResponseCode(response);
56             setResponseStatusCode(responseCode);
57             MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(responseCode));
58             setResponseDescription(responseCode);
59             setLogTimestamp();
60             setElapsedTime();
61             additionalPostHandling(response);
62         } catch (Exception e) {
63             logger.warn("Error in AbstractInboundFilter post", e);
64         } finally {
65             MDC.clear();
66         }
67     }
68
69     protected abstract int getResponseCode(GenericResponse response);
70
71     protected abstract void setServiceName(GenericRequest request);
72
73     protected void additionalPreHandling(GenericRequest request) {
74         // override to add additional pre handling
75     }
76
77     protected void additionalPostHandling(GenericResponse response) {
78         // override to add additional post handling
79     }
80
81 }