package com.github.msemys.esjc.ssl;

import static com.github.msemys.esjc.util.Preconditions.checkArgument;
import static com.github.msemys.esjc.util.Strings.isNullOrEmpty;

/**
 * SSL settings
 */
public class SslSettings {

    /**
     * Whether or not the connection is encrypted using SSL.
     */
    public final boolean useSslConnection;

    /**
     * The Common Name (CN) identifies the fully qualified domain name associated with the certificate.
     */
    public final String certificateCommonName;

    /**
     * Whether or not to validate the server SSL certificate.
     */
    public final boolean validateServerCertificate;

    private SslSettings(boolean useSslConnection, String certificateCommonName, boolean validateServerCertificate) {
        this.useSslConnection = useSslConnection;
        this.certificateCommonName = certificateCommonName;
        this.validateServerCertificate = validateServerCertificate;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("SslSettings{");
        sb.append("useSslConnection=").append(useSslConnection);
        sb.append(", certificateCommonName='").append(certificateCommonName).append('\'');
        sb.append(", validateServerCertificate=").append(validateServerCertificate);
        sb.append('}');
        return sb.toString();
    }

    /**
     * Creates a new SSL settings that enables connection encryption using SSL and
     * trusts an X.509 server certificate whose Common Name (CN) matches.
     *
     * @param certificateCommonName server certificate common name (CN)
     * @return SSL settings
     */
    public static SslSettings trustCertificateCN(String certificateCommonName) {
        checkArgument(!isNullOrEmpty(certificateCommonName), "certificateCommonName is null or empty");
        return new SslSettings(true, certificateCommonName, true);
    }

    /**
     * Creates a new SSL settings that enables connection encryption using SSL and
     * trusts all X.509 server certificates without any verification.
     *
     * @return SSL settings
     */
    public static SslSettings trustAllCertificates() {
        return new SslSettings(true, null, false);
    }

    /**
     * Creates a new SSL settings that do not use connection encryption.
     *
     * @return SSL settings
     */
    public static SslSettings noSsl() {
        return new SslSettings(false, null, false);
    }

}

@@ -217,16 +217,197 @@
 ttings %7B
+%0A    private static final SslSettings TRUST_ALL_CERTIFICATES = new SslSettings(true, null, false);%0A    private static final SslSettings NO_SSL = new SslSettings(false, null, false);
 %0A%0A    /*
@@ -2354,42 +2354,30 @@
 urn 
-new SslSettings(true, null, false)
+TRUST_ALL_CERTIFICATES
 ;%0A  
@@ -2564,43 +2564,14 @@
 urn 
-new SslSettings(false, null, false)
+NO_SSL
 ;%0A  

use predefined constants for 'noSsl()' and 'trustAllCertificates()' settings