3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2024 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.config;
24 import io.netty.channel.ChannelOption;
25 import io.netty.handler.timeout.ReadTimeoutHandler;
26 import io.netty.handler.timeout.WriteTimeoutHandler;
27 import java.util.concurrent.TimeUnit;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.context.annotation.Bean;
33 import org.springframework.context.annotation.Configuration;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
37 import org.springframework.stereotype.Component;
38 import org.springframework.web.reactive.function.client.WebClient;
39 import reactor.netty.http.client.HttpClient;
43 @RequiredArgsConstructor
44 public class DmiWebClientConfiguration {
46 @Value("${ncmp.dmi.httpclient.connectionTimeoutInSeconds:20000}")
47 private Integer connectionTimeoutInSeconds;
49 @Value("${ncmp.dmi.httpclient.maximumInMemorySizeInMegabytes:1}")
50 private Integer maximumInMemorySizeInMegabytes;
54 public static class DmiProperties {
55 @Value("${ncmp.dmi.auth.username}")
56 private String authUsername;
57 @Value("${ncmp.dmi.auth.password}")
58 private String authPassword;
59 @Value("${ncmp.dmi.api.base-path}")
60 private String dmiBasePath;
61 @Value("${ncmp.dmi.auth.enabled}")
62 private boolean dmiBasicAuthEnabled;
66 * Configures and create a WebClient bean.
68 * @return a WebClient instance.
71 public WebClient webClient() {
72 final var httpClient = HttpClient.create()
73 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutInSeconds * 1000)
74 .doOnConnected(connection ->
76 .addHandlerLast(new ReadTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS))
77 .addHandlerLast(new WriteTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS)));
79 return WebClient.builder()
80 .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
81 .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
82 .clientConnector(new ReactorClientHttpConnector(httpClient))
83 .codecs(configurer -> configurer
85 .maxInMemorySize(maximumInMemorySizeInMegabytes * 1024 * 1024))