2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
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.sdk.rest.services.dmaap.client.service.producer;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.security.GeneralSecurityException;
31 import java.security.KeyStore;
32 import javax.net.ssl.SSLContext;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
35 import org.apache.http.impl.client.HttpClients;
36 import org.apache.http.ssl.SSLContextBuilder;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
38 import org.springframework.boot.web.client.RestTemplateBuilder;
39 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
40 import org.springframework.web.client.RestTemplate;
41 import reactor.core.publisher.Mono;
43 public class DmaaPRestTemplateFactory {
46 * Function for creating RestTemplate object.
48 * @param publisherConfiguration - DMaaP publisher configuration object
49 * @return RestTemplate with correct ssl configuration
51 public Mono<RestTemplate> build(DmaapPublisherConfiguration publisherConfiguration) {
52 if (publisherConfiguration.enableDmaapCertAuth()) {
53 return createRestTemplateWithSslSetup(publisherConfiguration);
56 return Mono.just(new RestTemplate());
59 private Mono<RestTemplate> createRestTemplateWithSslSetup(DmaapPublisherConfiguration publisherConfiguration) {
61 RestTemplateBuilder builder = new RestTemplateBuilder();
63 SSLContext sslContext = createSslContext(publisherConfiguration,
64 loadPasswordFromFile(publisherConfiguration.keyStorePasswordPath()),
65 loadPasswordFromFile(publisherConfiguration.trustStorePasswordPath()));
67 return Mono.just(builder
68 .requestFactory(() -> createRequestFactory(sslContext)).build());
70 } catch (GeneralSecurityException | IOException e) {
75 private SSLContext createSslContext(DmaapPublisherConfiguration publisherConfiguration,
76 String keyStorePassword, String trustStorePassword)
77 throws IOException, GeneralSecurityException {
78 return new SSLContextBuilder()
80 keyStore(publisherConfiguration.keyStorePath(), keyStorePassword),
81 keyStorePassword.toCharArray())
83 getFile(publisherConfiguration.trustStorePath()), trustStorePassword.toCharArray())
87 private HttpComponentsClientHttpRequestFactory createRequestFactory(SSLContext sslContext) {
88 SSLConnectionSocketFactory socketFactory =
89 new SSLConnectionSocketFactory(sslContext);
90 HttpClient httpClient = HttpClients.custom()
91 .setSSLSocketFactory(socketFactory).build();
93 return new HttpComponentsClientHttpRequestFactory(httpClient);
96 private KeyStore keyStore(String keyStoreFile, String keyStorePassword)
97 throws GeneralSecurityException, IOException {
98 KeyStore ks = KeyStore.getInstance("jks");
99 ks.load(getResource(keyStoreFile), keyStorePassword.toCharArray());
103 private File getFile(String fileName) {
104 return new File(fileName);
107 private InputStream getResource(String fileName) throws FileNotFoundException {
108 return new FileInputStream(fileName);
111 private String loadPasswordFromFile(String path) throws IOException {
112 return new String(Files.readAllBytes(Paths.get(path)));