06660f35f44d13d84da64a5c8611b199067d367e
[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 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;
31
32 /**
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>
39  * <pre>
40  *     &lt;jaxrs:providers&gt;
41  *         &lt;bean class="org.openecomp.sdc.logging.ws.rs.LoggingRequestFilter"&gt;
42  *             &lt;property name="requestIdHeaders" value="X-ONAP-RequestID"/&gt;
43  *             &lt;property name="partnerNameHeaders" value="X-ONAP-InstanceID"/&gt;
44  *         &lt;/bean&gt;
45  *     &lt;/jaxrs:providers&gt;
46  * </pre>
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).
49  * </p>
50  *
51  * @author evitaliy, katyr
52  * @since 29 Oct 17
53  *
54  * @see ContainerRequestFilter
55  */
56 @Provider
57 public class LoggingRequestFilter implements ContainerRequestFilter {
58
59     static final String MULTI_VALUE_SEPARATOR = ",";
60
61     static final String START_TIME_KEY = "audit.start.time";
62
63     private ResourceInfo resource;
64
65     private HttpHeader requestIdHeader = new HttpHeader(DEFAULT_REQUEST_ID_HEADER);
66     private HttpHeader partnerNameHeader = new HttpHeader(DEFAULT_PARTNER_NAME_HEADER);
67
68     /**
69      * Injection of a resource that matches the request from JAX-RS context.
70      *
71      * @param resource automatically injected by JAX-RS container
72      */
73     @Context
74     public void setResource(ResourceInfo resource) {
75         this.resource = resource;
76     }
77
78     /**
79      * Configuration parameter for request ID HTTP header.
80      */
81     public void setRequestIdHeaders(String requestIdHeaders) {
82         this.requestIdHeader = new HttpHeader(requestIdHeaders.split(MULTI_VALUE_SEPARATOR));
83     }
84
85     /**
86      * Configuration parameter for partner name HTTP header.
87      */
88     public void setPartnerNameHeaders(String partnerNameHeaders) {
89         this.partnerNameHeader = new HttpHeader(partnerNameHeaders.split(MULTI_VALUE_SEPARATOR));
90     }
91
92     @Override
93     public void filter(ContainerRequestContext containerRequestContext) {
94
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
98             return;
99         }
100
101         containerRequestContext.setProperty(START_TIME_KEY, System.currentTimeMillis());
102
103         LoggingContext.clear();
104
105         ContextData.ContextDataBuilder contextData = ContextData.builder();
106         contextData.serviceName(resource.getResourceClass().getName() + "." + resource.getResourceMethod().getName());
107
108         String partnerName = partnerNameHeader.getAny(containerRequestContext::getHeaderString);
109         if (partnerName != null) {
110             contextData.partnerName(partnerName);
111         }
112
113         String requestId = requestIdHeader.getAny(containerRequestContext::getHeaderString);
114         contextData.requestId(requestId == null ? UUID.randomUUID().toString() : requestId);
115
116         LoggingContext.put(contextData.build());
117     }
118 }