2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.servlet;
19 import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_PARTNER_NAME_HEADER;
20 import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_REQUEST_ID_HEADER;
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;
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>
45 * <filter-name>LoggingServletFilter</filter-name>
46 * <filter-class>org.openecomp.sdc.logging.servlet.LoggingFilter</filter-class>
48 * <param-name>requestIdHeaders</param-name>
49 * <param-value>X-ONAP-RequestID</param-value>
52 * <param-name>partnerNameHeaders</param-name>
53 * <param-value>USER_ID</param-value>
57 * <filter-mapping>
58 * <filter-name>LoggingServletFilter</filter-name>
59 * <url-pattern>/*</url-pattern>
60 * </filter-mapping>
66 * @deprecated Kept for backward compatibility. For JAX-RS application, use
67 * {@link LoggingRequestFilter} and
68 * {@link LoggingResponseFilter} instead.
71 public class LoggingFilter implements Filter {
73 static final String MULTI_VALUE_SEPARATOR = ",";
75 static final String REQUEST_ID_HEADERS_PARAM = "requestIdHeaders";
76 static final String PARTNER_NAME_HEADERS_PARAM = "partnerNameHeaders";
78 private static final Logger LOGGER = LoggerFactory.getLogger(LoggingFilter.class);
80 private HttpHeader requestIdHeaders;
81 private HttpHeader partnerNameHeaders;
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);
90 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
91 throws IOException, ServletException {
93 HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
97 LoggingContext.clear();
99 ContextData.ContextDataBuilder contextData = ContextData.builder();
101 contextData.serviceName(httpRequest.getRequestURI());
103 String requestId = requestIdHeaders.getAny(httpRequest::getHeader);
104 contextData.requestId(requestId == null ? UUID.randomUUID().toString() : requestId);
106 String partner = partnerNameHeaders.getAny(httpRequest::getHeader);
107 if (partner != null) {
108 contextData.partnerName(partner);
111 LoggingContext.put(contextData.build());
113 chain.doFilter(request, response);
116 LoggingContext.clear();
121 public void destroy() {
122 // forced by the interface - not implemented
125 private HttpHeader getInitParam(FilterConfig config, String paramName, String defaultValue) {
127 String value = config.getInitParameter(paramName);
128 LOGGER.debug("Logging filter configuration param '{}' value '{}'", paramName, value);
131 return new HttpHeader(defaultValue);
133 return new HttpHeader(value.split(MULTI_VALUE_SEPARATOR));