2626import java .util .function .Function ;
2727
2828import org .apache .hc .client5 .http .classic .HttpClient ;
29+ import org .apache .hc .client5 .http .config .RequestConfig ;
2930import org .apache .hc .client5 .http .impl .DefaultRedirectStrategy ;
3031import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
3132import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
@@ -64,23 +65,33 @@ public final class HttpComponentsClientHttpRequestFactoryBuilder
6465
6566 private final Consumer <SocketConfig .Builder > socketConfigCustomizer ;
6667
68+ private final Consumer <RequestConfig .Builder > defaultRequestConfigManagerCustomizer ;
69+
6770 private final Function <SslBundle , TlsSocketStrategy > tlsSocketStrategyFactory ;
6871
6972 HttpComponentsClientHttpRequestFactoryBuilder () {
70- this (Collections .emptyList (), emptyCustomizer (), emptyCustomizer (), emptyCustomizer (),
73+ this (Collections .emptyList (), emptyCustomizer (), emptyCustomizer (), emptyCustomizer (), emptyCustomizer (),
7174 HttpComponentsClientHttpRequestFactoryBuilder ::createTlsSocketStrategy );
7275 }
7376
77+ private static TlsSocketStrategy createTlsSocketStrategy (SslBundle sslBundle ) {
78+ SslOptions options = sslBundle .getOptions ();
79+ return new DefaultClientTlsStrategy (sslBundle .createSslContext (), options .getEnabledProtocols (),
80+ options .getCiphers (), null , new DefaultHostnameVerifier ());
81+ }
82+
7483 private HttpComponentsClientHttpRequestFactoryBuilder (
7584 List <Consumer <HttpComponentsClientHttpRequestFactory >> customizers ,
7685 Consumer <HttpClientBuilder > httpClientCustomizer ,
7786 Consumer <PoolingHttpClientConnectionManagerBuilder > connectionManagerCustomizer ,
7887 Consumer <SocketConfig .Builder > socketConfigCustomizer ,
88+ Consumer <RequestConfig .Builder > defaultRequestConfigManagerCustomizer ,
7989 Function <SslBundle , TlsSocketStrategy > tlsSocketStrategyFactory ) {
8090 super (customizers );
8191 this .httpClientCustomizer = httpClientCustomizer ;
8292 this .connectionManagerCustomizer = connectionManagerCustomizer ;
8393 this .socketConfigCustomizer = socketConfigCustomizer ;
94+ this .defaultRequestConfigManagerCustomizer = defaultRequestConfigManagerCustomizer ;
8495 this .tlsSocketStrategyFactory = tlsSocketStrategyFactory ;
8596 }
8697
@@ -89,15 +100,15 @@ public HttpComponentsClientHttpRequestFactoryBuilder withCustomizer(
89100 Consumer <HttpComponentsClientHttpRequestFactory > customizer ) {
90101 return new HttpComponentsClientHttpRequestFactoryBuilder (mergedCustomizers (customizer ),
91102 this .httpClientCustomizer , this .connectionManagerCustomizer , this .socketConfigCustomizer ,
92- this .tlsSocketStrategyFactory );
103+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
93104 }
94105
95106 @ Override
96107 public HttpComponentsClientHttpRequestFactoryBuilder withCustomizers (
97108 Collection <Consumer <HttpComponentsClientHttpRequestFactory >> customizers ) {
98109 return new HttpComponentsClientHttpRequestFactoryBuilder (mergedCustomizers (customizers ),
99110 this .httpClientCustomizer , this .connectionManagerCustomizer , this .socketConfigCustomizer ,
100- this .tlsSocketStrategyFactory );
111+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
101112 }
102113
103114 /**
@@ -111,7 +122,7 @@ public HttpComponentsClientHttpRequestFactoryBuilder withHttpClientCustomizer(
111122 Assert .notNull (httpClientCustomizer , "'httpClientCustomizer' must not be null" );
112123 return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (),
113124 this .httpClientCustomizer .andThen (httpClientCustomizer ), this .connectionManagerCustomizer ,
114- this .socketConfigCustomizer , this .tlsSocketStrategyFactory );
125+ this .socketConfigCustomizer , this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
115126 }
116127
117128 /**
@@ -126,7 +137,7 @@ public HttpComponentsClientHttpRequestFactoryBuilder withConnectionManagerCustom
126137 Assert .notNull (connectionManagerCustomizer , "'connectionManagerCustomizer' must not be null" );
127138 return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (), this .httpClientCustomizer ,
128139 this .connectionManagerCustomizer .andThen (connectionManagerCustomizer ), this .socketConfigCustomizer ,
129- this .tlsSocketStrategyFactory );
140+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
130141 }
131142
132143 /**
@@ -141,7 +152,7 @@ public HttpComponentsClientHttpRequestFactoryBuilder withSocketConfigCustomizer(
141152 Assert .notNull (socketConfigCustomizer , "'socketConfigCustomizer' must not be null" );
142153 return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (), this .httpClientCustomizer ,
143154 this .connectionManagerCustomizer , this .socketConfigCustomizer .andThen (socketConfigCustomizer ),
144- this .tlsSocketStrategyFactory );
155+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
145156 }
146157
147158 /**
@@ -155,7 +166,25 @@ public HttpComponentsClientHttpRequestFactoryBuilder withTlsSocketStrategyFactor
155166 Function <SslBundle , TlsSocketStrategy > tlsSocketStrategyFactory ) {
156167 Assert .notNull (tlsSocketStrategyFactory , "'tlsSocketStrategyFactory' must not be null" );
157168 return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (), this .httpClientCustomizer ,
158- this .connectionManagerCustomizer , this .socketConfigCustomizer , tlsSocketStrategyFactory );
169+ this .connectionManagerCustomizer , this .socketConfigCustomizer ,
170+ this .defaultRequestConfigManagerCustomizer , tlsSocketStrategyFactory );
171+ }
172+
173+ /**
174+ * Return a new {@link HttpComponentsClientHttpRequestFactoryBuilder} that applies
175+ * additional customization to the underlying
176+ * {@link org.apache.hc.client5.http.config.RequestConfig.Builder} used for default
177+ * requests.
178+ * @param defaultRequestConfigManagerCustomizer the customizer to apply
179+ * @return a new {@link HttpComponentsClientHttpRequestFactoryBuilder} instance
180+ */
181+ public HttpComponentsClientHttpRequestFactoryBuilder withDefaultRequestConfigManagerCustomizer (
182+ Consumer <RequestConfig .Builder > defaultRequestConfigManagerCustomizer ) {
183+ Assert .notNull (defaultRequestConfigManagerCustomizer ,
184+ "'defaultRequestConfigManagerCustomizer' must not be null" );
185+ return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (), this .httpClientCustomizer ,
186+ this .connectionManagerCustomizer , this .socketConfigCustomizer , defaultRequestConfigManagerCustomizer ,
187+ this .tlsSocketStrategyFactory );
159188 }
160189
161190 @ Override
@@ -172,7 +201,8 @@ private HttpClient createHttpClient(ClientHttpRequestFactorySettings settings) {
172201 HttpClientBuilder builder = HttpClientBuilder .create ()
173202 .useSystemProperties ()
174203 .setRedirectStrategy (asRedirectStrategy (settings .redirects ()))
175- .setConnectionManager (createConnectionManager (settings ));
204+ .setConnectionManager (createConnectionManager (settings ))
205+ .setDefaultRequestConfig (createDefaultRequestConfig ());
176206 this .httpClientCustomizer .accept (builder );
177207 return builder .build ();
178208 }
@@ -204,10 +234,10 @@ private SocketConfig createSocketConfig(ClientHttpRequestFactorySettings setting
204234 return builder .build ();
205235 }
206236
207- private static TlsSocketStrategy createTlsSocketStrategy ( SslBundle sslBundle ) {
208- SslOptions options = sslBundle . getOptions ();
209- return new DefaultClientTlsStrategy ( sslBundle . createSslContext (), options . getEnabledProtocols (),
210- options . getCiphers (), null , new DefaultHostnameVerifier () );
237+ private RequestConfig createDefaultRequestConfig ( ) {
238+ RequestConfig . Builder builder = RequestConfig . custom ();
239+ this . defaultRequestConfigManagerCustomizer . accept ( builder );
240+ return builder . build ( );
211241 }
212242
213243 /**
0 commit comments