2020import com .microsoft .rest .retry .RetryHandler ;
2121import com .microsoft .rest .retry .RetryStrategy ;
2222import okhttp3 .Authenticator ;
23+ import okhttp3 .CipherSuite ;
2324import okhttp3 .ConnectionPool ;
25+ import okhttp3 .ConnectionSpec ;
2426import okhttp3 .Dispatcher ;
2527import okhttp3 .Interceptor ;
2628import okhttp3 .JavaNetCookieJar ;
2729import okhttp3 .OkHttpClient ;
30+ import okhttp3 .TlsVersion ;
2831import okio .AsyncTimeout ;
2932import retrofit2 .Retrofit ;
3033import retrofit2 .adapter .rxjava .RxJavaCallAdapterFactory ;
3134
3235import java .net .CookieManager ;
3336import java .net .CookiePolicy ;
3437import java .net .Proxy ;
38+ import java .util .Arrays ;
3539import java .util .concurrent .Executor ;
3640import java .util .concurrent .TimeUnit ;
3741
@@ -181,6 +185,10 @@ public static class Builder {
181185 private boolean useHttpClientThreadPool ;
182186 /** The connection pool in use for OkHttp. */
183187 private ConnectionPool connectionPool ;
188+ /** The tls versions in use for OkHttp. */
189+ private TlsVersion [] tlsVersions ;
190+ /** The cipher suites in use for OkHttp. */
191+ private CipherSuite [] cipherSuites ;
184192
185193 /**
186194 * Creates an instance of the builder with a base URL to the service.
@@ -199,6 +207,8 @@ private Builder(final RestClient restClient) {
199207 this .responseBuilderFactory = restClient .builder .responseBuilderFactory ;
200208 this .serializerAdapter = restClient .builder .serializerAdapter ;
201209 this .useHttpClientThreadPool = restClient .builder .useHttpClientThreadPool ;
210+ this .tlsVersions = restClient .builder .tlsVersions ;
211+ this .cipherSuites = restClient .builder .cipherSuites ;
202212 if (restClient .builder .credentials != null ) {
203213 this .credentials = restClient .builder .credentials ;
204214 }
@@ -479,6 +489,26 @@ public Builder withRetryStrategy(RetryStrategy strategy) {
479489 return this ;
480490 }
481491
492+ /**
493+ * Sets tls versions for OkHttp client.
494+ * @param tlsVersions the tls versions to use
495+ * @return the builder itself for chaining
496+ */
497+ public Builder withTlsVersions (TlsVersion ... tlsVersions ) {
498+ this .tlsVersions = tlsVersions ;
499+ return this ;
500+ }
501+
502+ /**
503+ * Sets cipher suites for OkHttp client.
504+ * @param cipherSuites the cipher suites to use
505+ * @return the builder itself for chaining
506+ */
507+ public Builder withCipherSuites (CipherSuite ... cipherSuites ) {
508+ this .cipherSuites = cipherSuites ;
509+ return this ;
510+ }
511+
482512 /**
483513 * Build a RestClient with all the current configurations.
484514 *
@@ -525,6 +555,17 @@ public RestClient build() {
525555 httpClientBuilder = httpClientBuilder .dispatcher (dispatcher );
526556 }
527557
558+ if (this .tlsVersions != null || this .cipherSuites != null ) {
559+ ConnectionSpec .Builder connectionSpecBuilder = new ConnectionSpec .Builder (ConnectionSpec .MODERN_TLS );
560+ if (this .tlsVersions != null && this .tlsVersions .length > 0 ) {
561+ connectionSpecBuilder .tlsVersions (this .tlsVersions );
562+ }
563+ if (this .cipherSuites != null && this .cipherSuites .length > 0 ) {
564+ connectionSpecBuilder .cipherSuites (this .cipherSuites );
565+ }
566+ this .httpClientBuilder .connectionSpecs (Arrays .asList (connectionSpecBuilder .build (), ConnectionSpec .CLEARTEXT ));
567+ }
568+
528569 OkHttpClient httpClient = httpClientBuilder
529570 .addInterceptor (userAgentInterceptor )
530571 .addInterceptor (customHeadersInterceptor )
0 commit comments