fb651495ac78618720a9f566b20c491b780f5376
[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.jaxrs;
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 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;
35
36 /**
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>
42  * <pre>
43  *     &lt;jaxrs:providers&gt;
44  *         &lt;bean class="org.openecomp.sdc.logging.ws.rs.LoggingRequestFilter"&gt;
45  *             &lt;property name="requestIdHeaders" value="X-ONAP-RequestID"/&gt;
46  *             &lt;property name="partnerNameHeaders" value="X-ONAP-InstanceID"/&gt;
47  *         &lt;/bean&gt;
48  *     &lt;/jaxrs:providers&gt;
49  * </pre>
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).
52  * </p>
53  *
54  * @author evitaliy, katyr
55  * @see ContainerRequestFilter
56  * @since 29 Oct 17
57  */
58 @Provider
59 public class LoggingRequestFilter implements ContainerRequestFilter {
60
61     static final String LOGGING_TRACKER_KEY = "onap.logging.tracker";
62
63     private static final String MULTI_VALUE_SEPARATOR = ",";
64     private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class);
65
66     private HttpServletRequest httpRequest;
67
68     private HttpHeader requestIdHeader = new HttpHeader(DEFAULT_REQUEST_ID_HEADER);
69     private HttpHeader partnerNameHeader = new HttpHeader(DEFAULT_PARTNER_NAME_HEADER);
70
71     private ResourceInfo resource;
72
73     /**
74      * Injection of a resource that matches the request from JAX-RS context.
75      *
76      * @param resource automatically injected by JAX-RS container
77      */
78     @Context
79     public void setResource(ResourceInfo resource) {
80         this.resource = resource;
81     }
82
83     /**
84      * Injection of HTTP request object from JAX-RS context.
85      *
86      * @param httpRequest automatically injected by JAX-RS container
87      */
88     @Context
89     public void setHttpRequest(HttpServletRequest httpRequest) {
90         this.httpRequest = httpRequest;
91     }
92
93     /**
94      * Configuration parameter for request ID HTTP header.
95      */
96     public void setRequestIdHeaders(String requestIdHeaders) {
97         LOGGER.debug("Valid request ID headers: {}", requestIdHeaders);
98         this.requestIdHeader = new HttpHeader(requestIdHeaders.split(MULTI_VALUE_SEPARATOR));
99     }
100
101     /**
102      * Configuration parameter for partner name HTTP header.
103      */
104     public void setPartnerNameHeaders(String partnerNameHeaders) {
105         LOGGER.debug("Valid partner name headers: {}", partnerNameHeaders);
106         this.partnerNameHeader = new HttpHeader(partnerNameHeaders.split(MULTI_VALUE_SEPARATOR));
107     }
108
109     @Override
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);
117     }
118 }