2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.sdk.rest.services.aai.client.service;
23 import io.netty.handler.codec.http.HttpHeaders;
24 import io.netty.handler.ssl.SslContext;
25 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
26 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.AaiHttpClient;
27 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.get.AaiHttpGetClient;
28 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.patch.AaiHttpPatchClient;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
30 import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Mono;
34 import reactor.netty.Connection;
35 import reactor.netty.http.client.HttpClient;
36 import reactor.netty.http.client.HttpClientRequest;
37 import reactor.netty.http.client.HttpClientResponse;
39 import javax.net.ssl.SSLException;
40 import java.util.Base64;
42 import java.util.function.BiConsumer;
44 public class AaiHttpClientFactory {
46 private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
48 private final AaiClientConfiguration configuration;
49 private final SslFactory sslFactory;
52 public AaiHttpClientFactory(SslFactory sslFactory, AaiClientConfiguration configuration) {
53 this.configuration = configuration;
54 this.sslFactory = sslFactory;
57 public AaiHttpClient<String> get() throws SSLException {
58 return new AaiHttpGetClient(configuration).createAaiHttpClient(build());
61 public AaiHttpClient<Integer> patch(JsonBodyBuilder jsonBodyBuilder) throws SSLException {
62 return new AaiHttpPatchClient(configuration, jsonBodyBuilder).createAaiHttpClient(build());
65 private HttpClient build() throws SSLException {
66 LOGGER.debug("Setting ssl context");
68 SslContext sslContext = createSslContext();
70 return HttpClient.create()
71 .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
72 .headers(this::settingHeaders)
73 .doOnRequest(logRequest())
74 .doOnResponse(logResponse());
77 private SslContext createSslContext() throws SSLException {
78 if (configuration.enableAaiCertAuth()) {
79 return sslFactory.createSecureContext(
80 configuration.keyStorePath(),
81 configuration.keyStorePasswordPath(),
82 configuration.trustStorePath(),
83 configuration.trustStorePasswordPath()
86 return sslFactory.createInsecureContext();
89 private HttpHeaders settingHeaders(HttpHeaders httpHeaders) {
90 httpHeaders.add("Authorization", "Basic " + performBasicAuthentication());
91 for(Map.Entry<String,String> header : configuration.aaiHeaders().entrySet())
92 httpHeaders.add(header.getKey(), header.getValue());
96 private String performBasicAuthentication() {
97 return Base64.getEncoder().encodeToString(
98 (configuration.aaiUserName() + ":" + configuration.aaiUserPassword()).getBytes()
102 private static BiConsumer<HttpClientRequest, Connection> logRequest() {
103 return (httpClientRequest, connection) -> {
104 LOGGER.info("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri());
105 httpClientRequest.requestHeaders().forEach(stringStringEntry ->
106 LOGGER.info("{}={}", stringStringEntry.getKey(), stringStringEntry.getValue())
111 private static BiConsumer<? super HttpClientResponse, ? super Connection> logResponse() {
112 return (httpClientResponse, connection) ->
113 LOGGER.info("ResponseStatus {}", httpClientResponse.status().code());