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.CombinedTracker;
31 import org.openecomp.sdc.logging.servlet.HttpHeader;
32 import org.openecomp.sdc.logging.servlet.Tracker;
35 * <p>Takes care of logging initialization an HTTP request hits the application. This includes populating logging
36 * context and tracking the request for audit. The filter <b>works in tandem</b> with {@link LoggingResponseFilter} or
37 * a similar implementation.</p>
38 * <p>The filter requires a few HTTP header names to be configured. These HTTP headers are used for propagating logging
39 * and tracing information between ONAP components. Sample configuration for a Spring environment:</p>
41 * <jaxrs:providers>
42 * <bean class="org.openecomp.sdc.logging.ws.rs.LoggingRequestFilter">
43 * <property name="requestIdHeaders" value="X-ONAP-RequestID"/>
44 * <property name="partnerNameHeaders" value="X-ONAP-InstanceID"/>
46 * </jaxrs:providers>
48 * <p>Keep in mind that the filters does nothing in case when a request cannot be mapped to a working JAX-RS resource
49 * (implementation). For instance, when the path is invalid (404), or there is no handler for a particular method (405).
52 * @author evitaliy, katyr
53 * @see ContainerRequestFilter
57 public class LoggingRequestFilter implements ContainerRequestFilter {
59 static final String LOGGING_TRACKER_KEY = "onap.logging.tracker";
61 private static final String MULTI_VALUE_SEPARATOR = ",";
62 private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class);
64 private HttpServletRequest httpRequest;
66 private HttpHeader requestIdHeader = new HttpHeader(new String[] {DEFAULT_REQUEST_ID_HEADER});
67 private HttpHeader partnerNameHeader = new HttpHeader(new String[] {DEFAULT_PARTNER_NAME_HEADER});
69 private ResourceInfo resource;
72 * Injection of a resource that matches the request from JAX-RS context.
74 * @param resource automatically injected by JAX-RS container
77 public void setResource(ResourceInfo resource) {
78 this.resource = resource;
82 * Injection of HTTP request object from JAX-RS context.
84 * @param httpRequest automatically injected by JAX-RS container
87 public void setHttpRequest(HttpServletRequest httpRequest) {
88 this.httpRequest = httpRequest;
92 * Configuration parameter for request ID HTTP header.
94 public void setRequestIdHeaders(String requestIdHeaders) {
95 LOGGER.debug("Valid request ID headers: {}", requestIdHeaders);
96 this.requestIdHeader = new HttpHeader(requestIdHeaders.split(MULTI_VALUE_SEPARATOR));
100 * Configuration parameter for partner name HTTP header.
102 public void setPartnerNameHeaders(String partnerNameHeaders) {
103 LOGGER.debug("Valid partner name headers: {}", partnerNameHeaders);
104 this.partnerNameHeader = new HttpHeader(partnerNameHeaders.split(MULTI_VALUE_SEPARATOR));
108 public void filter(ContainerRequestContext requestContext) {
109 Class<?> resourceClass = resource.getResourceMethod().getDeclaringClass();
110 Tracker tracker = new CombinedTracker(resourceClass, partnerNameHeader, requestIdHeader);
111 requestContext.setProperty(LOGGING_TRACKER_KEY, tracker);
112 tracker.preRequest(httpRequest);