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.cxf.interceptor;
24 import java.util.List;
26 import java.util.UUID;
27 import javax.servlet.http.HttpServletRequest;
28 import org.apache.cxf.binding.soap.SoapMessage;
29 import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
30 import org.apache.cxf.interceptor.Fault;
31 import org.apache.cxf.message.Message;
32 import org.apache.cxf.phase.Phase;
33 import org.apache.cxf.transport.http.AbstractHTTPDestination;
34 import org.onap.logging.filter.base.MDCSetup;
35 import org.onap.logging.ref.slf4j.ONAPLogConstants;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
41 public class SOAPLoggingInInterceptor extends AbstractSoapInterceptor {
43 protected static Logger logger = LoggerFactory.getLogger(SOAPLoggingInInterceptor.class);
45 public SOAPLoggingInInterceptor() {
50 public void handleMessage(SoapMessage message) throws Fault {
52 MDCSetup mdcSetup = new MDCSetup();
53 Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
54 HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
55 request.getRemoteAddr();
57 setRequestId(headers);
58 setInvocationId(headers);
59 setServiceName(message);
60 setMDCPartnerName(headers);
61 mdcSetup.setServerFQDN();
62 mdcSetup.setClientIPAddress(request);
63 mdcSetup.setInstanceID();
64 mdcSetup.setEntryTimeStamp();
65 mdcSetup.setLogTimestamp();
66 mdcSetup.setElapsedTime();
67 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "INPROGRESS");
68 logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
69 } catch (Exception e) {
70 logger.warn("Error in incoming SOAP Message Inteceptor", e);
74 private void setServiceName(SoapMessage message) {
75 String requestURI = (String) message.get(Message.REQUEST_URI);
76 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, requestURI);
79 // CXF Appears to flatten headers to lower case
80 private void setMDCPartnerName(Map<String, List<String>> headers) {
81 String partnerName = getValueOrDefault(headers, ONAPLogConstants.Headers.PARTNER_NAME.toLowerCase(), "UNKNOWN");
82 MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
85 private void setInvocationId(Map<String, List<String>> headers) {
86 String invocationId = getValueOrDefault(headers, ONAPLogConstants.Headers.INVOCATION_ID.toLowerCase(),
87 UUID.randomUUID().toString());
88 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
89 MDC.put(ONAPLogConstants.MDCs.SERVER_INVOCATION_ID, invocationId);
92 private void setRequestId(Map<String, List<String>> headers) {
93 String requestId = getValueOrDefault(headers, ONAPLogConstants.Headers.REQUEST_ID.toLowerCase(),
94 UUID.randomUUID().toString());
95 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
98 private String getValueOrDefault(Map<String, List<String>> headers, String headerName, String defaultValue) {
100 List<String> headerList = headers.get(headerName);
101 if (headerList != null && !headerList.isEmpty()) {
102 headerValue = headerList.get(0);
103 if (headerValue == null || headerValue.isEmpty())
104 headerValue = defaultValue;
106 headerValue = defaultValue;