Replaced all tabs with spaces in java and pom.xml
[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.ref.slf4j.ONAPLogConstants;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.slf4j.MDC;
38
39
40 public class SOAPLoggingInInterceptor extends AbstractSoapInterceptor {
41
42     protected static Logger logger = LoggerFactory.getLogger(SOAPLoggingInInterceptor.class);
43
44     public SOAPLoggingInInterceptor() {
45         super(Phase.READ);
46     }
47
48     @Override
49     public void handleMessage(SoapMessage message) throws Fault {
50         try {
51             SOAPMDCSetup mdcSetup = new SOAPMDCSetup();
52             Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
53             HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
54             request.getRemoteAddr();
55
56             setRequestId(headers);
57             setInvocationId(headers);
58             setServiceName(message);
59             setMDCPartnerName(headers);
60             mdcSetup.setServerFQDN();
61             mdcSetup.setClientIPAddress(request);
62             mdcSetup.setInstanceUUID();
63             mdcSetup.setEntryTimeStamp();
64             MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "INPROGRESS");
65             logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
66         } catch (Exception e) {
67             logger.warn("Error in incoming SOAP Message Inteceptor", e);
68         }
69     }
70
71     private void setServiceName(SoapMessage message) {
72         String requestURI = (String) message.get(Message.REQUEST_URI);
73         MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, requestURI);
74     }
75
76     // CXF Appears to flatten headers to lower case
77     private void setMDCPartnerName(Map<String, List<String>> headers) {
78         String partnerName = getValueOrDefault(headers, ONAPLogConstants.Headers.PARTNER_NAME.toLowerCase(), "");
79         MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
80     }
81
82     private void setInvocationId(Map<String, List<String>> headers) {
83         String invocationId = getValueOrDefault(headers, ONAPLogConstants.Headers.INVOCATION_ID.toLowerCase(),
84                 UUID.randomUUID().toString());
85         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
86     }
87
88     private void setRequestId(Map<String, List<String>> headers) {
89         String requestId = getValueOrDefault(headers, ONAPLogConstants.Headers.REQUEST_ID.toLowerCase(),
90                 UUID.randomUUID().toString());
91         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
92     }
93
94     private String getValueOrDefault(Map<String, List<String>> headers, String headerName, String defaultValue) {
95         String headerValue;
96         List<String> headerList = headers.get(headerName);
97         if (headerList != null && !headerList.isEmpty()) {
98             headerValue = headerList.get(0);
99             if (headerValue == null || headerValue.isEmpty())
100                 headerValue = defaultValue;
101         } else
102             headerValue = defaultValue;
103         return headerValue;
104     }
105
106 }