2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.apps.controllerblueprints.filters;
\r
20 import com.google.common.base.Preconditions;
\r
21 import org.apache.commons.lang3.StringUtils;
\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;
\r
23 import org.slf4j.Logger;
\r
24 import org.slf4j.LoggerFactory;
\r
25 import org.slf4j.MDC;
\r
26 import org.springframework.beans.factory.annotation.Value;
\r
27 import org.springframework.context.annotation.Configuration;
\r
28 import org.springframework.http.HttpHeaders;
\r
29 import org.springframework.http.server.reactive.ServerHttpRequest;
\r
30 import org.springframework.http.server.reactive.ServerHttpResponse;
\r
31 import org.springframework.web.server.ServerWebExchange;
\r
32 import org.springframework.web.server.WebFilter;
\r
33 import org.springframework.web.server.WebFilterChain;
\r
34 import reactor.core.publisher.Mono;
\r
36 import java.time.ZoneOffset;
\r
37 import java.time.ZonedDateTime;
\r
38 import java.time.format.DateTimeFormatter;
\r
39 import java.util.UUID;
\r
42 * ApplicationLoggingFilter
\r
44 * @author Brinda Santh 8/14/2018
\r
47 @SuppressWarnings("unused")
\r
48 public class ApplicationLoggingFilter implements WebFilter {
\r
49 private static Logger log = LoggerFactory.getLogger(ApplicationLoggingFilter.class);
\r
51 @SuppressWarnings("unused")
\r
52 @Value("${appVersion}")
\r
53 private String appVersion;
\r
56 public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
\r
59 ServerHttpRequest request = serverWebExchange.getRequest();
\r
60 ServerHttpResponse response = serverWebExchange.getResponse();
\r
62 String[] tokens = StringUtils.split(appVersion, '.');
\r
63 Preconditions.checkNotNull(tokens, "failed to split application versions");
\r
64 Preconditions.checkArgument(tokens.length == 3, "failed to tokenize application versions");
\r
65 HttpHeaders header = response.getHeaders();
\r
67 String requestID = defaultToUUID(request.getHeaders().getFirst("X-ONAP-RequestID"));
\r
68 String invocationID = defaultToUUID(request.getHeaders().getFirst("X-ONAP-InvocationID"));
\r
69 String partnerName = defaultToEmpty(request.getHeaders().getFirst("X-ONAP-PartnerName"));
\r
70 MDC.put("InvokeTimestamp", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
\r
71 MDC.put("RequestID", requestID);
\r
72 MDC.put("InvocationID", invocationID);
\r
73 MDC.put("PartnerName", partnerName);
\r
74 MDC.put("ClientIPAddress", defaultToEmpty(request.getRemoteAddress().getAddress()));
\r
75 MDC.put("ServerFQDN", defaultToEmpty(request.getRemoteAddress().getHostString()));
\r
77 header.add(BluePrintConstants.RESPONSE_HEADER_TRANSACTION_ID, requestID);
\r
78 header.add(BluePrintConstants.RESPONSE_HEADER_MINOR_VERSION, tokens[1]);
\r
79 header.add(BluePrintConstants.RESPONSE_HEADER_PATCH_VERSION, tokens[2]);
\r
80 header.add(BluePrintConstants.RESPONSE_HEADER_LATEST_VERSION, appVersion);
\r
81 } catch (Exception e) {
\r
82 e.printStackTrace();
\r
85 return webFilterChain.filter(serverWebExchange);
\r
89 private static String defaultToUUID(String in) {
\r
90 return in == null ? UUID.randomUUID().toString() : in;
\r
93 private static String defaultToEmpty(Object in) {
\r
94 return in == null ? "" : in.toString();
\r