2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END========================================================================
19 package org.onap.dcaegen2.collectors.datafile.service;
21 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
23 import org.onap.dcaegen2.collectors.datafile.config.DmaapCustomConfig;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
28 import org.springframework.web.reactive.function.client.WebClient;
30 import reactor.core.publisher.Mono;
33 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
35 public class DmaapReactiveWebClient {
37 private final Logger logger = LoggerFactory.getLogger(this.getClass());
39 private String dmaaPContentType;
40 private String dmaaPUserName;
41 private String dmaaPUserPassword;
44 * Creating DmaapReactiveWebClient passing to them basic DmaapConfig.
46 * @param dmaapCustomConfig - configuration object
47 * @return DmaapReactiveWebClient
49 public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
50 this.dmaaPUserName = dmaapCustomConfig.dmaapUserName();
51 this.dmaaPUserPassword = dmaapCustomConfig.dmaapUserPassword();
52 this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
57 * Construct Reactive WebClient with appropriate settings.
61 public WebClient build() {
62 return WebClient.builder()
63 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
64 .filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword))
66 .filter(logResponse())
70 private ExchangeFilterFunction logResponse() {
71 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
72 logger.info("Response Status {}", clientResponse.statusCode());
73 return Mono.just(clientResponse);
77 private ExchangeFilterFunction logRequest() {
78 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
79 logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
80 clientRequest.headers()
81 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
82 return Mono.just(clientRequest);