Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / logging / jaxrs / filter / MDCSetup.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.jaxrs.filter;
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.time.ZoneOffset;
26 import java.time.ZonedDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.util.Map;
29 import java.util.UUID;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.ws.rs.core.Response;
33 import org.onap.logging.ref.slf4j.ONAPLogConstants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class MDCSetup {
41
42     protected static Logger logger = LoggerFactory.getLogger(MDCSetup.class);
43
44     private static final String INSTANCE_UUID = UUID.randomUUID().toString();
45
46     public void setInstanceUUID() {
47         MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, INSTANCE_UUID);
48     }
49
50     public void setServerFQDN() {
51         String serverFQDN = "";
52         InetAddress addr = null;
53         try {
54             addr = InetAddress.getLocalHost();
55             serverFQDN = addr.toString();
56         } catch (UnknownHostException e) {
57             logger.warn("Cannot Resolve Host Name");
58             serverFQDN = "";
59         }
60         MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
61     }
62
63     public void setClientIPAddress(HttpServletRequest httpServletRequest) {
64         String remoteIpAddress = "";
65         if (httpServletRequest != null) {
66             remoteIpAddress = httpServletRequest.getRemoteAddr();
67         }
68         MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, remoteIpAddress);
69     }
70
71     public void setEntryTimeStamp() {
72         MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
73                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
74     }
75
76     public void setServiceName(HttpServletRequest request) {
77         MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI());
78     }
79
80     public void setRequestId(Map<String, String> headers) {
81         String requestId = headers.get(ONAPLogConstants.Headers.REQUEST_ID);
82         if (requestId == null || requestId.isEmpty())
83             requestId = UUID.randomUUID().toString();
84         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
85     }
86
87     public void setInvocationId(Map<String, String> headers) {
88         String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
89         if (invocationId == null || invocationId.isEmpty())
90             invocationId = UUID.randomUUID().toString();
91         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
92     }
93
94     public void setMDCPartnerName(Map<String, String> headers) {
95         String partnerName = headers.get(ONAPLogConstants.Headers.PARTNER_NAME);
96         if (partnerName == null || partnerName.isEmpty())
97             partnerName = "";
98         MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
99     }
100
101
102     public void setResponseStatusCode(HttpServletResponse response) {
103         String statusCode;
104         if (Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)) {
105             statusCode = ONAPLogConstants.ResponseStatus.COMPLETED.toString();
106         } else {
107             statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
108         }
109         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
110     }
111 }