All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.payu.sdk.api.http.OkHttpClientHelper Maven / Gradle / Ivy

The newest version!
package com.payu.sdk.api.http;

import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.OkHttpClient;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public final class OkHttpClientHelper {

  /**
   * The connection timeout in ms.
   */
  private static final int CONNECTION_TIMEOUT = 10000;
  private static final String SSL_CONTEXT = "TLS";

  public static OkHttpClient getCustomOkHttpClient() {
    try {
      final TrustManager[] trustAllCerts = new TrustManager[] {
          new X509TrustManager() {
            @Override public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
                String authType) throws CertificateException {
            }

            @Override public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
                String authType) throws CertificateException {
            }

            @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() {
              return null;
            }
          }
      };
      final SSLContext sslContext = SSLContext.getInstance(SSL_CONTEXT);
      sslContext.init(null, trustAllCerts, null);
      final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
      OkHttpClient okHttpClient = new OkHttpClient();
      okHttpClient.setSslSocketFactory(sslSocketFactory);
      okHttpClient.setHostnameVerifier(new HostnameVerifier() {
        @Override public boolean verify(String hostname, SSLSession session) {
          return true;
        }
      });
      okHttpClient.setConnectionSpecs(
          Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT));
      okHttpClient.setConnectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
      return okHttpClient;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy