32852b3265d56f0ed7b0015c0de5eaf8b3734cc9
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / config / NcmpConfiguration.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.config;
22
23 import java.util.Arrays;
24 import lombok.AccessLevel;
25 import lombok.RequiredArgsConstructor;
26 import org.apache.hc.client5.http.config.ConnectionConfig;
27 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
28 import org.apache.hc.client5.http.impl.classic.HttpClients;
29 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
30 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
31 import org.apache.hc.core5.util.TimeValue;
32 import org.apache.hc.core5.util.Timeout;
33 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
34 import org.springframework.boot.context.properties.EnableConfigurationProperties;
35 import org.springframework.boot.web.client.RestTemplateBuilder;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.client.ClientHttpRequestFactory;
41 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
42 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
43 import org.springframework.web.client.RestTemplate;
44
45 @Configuration
46 @EnableConfigurationProperties(HttpClientConfiguration.class)
47 @RequiredArgsConstructor(access = AccessLevel.PROTECTED)
48 public class NcmpConfiguration {
49
50     /**
51      * Rest template bean.
52      *
53      * @param restTemplateBuilder the rest template builder
54      * @param httpClientConfiguration the http client configuration
55      * @return rest template instance
56      */
57     @Bean
58     @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
59     public static RestTemplate restTemplate(final RestTemplateBuilder restTemplateBuilder,
60                                             final HttpClientConfiguration httpClientConfiguration) {
61
62         final ConnectionConfig connectionConfig = ConnectionConfig.copy(ConnectionConfig.DEFAULT)
63                 .setConnectTimeout(Timeout.of(httpClientConfiguration.getConnectionTimeoutInSeconds()))
64                 .build();
65
66         final PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
67                 .setDefaultConnectionConfig(connectionConfig)
68                 .setMaxConnTotal(httpClientConfiguration.getMaximumConnectionsTotal())
69                 .setMaxConnPerRoute(httpClientConfiguration.getMaximumConnectionsPerRoute())
70                 .build();
71
72         final CloseableHttpClient httpClient = HttpClients.custom()
73                 .setConnectionManager(connectionManager)
74                 .evictExpiredConnections()
75                 .evictIdleConnections(
76                         TimeValue.of(httpClientConfiguration.getIdleConnectionEvictionThresholdInSeconds()))
77                 .build();
78
79         final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
80
81         final RestTemplate restTemplate = restTemplateBuilder
82                 .requestFactory(() -> requestFactory)
83                 .setConnectTimeout(httpClientConfiguration.getConnectionTimeoutInSeconds())
84                 .build();
85
86         setRestTemplateMessageConverters(restTemplate);
87         return restTemplate;
88     }
89
90     private static void setRestTemplateMessageConverters(final RestTemplate restTemplate) {
91         final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter =
92             new MappingJackson2HttpMessageConverter();
93         mappingJackson2HttpMessageConverter.setSupportedMediaTypes(
94             Arrays.asList(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN));
95         restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
96     }
97
98 }