17
17
import org .apache .http .HeaderElement ;
18
18
import org .apache .http .HeaderElementIterator ;
19
19
import org .apache .http .HttpResponse ;
20
+ import org .apache .http .client .HttpRequestRetryHandler ;
20
21
import org .apache .http .config .Registry ;
21
22
import org .apache .http .config .RegistryBuilder ;
22
23
import org .apache .http .conn .ConnectionKeepAliveStrategy ;
28
29
import org .apache .http .protocol .HTTP ;
29
30
import org .apache .http .protocol .HttpContext ;
30
31
import org .apache .http .util .TextUtils ;
32
+ import org .cloudfoundry .identity .uaa .impl .config .RestTemplateConfig ;
31
33
import org .slf4j .Logger ;
32
34
import org .slf4j .LoggerFactory ;
33
35
import org .apache .http .conn .ssl .NoopHostnameVerifier ;
45
47
import javax .net .ssl .HostnameVerifier ;
46
48
import javax .net .ssl .SSLContext ;
47
49
import javax .servlet .http .HttpServletRequest ;
50
+ import java .io .IOException ;
48
51
import java .net .URLEncoder ;
49
52
import java .nio .charset .StandardCharsets ;
50
53
import java .security .KeyManagementException ;
@@ -63,23 +66,27 @@ public abstract class UaaHttpRequestUtils {
63
66
private static final Logger logger = LoggerFactory .getLogger (UaaHttpRequestUtils .class );
64
67
65
68
public static ClientHttpRequestFactory createRequestFactory (boolean skipSslValidation , int timeout ) {
66
- return createRequestFactory (getClientBuilder (skipSslValidation , 10 , 5 , 0 ) , timeout );
69
+ return createRequestFactory (getClientBuilder (skipSslValidation , 10 , 5 , 0 , 2000 , 0 ), timeout , timeout );
67
70
}
68
71
69
- public static ClientHttpRequestFactory createRequestFactory (boolean skipSslValidation , int timeout , int poolSize , int defaultMaxPerRoute , int maxKeepAlive ) {
70
- return createRequestFactory (getClientBuilder (skipSslValidation , poolSize , defaultMaxPerRoute , maxKeepAlive ), timeout );
72
+ public static ClientHttpRequestFactory createRequestFactory (boolean skipSslValidation , int timeout , int readTimeout , int poolSize , int defaultMaxPerRoute , int maxKeepAlive , int validateAfterInactivity , int retryCount ) {
73
+ return createRequestFactory (getClientBuilder (skipSslValidation , poolSize , defaultMaxPerRoute , maxKeepAlive , validateAfterInactivity , retryCount ), timeout , readTimeout );
71
74
}
72
75
73
- protected static ClientHttpRequestFactory createRequestFactory (HttpClientBuilder builder , int timeoutInMs ) {
76
+ public static ClientHttpRequestFactory createRequestFactory (boolean skipSslValidation , int timeout , int readTimeout , RestTemplateConfig restTemplateConfig ) {
77
+ return createRequestFactory (getClientBuilder (skipSslValidation , restTemplateConfig .maxTotal , restTemplateConfig .maxPerRoute , restTemplateConfig .maxKeepAlive , restTemplateConfig .validateAfterInactivity , restTemplateConfig .retryCount ), timeout , readTimeout );
78
+ }
79
+
80
+ protected static ClientHttpRequestFactory createRequestFactory (HttpClientBuilder builder , int timeoutInMs , int readTimeoutInMs ) {
74
81
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory (builder .build ());
75
82
76
- httpComponentsClientHttpRequestFactory .setReadTimeout (timeoutInMs );
83
+ httpComponentsClientHttpRequestFactory .setReadTimeout (readTimeoutInMs );
77
84
httpComponentsClientHttpRequestFactory .setConnectionRequestTimeout (timeoutInMs );
78
85
httpComponentsClientHttpRequestFactory .setConnectTimeout (timeoutInMs );
79
86
return httpComponentsClientHttpRequestFactory ;
80
87
}
81
88
82
- protected static HttpClientBuilder getClientBuilder (boolean skipSslValidation , int poolSize , int defaultMaxPerRoute , int maxKeepAlive ) {
89
+ protected static HttpClientBuilder getClientBuilder (boolean skipSslValidation , int poolSize , int defaultMaxPerRoute , int maxKeepAlive , int validateAfterInactivity , int retryCount ) {
83
90
HttpClientBuilder builder = HttpClients .custom ()
84
91
.useSystemProperties ()
85
92
.setRedirectStrategy (new DefaultRedirectStrategy ());
@@ -100,6 +107,7 @@ protected static HttpClientBuilder getClientBuilder(boolean skipSslValidation, i
100
107
}
101
108
cm .setMaxTotal (poolSize );
102
109
cm .setDefaultMaxPerRoute (defaultMaxPerRoute );
110
+ cm .setValidateAfterInactivity (validateAfterInactivity );
103
111
builder .setConnectionManager (cm );
104
112
105
113
if (maxKeepAlive <= 0 ) {
@@ -108,6 +116,10 @@ protected static HttpClientBuilder getClientBuilder(boolean skipSslValidation, i
108
116
builder .setKeepAliveStrategy (new UaaConnectionKeepAliveStrategy (maxKeepAlive ));
109
117
}
110
118
119
+ if (retryCount > 0 ) {
120
+ builder .setRetryHandler (new UaaHttpRequestRetryHandler (retryCount ));
121
+ }
122
+
111
123
return builder ;
112
124
}
113
125
@@ -178,6 +190,26 @@ public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpCont
178
190
}
179
191
}
180
192
193
+ private static class UaaHttpRequestRetryHandler implements HttpRequestRetryHandler {
194
+
195
+ private final int executionCount ;
196
+
197
+ public UaaHttpRequestRetryHandler (int executionCount ) {
198
+ this .executionCount = executionCount ;
199
+ }
200
+
201
+ @ Override
202
+ public boolean retryRequest (IOException exception , int executionCount , HttpContext context ) {
203
+ if (executionCount > this .executionCount ) {
204
+ return false ;
205
+ }
206
+ if (exception instanceof org .apache .http .NoHttpResponseException ) {
207
+ return true ;
208
+ }
209
+ return false ;
210
+ }
211
+ }
212
+
181
213
@ SuppressWarnings ("java:S1168" )
182
214
private static String [] split (final String s ) {
183
215
if (TextUtils .isBlank (s )) {
0 commit comments