b4cbfeeaf7189638d3c17af4ff1cfacda2110460
[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 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;
33
34 /**
35  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
36  */
37 public class DMaaPReactiveWebClient {
38
39     private final Logger logger = LoggerFactory.getLogger(this.getClass());
40
41     private String dmaaPContentType;
42     private String dmaaPUserName;
43     private String dmaaPUserPassword;
44
45     /**
46      * Creating DMaaPReactiveWebClient passing to them basic DMaaPConfig.
47      *
48      * @param dmaapCustomConfig - configuration object
49      * @return DMaaPReactiveWebClient
50      */
51     public DMaaPReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
52         this.dmaaPUserName = dmaapCustomConfig.dmaapUserName();
53         this.dmaaPUserPassword = dmaapCustomConfig.dmaapUserPassword();
54         this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
55         return this;
56     }
57
58     /**
59      * Construct Reactive WebClient with appropriate settings.
60      *
61      * @return WebClient
62      */
63     public WebClient build() {
64         return WebClient.builder()
65             .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
66             .filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword))
67             .filter(logRequest())
68             .filter(logResponse())
69             .build();
70     }
71
72     private ExchangeFilterFunction logResponse() {
73         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
74             logger.info("Response Status {}", clientResponse.statusCode());
75             return Mono.just(clientResponse);
76         });
77     }
78
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);
85         });
86     }
87
88 }