d3047561049a87c450ad1378a75882997a7842aa
[dcaegen2/services/sdk.git] /
1 /*
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
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.aai.client.service;
22
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;
37
38 import javax.net.ssl.SSLException;
39 import java.util.Base64;
40 import java.util.Map;
41 import java.util.function.BiConsumer;
42
43 public class AaiHttpClientFactory {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
46
47     private final AaiClientConfiguration configuration;
48     private final SslFactory sslFactory;
49
50
51     public AaiHttpClientFactory(SslFactory sslFactory, AaiClientConfiguration configuration) {
52         this.configuration = configuration;
53         this.sslFactory = sslFactory;
54     }
55
56     public AaiHttpClient<String> get() throws SSLException {
57         return new AaiHttpGetClient(configuration).createAaiHttpClient(build());
58     }
59
60     public AaiHttpClient<Integer> patch(JsonBodyBuilder jsonBodyBuilder) throws SSLException {
61         return new AaiHttpPatchClient(configuration, jsonBodyBuilder).createAaiHttpClient(build());
62     }
63
64     private HttpClient build() throws SSLException {
65         LOGGER.debug("Setting ssl context");
66
67         SslContext sslContext = createSslContext();
68
69         return HttpClient.create()
70                 .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
71                 .headers(this::settingHeaders)
72                 .doOnRequest(logRequest())
73                 .doOnResponse(logResponse());
74     }
75
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()
83             );
84         }
85         return sslFactory.createInsecureContext();
86     }
87
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());
92         return httpHeaders;
93     }
94
95     private String performBasicAuthentication() {
96         return Base64.getEncoder().encodeToString(
97                 (configuration.aaiUserName() + ":" + configuration.aaiUserPassword()).getBytes()
98         );
99     }
100
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())
106             );
107         };
108     }
109
110     private static BiConsumer<? super HttpClientResponse, ? super Connection>  logResponse() {
111         return (httpClientResponse, connection) ->
112                 LOGGER.info("ResponseStatus {}", httpClientResponse.status().code());
113     }
114 }