b1f2ab02d5ae3f0b58a062a9f5436f09a3f6b1e8
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
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.services.sdk.rest.services.dmaap.client.service.consumer;
22
23 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.RESPONSE_CODE;
24 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.SERVICE_NAME;
25
26 import io.netty.handler.ssl.SslContext;
27 import javax.net.ssl.SSLException;
28 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
29 import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.slf4j.MDC;
33 import org.springframework.http.client.reactive.ClientHttpConnector;
34 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
35 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
36 import org.springframework.web.reactive.function.client.WebClient;
37 import reactor.core.publisher.Mono;
38 import reactor.netty.http.client.HttpClient;
39
40 /**
41  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
42  */
43 public class DMaaPReactiveWebClientFactory {
44
45     private final Logger logger = LoggerFactory.getLogger(this.getClass());
46
47     private final SslFactory sslFactory;
48
49     public DMaaPReactiveWebClientFactory() {
50         this(new SslFactory());
51     }
52
53     DMaaPReactiveWebClientFactory(SslFactory sslFactory) {
54         this.sslFactory = sslFactory;
55     }
56
57     /**
58      * Construct Reactive WebClient with appropriate settings.
59      *
60      * @return WebClient
61      */
62     public WebClient build(DmaapConsumerConfiguration consumerConfiguration) throws SSLException {
63         SslContext sslContext = createSslContext(consumerConfiguration);
64         ClientHttpConnector reactorClientHttpConnector = new ReactorClientHttpConnector(
65                 HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)));
66         return WebClient.builder()
67                 .clientConnector(reactorClientHttpConnector)
68                 .filter(logRequest())
69                 .filter(logResponse())
70                 .build();
71     }
72
73     private SslContext createSslContext(DmaapConsumerConfiguration consumerConfiguration) throws SSLException {
74         if (consumerConfiguration.enableDmaapCertAuth()) {
75             return sslFactory.createSecureContext(
76                     consumerConfiguration.keyStorePath(), consumerConfiguration.keyStorePasswordPath(),
77                     consumerConfiguration.trustStorePath(), consumerConfiguration.trustStorePasswordPath()
78             );
79         }
80         return sslFactory.createInsecureContext();
81     }
82
83     private ExchangeFilterFunction logResponse() {
84         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
85             MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
86             logger.info("Response Status {}", clientResponse.statusCode());
87             MDC.remove(RESPONSE_CODE);
88             return Mono.just(clientResponse);
89         });
90     }
91
92     private ExchangeFilterFunction logRequest() {
93         return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
94             MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url()));
95             logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
96             clientRequest.headers()
97                 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
98             MDC.remove(SERVICE_NAME);
99             return Mono.just(clientRequest);
100         });
101     }
102
103 }