26
26
import java .util .function .Function ;
27
27
28
28
import org .apache .hc .client5 .http .classic .HttpClient ;
29
+ import org .apache .hc .client5 .http .config .RequestConfig ;
29
30
import org .apache .hc .client5 .http .impl .DefaultRedirectStrategy ;
30
31
import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
31
32
import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
@@ -64,23 +65,33 @@ public final class HttpComponentsClientHttpRequestFactoryBuilder
64
65
65
66
private final Consumer <SocketConfig .Builder > socketConfigCustomizer ;
66
67
68
+ private final Consumer <RequestConfig .Builder > defaultRequestConfigManagerCustomizer ;
69
+
67
70
private final Function <SslBundle , TlsSocketStrategy > tlsSocketStrategyFactory ;
68
71
69
72
HttpComponentsClientHttpRequestFactoryBuilder () {
70
- this (Collections .emptyList (), emptyCustomizer (), emptyCustomizer (), emptyCustomizer (),
73
+ this (Collections .emptyList (), emptyCustomizer (), emptyCustomizer (), emptyCustomizer (), emptyCustomizer (),
71
74
HttpComponentsClientHttpRequestFactoryBuilder ::createTlsSocketStrategy );
72
75
}
73
76
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
+
74
83
private HttpComponentsClientHttpRequestFactoryBuilder (
75
84
List <Consumer <HttpComponentsClientHttpRequestFactory >> customizers ,
76
85
Consumer <HttpClientBuilder > httpClientCustomizer ,
77
86
Consumer <PoolingHttpClientConnectionManagerBuilder > connectionManagerCustomizer ,
78
87
Consumer <SocketConfig .Builder > socketConfigCustomizer ,
88
+ Consumer <RequestConfig .Builder > defaultRequestConfigManagerCustomizer ,
79
89
Function <SslBundle , TlsSocketStrategy > tlsSocketStrategyFactory ) {
80
90
super (customizers );
81
91
this .httpClientCustomizer = httpClientCustomizer ;
82
92
this .connectionManagerCustomizer = connectionManagerCustomizer ;
83
93
this .socketConfigCustomizer = socketConfigCustomizer ;
94
+ this .defaultRequestConfigManagerCustomizer = defaultRequestConfigManagerCustomizer ;
84
95
this .tlsSocketStrategyFactory = tlsSocketStrategyFactory ;
85
96
}
86
97
@@ -89,15 +100,15 @@ public HttpComponentsClientHttpRequestFactoryBuilder withCustomizer(
89
100
Consumer <HttpComponentsClientHttpRequestFactory > customizer ) {
90
101
return new HttpComponentsClientHttpRequestFactoryBuilder (mergedCustomizers (customizer ),
91
102
this .httpClientCustomizer , this .connectionManagerCustomizer , this .socketConfigCustomizer ,
92
- this .tlsSocketStrategyFactory );
103
+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
93
104
}
94
105
95
106
@ Override
96
107
public HttpComponentsClientHttpRequestFactoryBuilder withCustomizers (
97
108
Collection <Consumer <HttpComponentsClientHttpRequestFactory >> customizers ) {
98
109
return new HttpComponentsClientHttpRequestFactoryBuilder (mergedCustomizers (customizers ),
99
110
this .httpClientCustomizer , this .connectionManagerCustomizer , this .socketConfigCustomizer ,
100
- this .tlsSocketStrategyFactory );
111
+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
101
112
}
102
113
103
114
/**
@@ -111,7 +122,7 @@ public HttpComponentsClientHttpRequestFactoryBuilder withHttpClientCustomizer(
111
122
Assert .notNull (httpClientCustomizer , "'httpClientCustomizer' must not be null" );
112
123
return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (),
113
124
this .httpClientCustomizer .andThen (httpClientCustomizer ), this .connectionManagerCustomizer ,
114
- this .socketConfigCustomizer , this .tlsSocketStrategyFactory );
125
+ this .socketConfigCustomizer , this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
115
126
}
116
127
117
128
/**
@@ -126,7 +137,7 @@ public HttpComponentsClientHttpRequestFactoryBuilder withConnectionManagerCustom
126
137
Assert .notNull (connectionManagerCustomizer , "'connectionManagerCustomizer' must not be null" );
127
138
return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (), this .httpClientCustomizer ,
128
139
this .connectionManagerCustomizer .andThen (connectionManagerCustomizer ), this .socketConfigCustomizer ,
129
- this .tlsSocketStrategyFactory );
140
+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
130
141
}
131
142
132
143
/**
@@ -141,7 +152,7 @@ public HttpComponentsClientHttpRequestFactoryBuilder withSocketConfigCustomizer(
141
152
Assert .notNull (socketConfigCustomizer , "'socketConfigCustomizer' must not be null" );
142
153
return new HttpComponentsClientHttpRequestFactoryBuilder (getCustomizers (), this .httpClientCustomizer ,
143
154
this .connectionManagerCustomizer , this .socketConfigCustomizer .andThen (socketConfigCustomizer ),
144
- this .tlsSocketStrategyFactory );
155
+ this .defaultRequestConfigManagerCustomizer , this . tlsSocketStrategyFactory );
145
156
}
146
157
147
158
/**
@@ -155,7 +166,25 @@ public HttpComponentsClientHttpRequestFactoryBuilder withTlsSocketStrategyFactor
155
166
Function <SslBundle , TlsSocketStrategy > tlsSocketStrategyFactory ) {
156
167
Assert .notNull (tlsSocketStrategyFactory , "'tlsSocketStrategyFactory' must not be null" );
157
168
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 );
159
188
}
160
189
161
190
@ Override
@@ -172,7 +201,8 @@ private HttpClient createHttpClient(ClientHttpRequestFactorySettings settings) {
172
201
HttpClientBuilder builder = HttpClientBuilder .create ()
173
202
.useSystemProperties ()
174
203
.setRedirectStrategy (asRedirectStrategy (settings .redirects ()))
175
- .setConnectionManager (createConnectionManager (settings ));
204
+ .setConnectionManager (createConnectionManager (settings ))
205
+ .setDefaultRequestConfig (createDefaultRequestConfig ());
176
206
this .httpClientCustomizer .accept (builder );
177
207
return builder .build ();
178
208
}
@@ -204,10 +234,10 @@ private SocketConfig createSocketConfig(ClientHttpRequestFactorySettings setting
204
234
return builder .build ();
205
235
}
206
236
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 ( );
211
241
}
212
242
213
243
/**
0 commit comments