d5878b0d98ca808b46a3f43ad2fd608f478e4441
[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 static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
22
23 import org.onap.dcaegen2.collectors.datafile.config.DmaapCustomConfig;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
28 import org.springframework.web.reactive.function.client.WebClient;
29
30 import reactor.core.publisher.Mono;
31
32 /**
33  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
34  */
35 public class DmaapReactiveWebClient {
36
37     private final Logger logger = LoggerFactory.getLogger(this.getClass());
38
39     private String dmaaPContentType;
40     private String dmaaPUserName;
41     private String dmaaPUserPassword;
42
43     /**
44      * Creating DmaapReactiveWebClient passing to them basic DmaapConfig.
45      *
46      * @param dmaapCustomConfig - configuration object
47      * @return DmaapReactiveWebClient
48      */
49     public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
50         this.dmaaPUserName = dmaapCustomConfig.dmaapUserName();
51         this.dmaaPUserPassword = dmaapCustomConfig.dmaapUserPassword();
52         this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
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             .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
64             .filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword))
65             .filter(logRequest())
66             .filter(logResponse())
67             .build();
68     }
69
70     private ExchangeFilterFunction logResponse() {
71         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
72             logger.info("Response Status {}", clientResponse.statusCode());
73             return Mono.just(clientResponse);
74         });
75     }
76
77     private ExchangeFilterFunction logRequest() {
78         return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
79             logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
80             clientRequest.headers()
81                 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
82             return Mono.just(clientRequest);
83         });
84     }
85
86 }