2 * ============LICENSE_START=======================================================
3 * Datafile Collector Service
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.collectors.datafile.service;
23 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
25 import org.onap.dcaegen2.collectors.datafile.config.DmaapCustomConfig;
26 import org.onap.dcaegen2.collectors.datafile.service.DMaaPReactiveWebClient;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
31 import org.springframework.web.reactive.function.client.WebClient;
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.dmaaPUserName = dmaapCustomConfig.dmaapUserName();
53 this.dmaaPUserPassword = dmaapCustomConfig.dmaapUserPassword();
54 this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
59 * Construct Reactive WebClient with appropriate settings.
63 public WebClient build() {
64 return WebClient.builder()
65 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
66 .filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword))
68 .filter(logResponse())
72 private ExchangeFilterFunction logResponse() {
73 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
74 logger.info("Response Status {}", clientResponse.statusCode());
75 return Mono.just(clientResponse);
79 private ExchangeFilterFunction logRequest() {
80 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
81 logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
82 clientRequest.headers()
83 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
84 return Mono.just(clientRequest);