new onap logo
[so.git] / cxf-logging / src / main / java / org / onap / so / logging / cxf / interceptor / SOAPLoggingInInterceptor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.so.logging.cxf.interceptor;
22
23
24 import java.util.List;
25 import java.util.Map;
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;
38 import org.slf4j.MDC;
39
40
41 public class SOAPLoggingInInterceptor extends AbstractSoapInterceptor {
42
43     protected static Logger logger = LoggerFactory.getLogger(SOAPLoggingInInterceptor.class);
44
45     public SOAPLoggingInInterceptor() {
46         super(Phase.READ);
47     }
48
49     @Override
50     public void handleMessage(SoapMessage message) throws Fault {
51         try {
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();
56
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);
71         }
72     }
73
74     private void setServiceName(SoapMessage message) {
75         String requestURI = (String) message.get(Message.REQUEST_URI);
76         MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, requestURI);
77     }
78
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);
83     }
84
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);
90     }
91
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);
96     }
97
98     private String getValueOrDefault(Map<String, List<String>> headers, String headerName, String defaultValue) {
99         String headerValue;
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;
105         } else
106             headerValue = defaultValue;
107         return headerValue;
108     }
109
110 }