2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.logging.filter.base;
23 import javax.servlet.http.HttpServletRequest;
24 import org.onap.logging.ref.slf4j.ONAPLogConstants;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 public abstract class AbstractAuditLogFilter<GenericRequest, GenericResponse> extends MDCSetup {
30 protected static final Logger logger = LoggerFactory.getLogger(AbstractAuditLogFilter.class);
32 protected void pre(SimpleMap headers, GenericRequest request, HttpServletRequest httpServletRequest) {
34 String requestId = getRequestId(headers);
35 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
36 setInvocationId(headers);
37 setServiceName(request);
38 setMDCPartnerName(headers);
40 setClientIPAddress(httpServletRequest);
43 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
44 additionalPreHandling(request);
48 } catch (Exception e) {
49 logger.warn("Error in AbstractInboundFilter pre", e);
53 protected void post(GenericResponse response) {
55 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.SERVER_INVOCATION_ID));
56 int responseCode = getResponseCode(response);
57 setResponseStatusCode(responseCode);
58 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(responseCode));
59 setResponseDescription(responseCode);
62 additionalPostHandling(response);
64 } catch (Exception e) {
65 logger.warn("Error in AbstractInboundFilter post", e);
71 protected abstract int getResponseCode(GenericResponse response);
73 protected abstract void setServiceName(GenericRequest request);
75 protected void additionalPreHandling(GenericRequest request) {
76 // override to add additional pre handling
79 protected void additionalPostHandling(GenericResponse response) {
80 // override to add additional post handling
83 protected void logEntering() {
84 logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
87 protected void logExiting() {
88 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting.");