|
1 | 1 | /* |
2 | | - * Copyright 2013-2022 the original author or authors. |
| 2 | + * Copyright 2013-2023 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
20 | 20 | import java.security.NoSuchAlgorithmException; |
21 | 21 | import java.security.SecureRandom; |
22 | 22 | import java.security.cert.X509Certificate; |
| 23 | +import java.util.List; |
23 | 24 | import java.util.concurrent.TimeUnit; |
24 | 25 |
|
25 | 26 | import javax.net.ssl.SSLContext; |
|
31 | 32 | import org.apache.commons.logging.LogFactory; |
32 | 33 | import org.apache.hc.client5.http.config.RequestConfig; |
33 | 34 | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 35 | +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; |
34 | 36 | import org.apache.hc.client5.http.impl.classic.HttpClients; |
35 | 37 | import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; |
36 | 38 | import org.apache.hc.client5.http.io.HttpClientConnectionManager; |
|
46 | 48 | import org.apache.hc.core5.util.TimeValue; |
47 | 49 | import org.apache.hc.core5.util.Timeout; |
48 | 50 |
|
| 51 | +import org.springframework.beans.factory.ObjectProvider; |
49 | 52 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
50 | 53 | import org.springframework.cloud.openfeign.support.FeignHttpClientProperties; |
51 | 54 | import org.springframework.context.annotation.Bean; |
|
56 | 59 | * |
57 | 60 | * @author Nguyen Ky Thanh |
58 | 61 | * @author changjin wei(魏昌进) |
| 62 | + * @author Kwangyong Kim |
59 | 63 | */ |
60 | 64 | @Configuration(proxyBeanMethods = false) |
61 | 65 | @ConditionalOnMissingBean(CloseableHttpClient.class) |
@@ -85,18 +89,23 @@ public HttpClientConnectionManager hc5ConnectionManager(FeignHttpClientPropertie |
85 | 89 |
|
86 | 90 | @Bean |
87 | 91 | public CloseableHttpClient httpClient5(HttpClientConnectionManager connectionManager, |
88 | | - FeignHttpClientProperties httpClientProperties) { |
89 | | - httpClient5 = HttpClients.custom().disableCookieManagement().useSystemProperties() |
90 | | - .setConnectionManager(connectionManager).evictExpiredConnections() |
91 | | - .setDefaultRequestConfig(RequestConfig.custom() |
92 | | - .setConnectTimeout( |
93 | | - Timeout.of(httpClientProperties.getConnectionTimeout(), TimeUnit.MILLISECONDS)) |
94 | | - .setRedirectsEnabled(httpClientProperties.isFollowRedirects()) |
95 | | - .setConnectionRequestTimeout( |
96 | | - Timeout.of(httpClientProperties.getHc5().getConnectionRequestTimeout(), |
97 | | - httpClientProperties.getHc5().getConnectionRequestTimeoutUnit())) |
98 | | - .build()) |
99 | | - .build(); |
| 92 | + FeignHttpClientProperties httpClientProperties, |
| 93 | + ObjectProvider<List<HttpClientBuilderCustomizer>> customizerProvider) { |
| 94 | + HttpClientBuilder httpClientBuilder = HttpClients.custom().disableCookieManagement().useSystemProperties() |
| 95 | + .setConnectionManager(connectionManager).evictExpiredConnections() |
| 96 | + .setDefaultRequestConfig(RequestConfig.custom() |
| 97 | + .setConnectTimeout( |
| 98 | + Timeout.of(httpClientProperties.getConnectionTimeout(), TimeUnit.MILLISECONDS)) |
| 99 | + .setRedirectsEnabled(httpClientProperties.isFollowRedirects()) |
| 100 | + .setConnectionRequestTimeout( |
| 101 | + Timeout.of(httpClientProperties.getHc5().getConnectionRequestTimeout(), |
| 102 | + httpClientProperties.getHc5().getConnectionRequestTimeoutUnit())) |
| 103 | + .build()); |
| 104 | + |
| 105 | + customizerProvider.getIfAvailable(List::of) |
| 106 | + .forEach(c -> c.customize(httpClientBuilder)); |
| 107 | + |
| 108 | + httpClient5 = httpClientBuilder.build(); |
100 | 109 | return httpClient5; |
101 | 110 | } |
102 | 111 |
|
@@ -146,4 +155,19 @@ public X509Certificate[] getAcceptedIssuers() { |
146 | 155 |
|
147 | 156 | } |
148 | 157 |
|
| 158 | + /** |
| 159 | + * Callback interface that customize {@link HttpClientBuilder} objects before HttpClient created. |
| 160 | + * |
| 161 | + * @author Kwangyong Kim |
| 162 | + * @since 4.1.0 |
| 163 | + */ |
| 164 | + public interface HttpClientBuilderCustomizer { |
| 165 | + |
| 166 | + /** |
| 167 | + * Customize HttpClientBuilder. |
| 168 | + * @param builder the {@code HttpClientBuilder} to customize |
| 169 | + */ |
| 170 | + void customize(HttpClientBuilder builder); |
| 171 | + |
| 172 | + } |
149 | 173 | } |
0 commit comments