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;
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;
25 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
27 import io.netty.handler.ssl.SslContext;
29 import javax.net.ssl.SSLException;
30 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
31 import org.onap.dcaegen2.services.prh.ssl.SslFactory;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import org.springframework.http.client.reactive.ClientHttpConnector;
36 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
37 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
38 import org.springframework.web.reactive.function.client.WebClient;
39 import reactor.core.publisher.Mono;
40 import reactor.netty.http.client.HttpClient;
43 public class AaiReactiveWebClientFactory {
45 private static final Logger LOGGER = LoggerFactory.getLogger(AaiReactiveWebClientFactory.class);
47 private final String aaiUserName;
48 private final String aaiUserPassword;
49 private final Map<String, String> aaiHeaders;
50 private final Boolean enableAaiCertAuth;
51 private final String trustStorePath;
52 private final String trustStorePasswordPath;
53 private final String keyStorePath;
54 private final String keyStorePasswordPath;
55 private final SslFactory sslFactory;
58 * Creating AaiReactiveWebClientFactory.
60 * @param configuration - configuration object
61 * @param sslFactory - factory for ssl setup
63 public AaiReactiveWebClientFactory(SslFactory sslFactory, AaiClientConfiguration configuration) {
64 this.aaiUserName = configuration.aaiUserName();
65 this.aaiUserPassword = configuration.aaiUserPassword();
66 this.aaiHeaders = configuration.aaiHeaders();
67 this.trustStorePath = configuration.trustStorePath();
68 this.trustStorePasswordPath = configuration.trustStorePasswordPath();
69 this.keyStorePath = configuration.keyStorePath();
70 this.keyStorePasswordPath = configuration.keyStorePasswordPath();
71 this.enableAaiCertAuth = configuration.enableAaiCertAuth();
72 this.sslFactory = sslFactory;
76 * Construct Reactive WebClient with appropriate settings.
80 public WebClient build() throws SSLException {
81 LOGGER.debug("Setting ssl context");
83 SslContext sslContext = createSslContext();
85 ClientHttpConnector reactorClientHttpConnector = new ReactorClientHttpConnector(
86 HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)));
88 return WebClient.builder()
89 .clientConnector(reactorClientHttpConnector)
90 .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders))
91 .filter(basicAuthentication(aaiUserName, aaiUserPassword))
93 .filter(logResponse())
97 private SslContext createSslContext() throws SSLException {
98 if (enableAaiCertAuth) {
99 return sslFactory.createSecureContext(
101 keyStorePasswordPath,
103 trustStorePasswordPath
106 return sslFactory.createInsecureContext();
109 private ExchangeFilterFunction logRequest() {
110 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
111 MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url()));
112 LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url());
113 clientRequest.headers()
114 .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
115 MDC.remove(SERVICE_NAME);
116 return Mono.just(clientRequest);
120 private ExchangeFilterFunction logResponse() {
121 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
122 MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
123 LOGGER.info("Response Status {}", clientResponse.statusCode());
124 MDC.remove(RESPONSE_CODE);
125 return Mono.just(clientResponse);