package com.stm.asset.http;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

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 class TrustAllSSLSocketFactory {

	public static SSLSocketFactory createSSLSocketFactory() {
		SSLSocketFactory sslFactory = null;
		try {
			SSLContext sslContext = SSLContext.getInstance("SSL");
			sslContext.init(null, new TrustManager[] { new TrustAllManager() }, new SecureRandom());
			sslFactory = sslContext.getSocketFactory();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sslFactory;
	}

	public static HostnameVerifier getHostnameVerifier() {
		return new TrustAllHostname();
	}

	private static class TrustAllHostname implements HostnameVerifier {

		@Override
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	}

	public static class TrustAllManager implements X509TrustManager {

		@Override
		public void checkClientTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {

		}

		@Override
		public void checkServerTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {

		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[]{};
		}
	}

}
