2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.prh.service.consumer;
23 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.RESPONSE_CODE;
24 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.SERVICE_NAME;
26 import io.netty.handler.ssl.SslContext;
27 import javax.net.ssl.SSLException;
28 import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration;
29 import org.onap.dcaegen2.services.prh.ssl.SslFactory;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
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;
42 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
44 public class DMaaPReactiveWebClientFactory {
46 private final Logger logger = LoggerFactory.getLogger(this.getClass());
48 private final SslFactory sslFactory;
50 public DMaaPReactiveWebClientFactory() {
51 this(new SslFactory());
54 DMaaPReactiveWebClientFactory(SslFactory sslFactory) {
55 this.sslFactory = sslFactory;
59 * Construct Reactive WebClient with appropriate settings.
63 public WebClient build(DmaapConsumerConfiguration consumerConfiguration) throws SSLException {
64 SslContext sslContext = createSslContext(consumerConfiguration);
65 ClientHttpConnector reactorClientHttpConnector = new ReactorClientHttpConnector(
66 HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)));
67 return WebClient.builder()
68 .clientConnector(reactorClientHttpConnector)
70 .filter(logResponse())
74 private SslContext createSslContext(DmaapConsumerConfiguration consumerConfiguration) throws SSLException {
75 if (consumerConfiguration.enableDmaapCertAuth()) {
76 return sslFactory.createSecureContext(
77 consumerConfiguration.keyStorePath(), consumerConfiguration.keyStorePasswordPath(),
78 consumerConfiguration.trustStorePath(), consumerConfiguration.trustStorePasswordPath()
81 return sslFactory.createInsecureContext();
84 private ExchangeFilterFunction logResponse() {
85 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
86 MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
87 logger.info("Response Status {}", clientResponse.statusCode());
88 MDC.remove(RESPONSE_CODE);
89 return Mono.just(clientResponse);
93 private ExchangeFilterFunction logRequest() {
94 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
95 MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url()));
96 logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
97 clientRequest.headers()
98 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
99 MDC.remove(SERVICE_NAME);
100 return Mono.just(clientRequest);