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 java.util.UUID;
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.ContextData;
29 import org.openecomp.sdc.logging.api.LoggingContext;
30 import org.openecomp.sdc.logging.servlet.HttpHeader;
33 * <p>Takes care of logging initialization an HTTP request hits the application. This includes populating logging
34 * context and storing the request processing start time, so that it can be used for audit. The filter was built
35 * <b>works in tandem</b> with {@link LoggingResponseFilter} or a similar implementation.</p>
36 * <p>The filter requires a few HTTP header names to be configured. These HTTP headers are used for propagating logging
37 * and tracing information between ONAP components.</p>
38 * <p>Sample configuration for a Spring environment:</p>
40 * <jaxrs:providers>
41 * <bean class="org.openecomp.sdc.logging.ws.rs.LoggingRequestFilter">
42 * <property name="requestIdHeaders" value="X-ONAP-RequestID"/>
43 * <property name="partnerNameHeaders" value="X-ONAP-InstanceID"/>
45 * </jaxrs:providers>
47 * <p>Keep in mind that the filters does nothing in case when a request cannot be mapped to a working JAX-RS resource
48 * (implementation). For instance, when the path is invalid (404), or there is no handler for a particular method (405).
51 * @author evitaliy, katyr
54 * @see ContainerRequestFilter
57 public class LoggingRequestFilter implements ContainerRequestFilter {
59 static final String MULTI_VALUE_SEPARATOR = ",";
61 static final String START_TIME_KEY = "audit.start.time";
63 private ResourceInfo resource;
65 private HttpHeader requestIdHeader = new HttpHeader(DEFAULT_REQUEST_ID_HEADER);
66 private HttpHeader partnerNameHeader = new HttpHeader(DEFAULT_PARTNER_NAME_HEADER);
69 * Injection of a resource that matches the request from JAX-RS context.
71 * @param resource automatically injected by JAX-RS container
74 public void setResource(ResourceInfo resource) {
75 this.resource = resource;
79 * Configuration parameter for request ID HTTP header.
81 public void setRequestIdHeaders(String requestIdHeaders) {
82 this.requestIdHeader = new HttpHeader(requestIdHeaders.split(MULTI_VALUE_SEPARATOR));
86 * Configuration parameter for partner name HTTP header.
88 public void setPartnerNameHeaders(String partnerNameHeaders) {
89 this.partnerNameHeader = new HttpHeader(partnerNameHeaders.split(MULTI_VALUE_SEPARATOR));
93 public void filter(ContainerRequestContext containerRequestContext) {
95 if (resource == null) {
96 // JAX-RS could not find a mapping this response, probably due to HTTP 404 (not found),
97 // 405 (method not allowed), 415 (unsupported media type), etc. with a message in Web server log
101 containerRequestContext.setProperty(START_TIME_KEY, System.currentTimeMillis());
103 LoggingContext.clear();
105 ContextData.ContextDataBuilder contextData = ContextData.builder();
106 contextData.serviceName(resource.getResourceClass().getName() + "." + resource.getResourceMethod().getName());
108 String partnerName = partnerNameHeader.getAny(containerRequestContext::getHeaderString);
109 if (partnerName != null) {
110 contextData.partnerName(partnerName);
113 String requestId = requestIdHeader.getAny(containerRequestContext::getHeaderString);
114 contextData.requestId(requestId == null ? UUID.randomUUID().toString() : requestId);
116 LoggingContext.put(contextData.build());