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;
27 import org.onap.dcaegen2.collectors.datafile.config.AaiClientConfiguration;
28 import org.onap.dcaegen2.collectors.datafile.service.AaiReactiveWebClient;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
32 import org.springframework.web.reactive.function.client.WebClient;
33 import reactor.core.publisher.Mono;
36 public class AaiReactiveWebClient {
38 private Logger logger = LoggerFactory.getLogger(this.getClass());
40 private String aaiUserName;
41 private String aaiUserPassword;
42 private Map<String, String> aaiHeaders;
45 * Creating AaiReactiveWebClient.
46 * @param configuration - configuration object
47 * @return AaiReactiveWebClient
49 public AaiReactiveWebClient fromConfiguration(AaiClientConfiguration configuration) {
50 this.aaiUserName = configuration.aaiUserName();
51 this.aaiUserPassword = configuration.aaiUserPassword();
52 this.aaiHeaders = configuration.aaiHeaders();
57 * Construct Reactive WebClient with appropriate settings.
61 public WebClient build() {
62 return WebClient.builder()
63 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
64 .filter(basicAuthentication(aaiUserName, aaiUserPassword))
66 .filter(logResponse())
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);
79 private ExchangeFilterFunction logResponse() {
80 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
81 logger.info("Response Status {}", clientResponse.statusCode());
82 return Mono.just(clientResponse);