@@ -207,7 +207,7 @@ public void setUpGlobal() throws Exception {
207
207
@ Test (groups = { "standalone" , "default_provider" })
208
208
public void zeroCopyPostTest () throws Throwable {
209
209
210
- final AsyncHttpClient client = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext ()).build ());
210
+ final AsyncHttpClient client = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext (new AtomicBoolean ( true ) )).build ());
211
211
try {
212
212
ClassLoader cl = getClass ().getClassLoader ();
213
213
// override system properties
@@ -226,7 +226,7 @@ public void zeroCopyPostTest() throws Throwable {
226
226
227
227
@ Test (groups = { "standalone" , "default_provider" })
228
228
public void multipleSSLRequestsTest () throws Throwable {
229
- final AsyncHttpClient c = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext ()).build ());
229
+ final AsyncHttpClient c = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext (new AtomicBoolean ( true ) )).build ());
230
230
try {
231
231
String body = "hello there" ;
232
232
@@ -246,7 +246,7 @@ public void multipleSSLRequestsTest() throws Throwable {
246
246
247
247
@ Test (groups = { "standalone" , "default_provider" })
248
248
public void multipleSSLWithoutCacheTest () throws Throwable {
249
- final AsyncHttpClient c = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext ()).setAllowSslConnectionPool (false ).build ());
249
+ final AsyncHttpClient c = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext (new AtomicBoolean ( true ) )).setAllowSslConnectionPool (false ).build ());
250
250
try {
251
251
String body = "hello there" ;
252
252
c .preparePost (getTargetUrl ()).setBody (body ).setHeader ("Content-Type" , "text/html" ).execute ();
@@ -263,40 +263,36 @@ public void multipleSSLWithoutCacheTest() throws Throwable {
263
263
264
264
@ Test (groups = { "standalone" , "default_provider" })
265
265
public void reconnectsAfterFailedCertificationPath () throws Throwable {
266
- final AsyncHttpClient c = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext ()).build ());
266
+ AtomicBoolean trusted = new AtomicBoolean (false );
267
+ final AsyncHttpClient c = getAsyncHttpClient (new Builder ().setSSLContext (createSSLContext (trusted )).build ());
267
268
try {
268
269
final String body = "hello there" ;
269
270
270
- TRUST_SERVER_CERT . set ( false );
271
+ // first request fails because server certificate is rejected
271
272
try {
272
- // first request fails because server certificate is rejected
273
- try {
274
- c .preparePost (getTargetUrl ()).setBody (body ).setHeader ("Content-Type" , "text/html" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
275
- } catch (final ExecutionException e ) {
276
- Throwable cause = e .getCause ();
277
- if (cause instanceof ConnectException ) {
278
- assertNotNull (cause .getCause ());
279
- assertTrue (cause .getCause () instanceof SSLHandshakeException );
280
- } else {
281
- assertTrue (cause instanceof SSLHandshakeException );
282
- }
273
+ c .preparePost (getTargetUrl ()).setBody (body ).setHeader ("Content-Type" , "text/html" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
274
+ } catch (final ExecutionException e ) {
275
+ Throwable cause = e .getCause ();
276
+ if (cause instanceof ConnectException ) {
277
+ assertNotNull (cause .getCause ());
278
+ assertTrue (cause .getCause () instanceof SSLHandshakeException );
279
+ } else {
280
+ assertTrue (cause instanceof SSLHandshakeException );
283
281
}
282
+ }
284
283
285
- TRUST_SERVER_CERT .set (true );
284
+ trusted .set (true );
286
285
287
- // second request should succeed
288
- final Response response = c .preparePost (getTargetUrl ()).setBody (body ).setHeader ("Content-Type" , "text/html" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
286
+ // second request should succeed
287
+ final Response response = c .preparePost (getTargetUrl ()).setBody (body ).setHeader ("Content-Type" , "text/html" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
289
288
290
- assertEquals (response .getResponseBody (), body );
291
- } finally {
292
- TRUST_SERVER_CERT .set (true );
293
- }
289
+ assertEquals (response .getResponseBody (), body );
294
290
} finally {
295
291
c .close ();
296
292
}
297
293
}
298
294
299
- private static SSLContext createSSLContext () {
295
+ private static SSLContext createSSLContext (AtomicBoolean trusted ) {
300
296
try {
301
297
InputStream keyStoreStream = BasicHttpsTest .class .getResourceAsStream ("ssltest-cacerts.jks" );
302
298
char [] keyStorePassword = "changeit" .toCharArray ();
@@ -310,7 +306,7 @@ private static SSLContext createSSLContext() {
310
306
311
307
// Initialize the SSLContext to work with our key managers.
312
308
KeyManager [] keyManagers = kmf .getKeyManagers ();
313
- TrustManager [] trustManagers = new TrustManager [] { DUMMY_TRUST_MANAGER };
309
+ TrustManager [] trustManagers = new TrustManager [] { dummyTrustManager ( trusted ) };
314
310
SecureRandom secureRandom = new SecureRandom ();
315
311
316
312
SSLContext sslContext = SSLContext .getInstance ("TLS" );
@@ -322,20 +318,21 @@ private static SSLContext createSSLContext() {
322
318
}
323
319
}
324
320
325
- private static final AtomicBoolean TRUST_SERVER_CERT = new AtomicBoolean (true );
326
- private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager () {
327
- public X509Certificate [] getAcceptedIssuers () {
328
- return new X509Certificate [0 ];
329
- }
330
-
331
- public void checkClientTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
332
- }
333
-
334
- public void checkServerTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
335
- if (!TRUST_SERVER_CERT .get ()) {
336
- throw new CertificateException ("Server certificate not trusted." );
337
- }
338
- }
339
- };
321
+ private static final TrustManager dummyTrustManager (final AtomicBoolean trusted ) {
322
+ return new X509TrustManager () {
323
+ public X509Certificate [] getAcceptedIssuers () {
324
+ return new X509Certificate [0 ];
325
+ }
326
+
327
+ public void checkClientTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
328
+ }
329
+
330
+ public void checkServerTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
331
+ if (!trusted .get ()) {
332
+ throw new CertificateException ("Server certificate not trusted." );
333
+ }
334
+ }
335
+ };
336
+ }
340
337
341
338
}
0 commit comments