e99b811403081aac953828def46e3a37e54b18af
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.service;
18
19 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
20
21 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.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 import org.springframework.web.reactive.function.client.WebClient.Builder;
28
29 import reactor.core.publisher.Mono;
30
31 /**
32  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
33  */
34 public class DmaapReactiveWebClient {
35
36     private final Logger logger = LoggerFactory.getLogger(this.getClass());
37
38     private String dmaaPContentType;
39     private String dmaaPUserName;
40     private String dmaaPUserPassword;
41
42     /**
43      * Creating DmaapReactiveWebClient passing to them basic DmaapConfig.
44      *
45      * @param dmaapCustomConfig - configuration object
46      * @return DmaapReactiveWebClient
47      */
48     public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) {
49         this.dmaaPContentType = dmaapCustomConfig.dmaapContentType();
50         return this;
51     }
52
53     /**
54      * Construct Reactive WebClient with appropriate settings.
55      *
56      * @return WebClient
57      */
58     public WebClient build() {
59         Builder webClientBuilder = WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType)
60                 .filter(logRequest()).filter(logResponse());
61         if (dmaaPUserName != null && !dmaaPUserName.isEmpty() && dmaaPUserPassword != null
62                 && !dmaaPUserPassword.isEmpty()) {
63             webClientBuilder.filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword));
64
65         }
66         return webClientBuilder.build();
67     }
68
69     private ExchangeFilterFunction logResponse() {
70         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
71             logger.trace("Response Status {}", clientResponse.statusCode());
72             return Mono.just(clientResponse);
73         });
74     }
75
76     private ExchangeFilterFunction logRequest() {
77         return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
78             logger.trace("Request: {} {}", clientRequest.method(), clientRequest.url());
79             clientRequest.headers()
80                     .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
81             logger.trace("HTTP request headers: {}", clientRequest.headers());
82             return Mono.just(clientRequest);
83         });
84     }
85
86 }