9a9ff27e809a94db7c5cd023019c718e546db818
[dcaegen2/collectors/datafile.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.service;
22
23 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
24
25 import java.util.Map;
26
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;
34
35
36 public class AaiReactiveWebClient {
37
38     private Logger logger = LoggerFactory.getLogger(this.getClass());
39
40     private String aaiUserName;
41     private String aaiUserPassword;
42     private Map<String, String> aaiHeaders;
43
44     /**
45      * Creating AaiReactiveWebClient.
46      * @param configuration - configuration object
47      * @return AaiReactiveWebClient
48      */
49     public AaiReactiveWebClient fromConfiguration(AaiClientConfiguration configuration) {
50         this.aaiUserName = configuration.aaiUserName();
51         this.aaiUserPassword = configuration.aaiUserPassword();
52         this.aaiHeaders = configuration.aaiHeaders();
53         return this;
54     }
55
56     /**
57      * Construct Reactive WebClient with appropriate settings.
58      *
59      * @return WebClient
60      */
61     public WebClient build() {
62         return WebClient.builder()
63                 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
64                 .filter(basicAuthentication(aaiUserName, aaiUserPassword))
65                 .filter(logRequest())
66                 .filter(logResponse())
67                 .build();
68     }
69
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);
76         });
77     }
78
79     private ExchangeFilterFunction logResponse() {
80         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
81             logger.info("Response Status {}", clientResponse.statusCode());
82             return Mono.just(clientResponse);
83         });
84     }
85 }