package be.iminds.ilabt.jfed.lowlevel.connection;

import javax.net.ssl.SSLException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: twalcari
 * Date: 12/9/2016
 * Time: 15:40
 */
public class HostnameAndCertificateMismatchSSLException extends SSLException {
    private final String host;
    private final String cn;
    private final List<String> subjectAlts;
    private final List<String> allowedCertificateHostnameAliases;

    private static String makeMessage(String host, String cn, List<String>  subjectAlts, List<String> allowedCertificateHostnameAliases, Throwable origThrowable) {
        String newMessage = origThrowable == null ? "" : origThrowable.getMessage();
        newMessage += " (details:";
        newMessage += " host='" + host + "'";
        newMessage += " cn='" + cn + "'";
        newMessage += " subjectAlts='" + subjectAlts +"'";
        newMessage += " allowedCertificateHostnameAliases='" + allowedCertificateHostnameAliases + "'";
        newMessage += ")";
        return newMessage;
    }

    public HostnameAndCertificateMismatchSSLException(String host, String cn, List<String> subjectAlts,
                                                      List<String> allowedCertificateHostnameAliases,
                                                      Throwable throwable) {
        super(makeMessage(host, cn, subjectAlts, allowedCertificateHostnameAliases, throwable), throwable);

        this.host = host;
        this.cn = cn;
        this.subjectAlts = subjectAlts;
        this.allowedCertificateHostnameAliases = allowedCertificateHostnameAliases == null ? null
                : new ArrayList<>(allowedCertificateHostnameAliases);
    }

    public String getHost() {
        return host;
    }

    public String getCn() {
        return cn;
    }

    public List<String> getSubjectAlts() {
        return subjectAlts;
    }

    public List<String> getAllowedCertificateHostnameAliases() {
        return allowedCertificateHostnameAliases;
    }
}
