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.jaxrs;
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 javax.servlet.http.HttpServletRequest;
23 import javax.ws.rs.container.ContainerRequestContext;
24 import javax.ws.rs.container.ContainerRequestFilter;
25 import javax.ws.rs.container.ResourceInfo;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.ext.Provider;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.servlet.AuditTracker;
31 import org.openecomp.sdc.logging.servlet.CombinedTracker;
32 import org.openecomp.sdc.logging.servlet.ContextTracker;
33 import org.openecomp.sdc.logging.servlet.HttpHeader;
34 import org.openecomp.sdc.logging.servlet.Tracker;
37 * <p>Takes care of logging initialization an HTTP request hits the application. This includes populating logging
38 * context and tracking the request for audit. The filter <b>works in tandem</b> with {@link LoggingResponseFilter} or
39 * a similar implementation.</p>
40 * <p>The filter requires a few HTTP header names to be configured. These HTTP headers are used for propagating logging
41 * and tracing information between ONAP components. Sample configuration for a Spring environment:</p>
43 * <jaxrs:providers>
44 * <bean class="org.openecomp.sdc.logging.ws.rs.LoggingRequestFilter">
45 * <property name="requestIdHeaders" value="X-ONAP-RequestID"/>
46 * <property name="partnerNameHeaders" value="X-ONAP-InstanceID"/>
48 * </jaxrs:providers>
50 * <p>Keep in mind that the filters does nothing in case when a request cannot be mapped to a working JAX-RS resource
51 * (implementation). For instance, when the path is invalid (404), or there is no handler for a particular method (405).
54 * @author evitaliy, katyr
55 * @see ContainerRequestFilter
59 public class LoggingRequestFilter implements ContainerRequestFilter {
61 static final String LOGGING_TRACKER_KEY = "onap.logging.tracker";
63 private static final String MULTI_VALUE_SEPARATOR = ",";
64 private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class);
66 private HttpServletRequest httpRequest;
68 private HttpHeader requestIdHeader = new HttpHeader(DEFAULT_REQUEST_ID_HEADER);
69 private HttpHeader partnerNameHeader = new HttpHeader(DEFAULT_PARTNER_NAME_HEADER);
71 private ResourceInfo resource;
74 * Injection of a resource that matches the request from JAX-RS context.
76 * @param resource automatically injected by JAX-RS container
79 public void setResource(ResourceInfo resource) {
80 this.resource = resource;
84 * Injection of HTTP request object from JAX-RS context.
86 * @param httpRequest automatically injected by JAX-RS container
89 public void setHttpRequest(HttpServletRequest httpRequest) {
90 this.httpRequest = httpRequest;
94 * Configuration parameter for request ID HTTP header.
96 public void setRequestIdHeaders(String requestIdHeaders) {
97 LOGGER.debug("Valid request ID headers: {}", requestIdHeaders);
98 this.requestIdHeader = new HttpHeader(requestIdHeaders.split(MULTI_VALUE_SEPARATOR));
102 * Configuration parameter for partner name HTTP header.
104 public void setPartnerNameHeaders(String partnerNameHeaders) {
105 LOGGER.debug("Valid partner name headers: {}", partnerNameHeaders);
106 this.partnerNameHeader = new HttpHeader(partnerNameHeaders.split(MULTI_VALUE_SEPARATOR));
110 public void filter(ContainerRequestContext requestContext) {
111 Class<?> resourceClass = resource.getResourceMethod().getDeclaringClass();
112 Tracker tracker = new CombinedTracker(
113 new ContextTracker(partnerNameHeader, requestIdHeader),
114 new AuditTracker(resourceClass));
115 requestContext.setProperty(LOGGING_TRACKER_KEY, tracker);
116 tracker.preRequest(httpRequest);