095ba8c9e13f8a1f852afba5245e92143c241e52
[dcaegen2/collectors/datafile.git] /
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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========================================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.service;
20
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;
27
28 import reactor.core.publisher.Mono;
29
30 /**
31  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
32  */
33 public class DmaapReactiveWebClient {
34
35     private final Logger logger = LoggerFactory.getLogger(this.getClass());
36
37     private String dmaaPContentType;
38
39     /**
40      * Creating DmaapReactiveWebClient passing to them basic DmaapConfig.
41      *
42      * @param dmaapCustomConfig - configuration object
43      * @return DmaapReactiveWebClient
44      */
45     public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
46         this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
47         return this;
48     }
49
50     /**
51      * Construct Reactive WebClient with appropriate settings.
52      *
53      * @return WebClient
54      */
55     public WebClient build() {
56         return WebClient.builder()
57             .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
58             .filter(logRequest())
59             .filter(logResponse())
60             .build();
61     }
62
63     private ExchangeFilterFunction logResponse() {
64         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
65             logger.info("Response Status {}", clientResponse.statusCode());
66             return Mono.just(clientResponse);
67         });
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 }