package lu.tudor.santec.gecamed.testing.wikifood.mdb;

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

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;



public class SSLManager {

    static boolean initialized = false;
    
    static {
	initSSL();
    }
    
    
    public static void initSSL() {
	
	if (initialized )
	    return;
	    
	    try {
		SSLContext sslContext = SSLContext.getInstance( "SSL" );

		// set up a TrustManager that trusts everything
		sslContext.init( null, new TrustManager[]
		    {
		        new X509TrustManager()
		        {
		            public X509Certificate[] getAcceptedIssuers()
		            {
		                return null;
		            }
		            public void checkClientTrusted( X509Certificate[] certs, String authType )
		            {
		                // everything is trusted
		            }
		            public void checkServerTrusted( X509Certificate[] certs, String authType )
		            {
		                // everything is trusted
		            }
		        }
		    }, new SecureRandom() );
		HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

		// setup a hostname verifier that verifies everything
		HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier()		
		{
		    public boolean verify( String arg0, SSLSession arg1 )
		    {
		        return true;
		    }
		} );
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    
	    initialized = true;
	    
	}
}
