23fd0bc797bfc640a29eb4254072c259415a80e3
[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.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.RESPONSE_CODE;
20 import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.SERVICE_NAME;
21 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
22
23 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapCustomConfig;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.slf4j.MDC;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
29 import org.springframework.web.reactive.function.client.WebClient;
30 import org.springframework.web.reactive.function.client.WebClient.Builder;
31
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.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         Builder webClientBuilder = WebClient.builder()
63                 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType) //
64                 .filter(logRequest()) //
65                 .filter(logResponse());
66         if (dmaaPUserName != null && !dmaaPUserName.isEmpty() && dmaaPUserPassword != null
67                 && !dmaaPUserPassword.isEmpty()) {
68             webClientBuilder.filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword));
69         }
70         return webClientBuilder.build();
71     }
72
73     private ExchangeFilterFunction logResponse() {
74         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
75             MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
76             logger.trace("Response Status {}", clientResponse.statusCode());
77             MDC.remove(RESPONSE_CODE);
78             return Mono.just(clientResponse);
79         });
80     }
81
82     private ExchangeFilterFunction logRequest() {
83         return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
84             MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url()));
85             logger.trace("Request: {} {}", clientRequest.method(), clientRequest.url());
86             clientRequest.headers()
87                     .forEach((name, values) -> values.forEach(value -> logger.trace("{}={}", name, value)));
88             logger.trace("HTTP request headers: {}", clientRequest.headers());
89             MDC.remove(SERVICE_NAME);
90             return Mono.just(clientRequest);
91         });
92     }
93
94 }