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.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
21 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapCustomConfig;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.http.HttpHeaders;
25 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
26 import org.springframework.web.reactive.function.client.WebClient;
27 import org.springframework.web.reactive.function.client.WebClient.Builder;
29 import reactor.core.publisher.Mono;
32 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
34 public class DmaapReactiveWebClient {
36 private final Logger logger = LoggerFactory.getLogger(this.getClass());
38 private String dmaaPContentType;
39 private String dmaaPUserName;
40 private String dmaaPUserPassword;
43 * Creating DmaapReactiveWebClient passing to them basic DmaapConfig.
45 * @param dmaapCustomConfig - configuration object
46 * @return DmaapReactiveWebClient
48 public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
49 this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
54 * Construct Reactive WebClient with appropriate settings.
58 public WebClient build() {
59 Builder webClientBuilder = WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
60 .filter(logRequest()).filter(logResponse());
61 if (dmaaPUserName != null && !dmaaPUserName.isEmpty() && dmaaPUserPassword != null
62 && !dmaaPUserPassword.isEmpty()) {
63 webClientBuilder.filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword));
66 return webClientBuilder.build();
69 private ExchangeFilterFunction logResponse() {
70 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
71 logger.trace("Response Status {}", clientResponse.statusCode());
72 return Mono.just(clientResponse);
76 private ExchangeFilterFunction logRequest() {
77 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
78 logger.trace("Request: {} {}", clientRequest.method(), clientRequest.url());
79 clientRequest.headers()
80 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
81 logger.trace("HTTP request headers: {}", clientRequest.headers());
82 return Mono.just(clientRequest);