e53f28119cb8c905fe3e4b970106facdef2dbe90
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 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 static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_PARTNER_NAME_HEADER;
20 import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_REQUEST_ID_HEADER;
21
22 import java.io.IOException;
23 import java.util.UUID;
24 import javax.servlet.Filter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.FilterConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import org.openecomp.sdc.logging.api.ContextData;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.logging.api.LoggingContext;
35 import org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter;
36 import org.openecomp.sdc.logging.servlet.jaxrs.LoggingResponseFilter;
37
38 /**
39  * <p>Places logging context information. Must be configured as a servlet filter in <i>web.xml</i>. The behavior can be
40  * customized via init-params.</p>
41  * <p>Example:</p>
42  * <pre>
43  *
44  *  &lt;filter&gt;
45  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
46  *      &lt;filter-class&gt;org.openecomp.sdc.logging.servlet.LoggingFilter&lt;/filter-class&gt;
47  *      &lt;init-param&gt;
48  *          &lt;param-name&gt;requestIdHeaders&lt;/param-name&gt;
49  *          &lt;param-value&gt;X-ONAP-RequestID&lt;/param-value&gt;
50  *      &lt;/init-param&gt;
51  *      &lt;init-param&gt;
52  *          &lt;param-name&gt;partnerNameHeaders&lt;/param-name&gt;
53  *          &lt;param-value&gt;USER_ID&lt;/param-value&gt;
54  *      &lt;/init-param&gt;
55  *  &lt;/filter&gt;
56  *
57  *  &lt;filter-mapping&gt;
58  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
59  *      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
60  *  &lt;/filter-mapping&gt;
61  *
62  * </pre>
63  *
64  * @author evitaliy
65  * @since 25 Jul 2016
66  * @deprecated Kept for backward compatibility. For JAX-RS application, use
67  * {@link LoggingRequestFilter} and
68  * {@link LoggingResponseFilter} instead.
69  */
70 @Deprecated
71 public class LoggingFilter implements Filter {
72
73     static final String MULTI_VALUE_SEPARATOR = ",";
74
75     static final String REQUEST_ID_HEADERS_PARAM = "requestIdHeaders";
76     static final String PARTNER_NAME_HEADERS_PARAM = "partnerNameHeaders";
77
78     private static final Logger LOGGER = LoggerFactory.getLogger(LoggingFilter.class);
79
80     private HttpHeader requestIdHeaders;
81     private HttpHeader partnerNameHeaders;
82
83     @Override
84     public void init(FilterConfig config) {
85         requestIdHeaders = getInitParam(config, REQUEST_ID_HEADERS_PARAM, DEFAULT_REQUEST_ID_HEADER);
86         partnerNameHeaders = getInitParam(config, PARTNER_NAME_HEADERS_PARAM, DEFAULT_PARTNER_NAME_HEADER);
87     }
88
89     @Override
90     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
91             throws IOException, ServletException {
92
93         HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
94
95         try {
96
97             LoggingContext.clear();
98
99             ContextData.ContextDataBuilder contextData = ContextData.builder();
100
101             contextData.serviceName(httpRequest.getRequestURI());
102
103             String requestId = requestIdHeaders.getAny(httpRequest::getHeader);
104             contextData.requestId(requestId == null ? UUID.randomUUID().toString() : requestId);
105
106             String partner = partnerNameHeaders.getAny(httpRequest::getHeader);
107             if (partner != null) {
108                 contextData.partnerName(partner);
109             }
110
111             LoggingContext.put(contextData.build());
112
113             chain.doFilter(request, response);
114
115         } finally {
116             LoggingContext.clear();
117         }
118     }
119
120     @Override
121     public void destroy() {
122         // forced by the interface - not implemented
123     }
124
125     private HttpHeader getInitParam(FilterConfig config, String paramName, String defaultValue) {
126
127         String value = config.getInitParameter(paramName);
128         LOGGER.debug("Logging filter configuration param '{}' value '{}'", paramName, value);
129
130         if (value == null) {
131             return new HttpHeader(defaultValue);
132         } else {
133             return new HttpHeader(value.split(MULTI_VALUE_SEPARATOR));
134         }
135     }
136 }