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