1818
1919import java .net .URI ;
2020import java .net .URISyntaxException ;
21+ import java .time .Duration ;
2122import java .util .Map ;
22- import java .util .concurrent .TimeUnit ;
2323
2424import org .apache .hc .client5 .http .HttpRoute ;
2525import org .apache .hc .client5 .http .auth .AuthScope ;
3131import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
3232import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
3333import org .apache .hc .core5 .http .HttpHost ;
34-
34+ import org . apache . hc . core5 . util . Timeout ;
3535import org .springframework .beans .factory .FactoryBean ;
3636
3737/**
38- * {@code FactoryBean} to set up a <a href="http://hc.apache.org/httpcomponents-client">Apache
39- * CloseableHttpClient</a>
38+ * {@link FactoryBean} to set up a {@link CloseableHttpClient} using HttpComponents HttpClient 5.
4039 *
40+ * @see http://hc.apache.org/httpcomponents-client
4141 * @author Lars Uffmann
4242 * @since 4.0.5
4343 */
4444public class HttpComponents5ClientFactory implements FactoryBean <CloseableHttpClient > {
45- private static final int DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS = (60 * 1000 );
4645
47- private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS ;
48- private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000 );
46+ private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration .ofSeconds (60 );
47+
48+ private static final Duration DEFAULT_READ_TIMEOUT = Duration .ofSeconds (60 );
49+
50+ private Duration connectionTimeout = DEFAULT_CONNECTION_TIMEOUT ;
4951
50- private int readTimeout = DEFAULT_READ_TIMEOUT_MILLISECONDS ;
52+ private Duration readTimeout = DEFAULT_READ_TIMEOUT ;
5153
5254 private int maxTotalConnections = -1 ;
55+
5356 private AuthScope authScope = null ;
5457
5558 private Credentials credentials = null ;
@@ -86,24 +89,28 @@ public void setAuthScope(AuthScope authScope) {
8689 /**
8790 * Sets the timeout until a connection is established. A value of 0 means <em>never</em> timeout.
8891 *
89- * @param timeout the timeout value in milliseconds
92+ * @param timeout the timeout value
9093 */
91- public void setConnectionTimeout (int timeout ) {
92- if (timeout < 0 ) {
94+ public void setConnectionTimeout (Duration timeout ) {
95+
96+ if (timeout .isNegative ()) {
9397 throw new IllegalArgumentException ("timeout must be a non-negative value" );
9498 }
99+
95100 this .connectionTimeout = timeout ;
96101 }
97102
98103 /**
99104 * Set the socket read timeout for the underlying HttpClient. A value of 0 means <em>never</em> timeout.
100105 *
101- * @param timeout the timeout value in milliseconds
106+ * @param timeout the timeout value
102107 */
103- public void setReadTimeout (int timeout ) {
104- if (timeout < 0 ) {
108+ public void setReadTimeout (Duration timeout ) {
109+
110+ if (timeout .isNegative ()) {
105111 throw new IllegalArgumentException ("timeout must be a non-negative value" );
106112 }
113+
107114 this .readTimeout = timeout ;
108115 }
109116
@@ -114,9 +121,11 @@ public void setReadTimeout(int timeout) {
114121 * @see PoolingHttpClientConnectionManager...
115122 */
116123 public void setMaxTotalConnections (int maxTotalConnections ) {
124+
117125 if (maxTotalConnections <= 0 ) {
118126 throw new IllegalArgumentException ("maxTotalConnections must be a positive value" );
119127 }
128+
120129 this .maxTotalConnections = maxTotalConnections ;
121130 }
122131
@@ -142,14 +151,14 @@ public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost)
142151 void applyMaxConnectionsPerHost (PoolingHttpClientConnectionManager connectionManager ) throws URISyntaxException {
143152
144153 for (Map .Entry <String , String > entry : maxConnectionsPerHost .entrySet ()) {
154+
145155 URI uri = new URI (entry .getKey ());
146156 HttpHost host = new HttpHost (uri .getScheme (), uri .getHost (), getPort (uri ));
147157 final HttpRoute route ;
148158
149159 if (uri .getScheme ().equals ("https" )) {
150160 route = new HttpRoute (host , null , true );
151- }
152- else {
161+ } else {
153162 route = new HttpRoute (host );
154163 }
155164 int max = Integer .parseInt (entry .getValue ());
@@ -158,14 +167,17 @@ void applyMaxConnectionsPerHost(PoolingHttpClientConnectionManager connectionMan
158167 }
159168
160169 static int getPort (URI uri ) {
170+
161171 if (uri .getPort () == -1 ) {
172+
162173 if ("https" .equalsIgnoreCase (uri .getScheme ())) {
163174 return 443 ;
164175 }
165176 if ("http" .equalsIgnoreCase (uri .getScheme ())) {
166177 return 80 ;
167178 }
168179 }
180+
169181 return uri .getPort ();
170182 }
171183
@@ -176,34 +188,38 @@ public boolean isSingleton() {
176188
177189 @ Override
178190 public CloseableHttpClient getObject () throws Exception {
179- PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder .create ();
191+
192+ PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder
193+ .create ();
194+
180195 if (this .maxTotalConnections != -1 ) {
181196 connectionManagerBuilder .setMaxConnTotal (this .maxTotalConnections );
182197 }
183198
184- if (null != this . connectionManagerBuilderCustomizer ) {
199+ if (this . connectionManagerBuilderCustomizer != null ) {
185200 this .connectionManagerBuilderCustomizer .customize (connectionManagerBuilder );
186201 }
187202
188203 this .connectionManager = connectionManagerBuilder .build ();
189204
190205 applyMaxConnectionsPerHost (connectionManager );
191206
192- RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ()
193- .setConnectTimeout ( connectionTimeout , TimeUnit . MILLISECONDS )
194- .setResponseTimeout (readTimeout , TimeUnit . MILLISECONDS );
207+ RequestConfig .Builder requestConfigBuilder = RequestConfig .custom () //
208+ .setConnectionRequestTimeout ( Timeout . of ( connectionTimeout )) //
209+ .setResponseTimeout (Timeout . of ( readTimeout ) );
195210
196- HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ()
197- .setDefaultRequestConfig (requestConfigBuilder .build ())
211+ HttpClientBuilder httpClientBuilder = HttpClientBuilder .create () //
212+ .setDefaultRequestConfig (requestConfigBuilder .build ()) //
198213 .setConnectionManager (connectionManager );
199214
200- if (null != credentials && null != authScope ) {
215+ if (credentials != null && authScope != null ) {
216+
201217 BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider ();
202218 basicCredentialsProvider .setCredentials (authScope , credentials );
203219 httpClientBuilder .setDefaultCredentialsProvider (basicCredentialsProvider );
204220 }
205221
206- if (null != this . clientBuilderCustomizer ) {
222+ if (this . clientBuilderCustomizer != null ) {
207223 clientBuilderCustomizer .customize (httpClientBuilder );
208224 }
209225
@@ -223,7 +239,8 @@ public void setClientBuilderCustomizer(HttpClientBuilderCustomizer clientBuilder
223239 this .clientBuilderCustomizer = clientBuilderCustomizer ;
224240 }
225241
226- public void setConnectionManagerBuilderCustomizer (PoolingHttpClientConnectionManagerBuilderCustomizer connectionManagerBuilderCustomizer ) {
242+ public void setConnectionManagerBuilderCustomizer (
243+ PoolingHttpClientConnectionManagerBuilderCustomizer connectionManagerBuilderCustomizer ) {
227244 this .connectionManagerBuilderCustomizer = connectionManagerBuilderCustomizer ;
228245 }
229246
0 commit comments