2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.configration;
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.net.ssl.SSLContext;
25 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.apache.http.impl.client.HttpClients;
28 import org.apache.http.ssl.SSLContextBuilder;
29 import org.onap.so.aaisimulator.utils.CacheName;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
34 import org.springframework.cache.Cache;
35 import org.springframework.cache.CacheManager;
36 import org.springframework.cache.concurrent.ConcurrentMapCache;
37 import org.springframework.cache.support.SimpleCacheManager;
38 import org.springframework.context.annotation.Bean;
39 import org.springframework.context.annotation.Configuration;
40 import org.springframework.context.annotation.Profile;
41 import org.springframework.core.io.Resource;
42 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
43 import org.springframework.web.client.RestTemplate;
44 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
47 * @author waqas.ikram@ericsson.com
51 public class ApplicationConfigration {
52 private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfigration.class);
56 public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
57 return (mapperBuilder) -> mapperBuilder.modulesToInstall(new JaxbAnnotationModule());
61 public CacheManager cacheManager() {
62 final SimpleCacheManager manager = new SimpleCacheManager();
64 final List<Cache> caches = new ArrayList<>();
65 for (final CacheName cacheName : CacheName.values()) {
66 caches.add(getCache(cacheName.getName()));
68 manager.setCaches(caches);
72 private Cache getCache(final String name) {
73 LOGGER.info("Creating cache with name: {}", name);
74 return new ConcurrentMapCache(name);
79 public RestTemplate restTemplate(@Value("${http.client.ssl.trust-store:#{null}}") final Resource trustStore,
80 @Value("${http.client.ssl.trust-store-password:#{null}}") final String trustStorePassword)
82 LOGGER.info("Setting up RestTemplate .... ");
83 final RestTemplate restTemplate = new RestTemplate();
85 final HttpComponentsClientHttpRequestFactory factory =
86 new HttpComponentsClientHttpRequestFactory(httpClient(trustStore, trustStorePassword));
88 restTemplate.setRequestFactory(factory);
92 private CloseableHttpClient httpClient(final Resource trustStore, final String trustStorePassword)
94 LOGGER.info("Creating SSLConnectionSocketFactory with custom SSLContext and HostnameVerifier ... ");
95 return HttpClients.custom().setSSLSocketFactory(getSSLConnectionSocketFactory(trustStore, trustStorePassword))
99 private SSLConnectionSocketFactory getSSLConnectionSocketFactory(final Resource trustStore,
100 final String trustStorePassword) throws Exception {
101 return new SSLConnectionSocketFactory(getSslContext(trustStore, trustStorePassword));
104 private SSLContext getSslContext(final Resource trustStore, final String trustStorePassword)
105 throws Exception, Exception {
106 return new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();