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 org.onap.dcaegen2.collectors.datafile.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;
28 import reactor.core.publisher.Mono;
31 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
33 public class DmaapReactiveWebClient {
35 private final Logger logger = LoggerFactory.getLogger(this.getClass());
37 private String dmaaPContentType;
40 * Creating DmaapReactiveWebClient passing to them basic DmaapConfig.
42 * @param dmaapCustomConfig - configuration object
43 * @return DmaapReactiveWebClient
45 public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
46 this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
51 * Construct Reactive WebClient with appropriate settings.
55 public WebClient build() {
56 return WebClient.builder()
57 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
59 .filter(logResponse())
63 private ExchangeFilterFunction logResponse() {
64 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
65 logger.info("Response Status {}", clientResponse.statusCode());
66 return Mono.just(clientResponse);
70 private ExchangeFilterFunction logRequest() {
71 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
72 logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
73 clientRequest.headers()
74 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
75 return Mono.just(clientRequest);