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;
26 import lombok.RequiredArgsConstructor;
27 import org.apache.hc.client5.http.config.ConnectionConfig;
28 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
29 import org.apache.hc.client5.http.impl.classic.HttpClients;
30 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
31 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
32 import org.apache.hc.core5.util.TimeValue;
33 import org.apache.hc.core5.util.Timeout;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
36 import org.springframework.boot.context.properties.EnableConfigurationProperties;
37 import org.springframework.boot.web.client.RestTemplateBuilder;
38 import org.springframework.context.annotation.Bean;
39 import org.springframework.context.annotation.Configuration;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.client.ClientHttpRequestFactory;
43 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
44 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
45 import org.springframework.stereotype.Component;
46 import org.springframework.web.client.RestTemplate;
49 @EnableConfigurationProperties(HttpClientConfiguration.class)
50 @RequiredArgsConstructor(access = AccessLevel.PROTECTED)
51 public class NcmpConfiguration {
55 public static class DmiProperties {
56 @Value("${ncmp.dmi.auth.username}")
57 private String authUsername;
58 @Value("${ncmp.dmi.auth.password}")
59 private String authPassword;
60 @Value("${ncmp.dmi.api.base-path}")
61 private String dmiBasePath;
62 @Value("${ncmp.dmi.auth.enabled}")
63 private boolean dmiBasicAuthEnabled;
69 * @param restTemplateBuilder the rest template builder
70 * @param httpClientConfiguration the http client configuration
71 * @return rest template instance
74 @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
75 public static RestTemplate restTemplate(final RestTemplateBuilder restTemplateBuilder,
76 final HttpClientConfiguration httpClientConfiguration) {
78 final ConnectionConfig connectionConfig = ConnectionConfig.copy(ConnectionConfig.DEFAULT)
79 .setConnectTimeout(Timeout.of(httpClientConfiguration.getConnectionTimeoutInSeconds()))
82 final PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
83 .setDefaultConnectionConfig(connectionConfig)
84 .setMaxConnTotal(httpClientConfiguration.getMaximumConnectionsTotal())
85 .setMaxConnPerRoute(httpClientConfiguration.getMaximumConnectionsPerRoute())
88 final CloseableHttpClient httpClient = HttpClients.custom()
89 .setConnectionManager(connectionManager)
90 .evictExpiredConnections()
91 .evictIdleConnections(
92 TimeValue.of(httpClientConfiguration.getIdleConnectionEvictionThresholdInSeconds()))
95 final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
97 final RestTemplate restTemplate = restTemplateBuilder
98 .requestFactory(() -> requestFactory)
99 .setConnectTimeout(httpClientConfiguration.getConnectionTimeoutInSeconds())
102 setRestTemplateMessageConverters(restTemplate);
106 private static void setRestTemplateMessageConverters(final RestTemplate restTemplate) {
107 final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter =
108 new MappingJackson2HttpMessageConverter();
109 mappingJackson2HttpMessageConverter.setSupportedMediaTypes(
110 Arrays.asList(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN));
111 restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);