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
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=========================================================
21 package org.onap.cps.ncmp.api.impl.config;
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;
46 @EnableConfigurationProperties(HttpClientConfiguration.class)
47 @RequiredArgsConstructor(access = AccessLevel.PROTECTED)
48 public class NcmpConfiguration {
53 * @param restTemplateBuilder the rest template builder
54 * @param httpClientConfiguration the http client configuration
55 * @return rest template instance
58 @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
59 public static RestTemplate restTemplate(final RestTemplateBuilder restTemplateBuilder,
60 final HttpClientConfiguration httpClientConfiguration) {
62 final ConnectionConfig connectionConfig = ConnectionConfig.copy(ConnectionConfig.DEFAULT)
63 .setConnectTimeout(Timeout.of(httpClientConfiguration.getConnectionTimeoutInSeconds()))
66 final PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
67 .setDefaultConnectionConfig(connectionConfig)
68 .setMaxConnTotal(httpClientConfiguration.getMaximumConnectionsTotal())
69 .setMaxConnPerRoute(httpClientConfiguration.getMaximumConnectionsPerRoute())
72 final CloseableHttpClient httpClient = HttpClients.custom()
73 .setConnectionManager(connectionManager)
74 .evictExpiredConnections()
75 .evictIdleConnections(
76 TimeValue.of(httpClientConfiguration.getIdleConnectionEvictionThresholdInSeconds()))
79 final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
81 final RestTemplate restTemplate = restTemplateBuilder
82 .requestFactory(() -> requestFactory)
83 .setConnectTimeout(httpClientConfiguration.getConnectionTimeoutInSeconds())
86 setRestTemplateMessageConverters(restTemplate);
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);