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