2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. 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 distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.service;
19 import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.RESPONSE_CODE;
20 import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.SERVICE_NAME;
21 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
23 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapCustomConfig;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
29 import org.springframework.web.reactive.function.client.WebClient;
30 import org.springframework.web.reactive.function.client.WebClient.Builder;
32 import reactor.core.publisher.Mono;
35 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
37 public class DmaapReactiveWebClient {
39 private final Logger logger = LoggerFactory.getLogger(this.getClass());
41 private String dmaaPContentType;
42 private String dmaaPUserName;
43 private String dmaaPUserPassword;
46 * Creating DmaapReactiveWebClient passing to them basic DmaapConfig.
48 * @param dmaapCustomConfig - configuration object
49 * @return DmaapReactiveWebClient
51 public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
52 this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
57 * Construct Reactive WebClient with appropriate settings.
61 public WebClient build() {
62 Builder webClientBuilder = WebClient.builder()
63 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType) //
64 .filter(logRequest()) //
65 .filter(logResponse());
66 if (dmaaPUserName != null && !dmaaPUserName.isEmpty() && dmaaPUserPassword != null
67 && !dmaaPUserPassword.isEmpty()) {
68 webClientBuilder.filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword));
70 return webClientBuilder.build();
73 private ExchangeFilterFunction logResponse() {
74 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
75 MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
76 logger.trace("Response Status {}", clientResponse.statusCode());
77 MDC.remove(RESPONSE_CODE);
78 return Mono.just(clientResponse);
82 private ExchangeFilterFunction logRequest() {
83 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
84 MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url()));
85 logger.trace("Request: {} {}", clientRequest.method(), clientRequest.url());
86 clientRequest.headers()
87 .forEach((name, values) -> values.forEach(value -> logger.trace("{}={}", name, value)));
88 logger.trace("HTTP request headers: {}", clientRequest.headers());
89 MDC.remove(SERVICE_NAME);
90 return Mono.just(clientRequest);