Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-logging-lib / openecomp-sdc-logging-core / src / main / java / org / openecomp / sdc / logging / servlet / LoggingFilter.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.logging.servlet;
18
19 import org.openecomp.sdc.logging.api.Logger;
20 import org.openecomp.sdc.logging.api.LoggerFactory;
21 import org.slf4j.MDC;
22
23 import javax.servlet.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.FilterConfig;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29 import javax.servlet.http.HttpServletRequest;
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33 import java.util.UUID;
34 import java.util.concurrent.atomic.AtomicLong;
35
36
37 /**
38  *
39  * <p>Pushes information required by EELF onto MDC (Mapped Diagnostic Context).</p>
40  *
41  * <p>This is servlet filter that should be configured in <i>web.xml</i> to be used. Example:</p>
42  *
43  * <pre>
44  *
45  *  &lt;filter&gt;
46  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
47  *      &lt;filter-class&gt;org.openecomp.sdc.logging.servlet.LoggingFilter&lt;/filter-class&gt;
48  *  &lt;/filter&gt;
49  *
50  *  &lt;filter-mapping&gt;
51  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
52  *      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
53  *  &lt;/filter-mapping&gt;
54  *
55  * </pre>
56  *
57  * @author evitaliy
58  * @since 25/07/2016.
59  */
60 public class LoggingFilter implements Filter {
61
62     // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes
63     private static final HostAddressCache HOST_ADDRESS = new HostAddressCache();
64     private static final String UNKNOWN = "UNKNOWN";
65
66     private static final Logger LOGGER = LoggerFactory.getLogger(LoggingFilter.class);
67
68     public void destroy() {
69     }
70
71     public void doFilter(ServletRequest request, ServletResponse response,
72                          FilterChain chain) throws IOException, ServletException {
73
74         try {
75
76             MDC.clear();
77
78             MDC.put("RequestId", UUID.randomUUID().toString());
79             MDC.put("ServiceInstanceId", "N/A"); // not applicable
80             MDC.put("ServiceName", "ASDC");
81             MDC.put("InstanceUUID", "N/A");
82
83             // For some reason chooses IPv4 or IPv6 in a random way
84             MDC.put("RemoteHost", request.getRemoteHost());
85
86             InetAddress host = HOST_ADDRESS.get();
87
88             String ipAddress;
89             String hostName;
90             if (host == null) {
91                 ipAddress = UNKNOWN;
92                 hostName = UNKNOWN;
93             } else {
94                 ipAddress = host.getHostAddress();
95                 hostName = host.getHostName();
96             }
97
98             MDC.put("ServerIPAddress", ipAddress);
99             MDC.put("ServerFQDN", hostName);
100
101             if(request instanceof HttpServletRequest) {
102                 String userName = ((HttpServletRequest) request).getHeader("USER_ID");
103                 MDC.put("PartnerName", userName);
104             }
105             // TODO: Clarify what these stand for
106     //        MDC.put("AlertSeverity", );
107     //        MDC.put("Timer", );
108
109             chain.doFilter(request, response);
110
111         } finally {
112             MDC.clear();
113         }
114     }
115
116     public void init(FilterConfig config) throws ServletException { }
117
118     private static class HostAddressCache {
119
120         private static final long REFRESH_TIME = 1000L;
121
122         private final AtomicLong lastUpdated = new AtomicLong(0L);
123         private InetAddress hostAddress;
124
125         public InetAddress get() {
126
127             long current = System.currentTimeMillis();
128             if (current - lastUpdated.get() > REFRESH_TIME) {
129
130                 synchronized (this) {
131
132                     try {
133                         lastUpdated.set(current); // set now to register the attempt even if failed
134                         hostAddress = InetAddress.getLocalHost();
135                     } catch (UnknownHostException e) {
136                         LOGGER.error("Failed to retrieve local hostname for logging", e);
137                         hostAddress = null;
138                     }
139                 }
140             }
141
142             return hostAddress;
143         }
144     }
145 }