All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.difi.certvalidator.structure.XorJunction Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package no.difi.certvalidator.structure;

import no.difi.certvalidator.api.CertificateValidationException;
import no.difi.certvalidator.api.FailedValidationException;
import no.difi.certvalidator.api.ValidatorRule;

import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

/**
 * Allows combining instances of validators using a limited set of logic.
 */
public class XorJunction extends AbstractJunction {

    public XorJunction(ValidatorRule... validatorRules) {
        super(validatorRules);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void validate(X509Certificate certificate) throws CertificateValidationException {
        List exceptions = new ArrayList();

        for (ValidatorRule validatorRule : validatorRules) {
            try {
                validatorRule.validate(certificate);
            } catch (CertificateValidationException e) {
                exceptions.add(e);
            }
        }

        if (exceptions.size() != validatorRules.size() - 1) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(String.format("Xor-junction failed with results (%s of %s):", exceptions.size(), validatorRules.size()));
            for (Exception e : exceptions)
                stringBuilder.append("\n* ").append(e.getMessage());

            throw new FailedValidationException(stringBuilder.toString());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy