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