2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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.so.logging.spring.interceptor;
23 import java.util.Collections;
25 import java.util.UUID;
26 import java.util.stream.Collectors;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.ext.Providers;
32 import org.onap.logging.ref.slf4j.ONAPLogConstants;
33 import org.onap.so.logging.jaxrs.filter.MDCSetup;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39 import org.springframework.web.servlet.ModelAndView;
40 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
43 public class LoggingInterceptor extends HandlerInterceptorAdapter {
45 Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);
51 private Providers providers;
54 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
56 Map<String, String> headers = Collections.list(((HttpServletRequest) request).getHeaderNames())
58 .collect(Collectors.toMap(h -> h, request::getHeader));
59 setRequestId(headers);
60 setInvocationId(headers);
61 setServiceName(request);
62 setMDCPartnerName(headers);
63 mdcSetup.setClientIPAddress(request);
64 mdcSetup.setEntryTimeStamp();
65 mdcSetup.setInstanceUUID();
66 mdcSetup.setServerFQDN();
67 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "INPROGRESS");
68 logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
73 public void postHandle(
74 HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
76 setResponseStatusCode(response);
77 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION,"");
78 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE,String.valueOf(response.getStatus()));
79 logger.info(ONAPLogConstants.Markers.EXIT, "Exiting.");
83 private void setResponseStatusCode(HttpServletResponse response) {
85 if(Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){
86 statusCode=ONAPLogConstants.ResponseStatus.COMPLETED.toString();
88 statusCode= ONAPLogConstants.ResponseStatus.ERROR.toString();
90 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
93 private void setServiceName(HttpServletRequest request) {
94 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI());
97 private void setRequestId(Map<String, String> headers) {
98 String requestId=headers.get(ONAPLogConstants.Headers.REQUEST_ID);
99 if(requestId == null || requestId.isEmpty())
100 requestId = UUID.randomUUID().toString();
101 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID,requestId);
104 private void setInvocationId(Map<String, String> headers) {
105 String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
106 if(invocationId == null || invocationId.isEmpty())
107 invocationId =UUID.randomUUID().toString();
108 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
111 private void setMDCPartnerName(Map<String, String> headers) {
112 String partnerName=headers.get(ONAPLogConstants.Headers.PARTNER_NAME);
113 if(partnerName == null || partnerName.isEmpty())
115 MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME,partnerName);