f617ea65001aebb467efc75b2754e20e050190a8
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.logging.servlet;
22
23 import org.omg.CORBA.Request;
24 import org.slf4j.MDC;
25
26 import javax.servlet.*;
27 import javax.servlet.http.HttpServletRequest;
28 import java.io.IOException;
29 import java.net.InetAddress;
30 import java.net.UnknownHostException;
31 import java.util.UUID;
32 import java.util.concurrent.atomic.AtomicLong;
33
34
35 /**
36  *
37  * <p>Pushes information required by EELF onto MDC (Mapped Diagnostic Context).</p>
38  *
39  * <p>This is servlet filter that should be configured in <i>web.xml</i> to be used. Example:</p>
40  *
41  * <pre>
42  *
43  *  &lt;filter&gt;
44  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
45  *      &lt;filter-class&gt;org.openecomp.sdc.logging.servlet.LoggingFilter&lt;/filter-class&gt;
46  *  &lt;/filter&gt;
47  *
48  *  &lt;filter-mapping&gt;
49  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
50  *      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
51  *  &lt;/filter-mapping&gt;
52  *
53  * </pre>
54  *
55  * @author evitaliy
56  * @since 25/07/2016.
57  */
58 public class LoggingFilter implements Filter {
59
60     // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes
61     private static final HostAddressCache HOST_ADDRESS = new HostAddressCache();
62     private static final String UNKNOWN = "UNKNOWN";
63
64     public void destroy() {
65     }
66
67     public void doFilter(ServletRequest request, ServletResponse response,
68                          FilterChain chain) throws IOException, ServletException {
69
70         try {
71
72             MDC.clear();
73
74             MDC.put("RequestId", UUID.randomUUID().toString());
75             MDC.put("ServiceInstanceId", "N/A"); // not applicable
76             MDC.put("ServiceName", "ASDC");
77             MDC.put("InstanceUUID", "N/A");
78
79             // For some reason chooses IPv4 or IPv6 in a random way
80             MDC.put("RemoteHost", request.getRemoteHost());
81
82             InetAddress host = HOST_ADDRESS.get();
83
84             String ipAddress, hostName;
85             if (host == null) {
86                 ipAddress = UNKNOWN;
87                 hostName = UNKNOWN;
88             } else {
89                 ipAddress = host.getHostAddress();
90                 hostName = host.getHostName();
91             }
92
93             MDC.put("ServerIPAddress", ipAddress);
94             MDC.put("ServerFQDN", hostName);
95
96             if(request instanceof HttpServletRequest) {
97                 String userName = ((HttpServletRequest) request).getHeader("USER_ID");
98                 MDC.put("PartnerName", userName);
99             }
100             // TODO: Clarify what these stand for
101     //        MDC.put("AlertSeverity", );
102     //        MDC.put("Timer", );
103
104             chain.doFilter(request, response);
105
106         } finally {
107             MDC.clear();
108         }
109     }
110
111     public void init(FilterConfig config) throws ServletException { }
112
113     private static class HostAddressCache {
114
115         private static final long REFRESH_TIME = 1000L;
116
117         private AtomicLong lastUpdated = new AtomicLong(0L);
118         private InetAddress hostAddress;
119
120         public InetAddress get() {
121
122             long current = System.currentTimeMillis();
123             if (current - lastUpdated.get() > REFRESH_TIME) {
124
125                 synchronized (this) {
126
127                     try {
128                         lastUpdated.set(current); // set now to register the attempt even if failed
129                         hostAddress = InetAddress.getLocalHost();
130                     } catch (UnknownHostException e) {
131                         hostAddress = null;
132                     }
133                 }
134             }
135
136             return hostAddress;
137         }
138     }
139 }