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.netty.Connection;
34 import reactor.netty.http.client.HttpClient;
35 import reactor.netty.http.client.HttpClientRequest;
36 import reactor.netty.http.client.HttpClientResponse;
38 import javax.net.ssl.SSLException;
39 import java.util.Base64;
41 import java.util.function.BiConsumer;
43 public class AaiHttpClientFactory {
45 private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
47 private final AaiClientConfiguration configuration;
48 private final SslFactory sslFactory;
51 public AaiHttpClientFactory(SslFactory sslFactory, AaiClientConfiguration configuration) {
52 this.configuration = configuration;
53 this.sslFactory = sslFactory;
56 public AaiHttpClient<String> get() throws SSLException {
57 return new AaiHttpGetClient(configuration).createAaiHttpClient(build());
60 public AaiHttpClient<Integer> patch(JsonBodyBuilder jsonBodyBuilder) throws SSLException {
61 return new AaiHttpPatchClient(configuration, jsonBodyBuilder).createAaiHttpClient(build());
64 private HttpClient build() throws SSLException {
65 LOGGER.debug("Setting ssl context");
67 SslContext sslContext = createSslContext();
69 return HttpClient.create()
70 .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
71 .headers(this::settingHeaders)
72 .doOnRequest(logRequest())
73 .doOnResponse(logResponse());
76 private SslContext createSslContext() throws SSLException {
77 if (configuration.enableAaiCertAuth()) {
78 return sslFactory.createSecureContext(
79 configuration.keyStorePath(),
80 configuration.keyStorePasswordPath(),
81 configuration.trustStorePath(),
82 configuration.trustStorePasswordPath()
85 return sslFactory.createInsecureContext();
88 private HttpHeaders settingHeaders(HttpHeaders httpHeaders) {
89 httpHeaders.add("Authorization", "Basic " + performBasicAuthentication());
90 for(Map.Entry<String,String> header : configuration.aaiHeaders().entrySet())
91 httpHeaders.add(header.getKey(), header.getValue());
95 private String performBasicAuthentication() {
96 return Base64.getEncoder().encodeToString(
97 (configuration.aaiUserName() + ":" + configuration.aaiUserPassword()).getBytes()
101 private static BiConsumer<HttpClientRequest, Connection> logRequest() {
102 return (httpClientRequest, connection) -> {
103 LOGGER.info("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri());
104 httpClientRequest.requestHeaders().forEach(stringStringEntry ->
105 LOGGER.info("{}={}", stringStringEntry.getKey(), stringStringEntry.getValue())
110 private static BiConsumer<? super HttpClientResponse, ? super Connection> logResponse() {
111 return (httpClientResponse, connection) ->
112 LOGGER.info("ResponseStatus {}", httpClientResponse.status().code());