// Path: src/main/java/com/soasta/jenkins/ProxyChecker.java
// public class ProxyChecker
// {
//   public static boolean useProxy(String host, ProxyConfiguration proxyConfig)
//   {
//     // Check if the proxy applies for this destination host.
//     // This code is more or less copied from ProxyConfiguration.createProxy() :-(.
//     if (proxyConfig != null && proxyConfig.name != null) 
//     {
//       for (Pattern p : proxyConfig.getNoProxyHostPatterns()) 
//       {   
//           if (p.matcher(host).matches()) 
//           {
//               // It's a match.
//               // Don't use the proxy.
//               return false;
//           }
//       }
//       // we have checked, and the proxy host pattern doesn't match our whitelist, So use the proxy.
//       return true;
//     }
//     else
//     {
//       // jenkins is not configured to use a proxy.
//       return false;
//     }
//   }
// }


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import org.apache.http.util.EntityUtils;
import com.soasta.jenkins.ProxyChecker;
import hudson.ProxyConfiguration;
import jenkins.model.Jenkins;
        new X509TrustManager() 
        {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() 
          {
            return new X509Certificate[] {};
          }
          public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) 
          {
          }
          public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
          {
          }
        }
      }; 
  }
  
  public static SSLConnectionSocketFactory getSSLFactory(KeyManager[] keyManager, TrustManager[] trustManagers) throws Exception
  {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    
    sslContext.init(keyManager, trustManagers, new java.security.SecureRandom());
    SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    
    return sslConnectionFactory;
  }
  
  private CredentialsProvider getProxyCreds(ProxyConfiguration proxyInfo, String host)
  {
    CredentialsProvider credentialsProvider = null;
    // is the host on the no proxy list? 
// Path: src/main/java/com/soasta/jenkins/ProxyChecker.java
// public class ProxyChecker
// {
//   public static boolean useProxy(String host, ProxyConfiguration proxyConfig)
//   {
//     // Check if the proxy applies for this destination host.
//     // This code is more or less copied from ProxyConfiguration.createProxy() :-(.
//     if (proxyConfig != null && proxyConfig.name != null) 
//     {
//       for (Pattern p : proxyConfig.getNoProxyHostPatterns()) 
//       {   
//           if (p.matcher(host).matches()) 
//           {
//               // It's a match.
//               // Don't use the proxy.
//               return false;
//           }
//       }
//       // we have checked, and the proxy host pattern doesn't match our whitelist, So use the proxy.
//       return true;
//     }
//     else
//     {
//       // jenkins is not configured to use a proxy.
//       return false;
//     }
//   }
// }


// Path: src/main/java/com/soasta/jenkins/httpclient/GenericSelfClosingHttpClient.java
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import org.apache.http.util.EntityUtils;
import com.soasta.jenkins.ProxyChecker;
import hudson.ProxyConfiguration;
import jenkins.model.Jenkins;

        new X509TrustManager() 
        {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() 
          {
            return new X509Certificate[] {};
          }
          public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) 
          {
          }
          public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
          {
          }
        }
      }; 
  }
  
  public static SSLConnectionSocketFactory getSSLFactory(KeyManager[] keyManager, TrustManager[] trustManagers) throws Exception
  {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    
    sslContext.init(keyManager, trustManagers, new java.security.SecureRandom());
    SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    
    return sslConnectionFactory;
  }
  
  private CredentialsProvider getProxyCreds(ProxyConfiguration proxyInfo, String host)
  {
    CredentialsProvider credentialsProvider = null;
    // is the host on the no proxy list? 
    if (proxyInfo != null && proxyInfo.name != null && ProxyChecker.useProxy(host, proxyInfo))