package org.apereo.cas.adaptors.x509.authentication.principal;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableList;
import org.apereo.cas.adaptors.x509.util.CertUtils;
import org.apereo.cas.adaptors.x509.util.X509CertificateCredentialJsonDeserializer;
import org.apereo.cas.adaptors.x509.util.X509CertificateCredentialJsonSerializer;
import org.apereo.cas.authentication.AbstractCredential;

import java.security.cert.X509Certificate;

/**
 * An X.509 certificate credential.
 *
 * @author Scott Battaglia
 * @author Marvin S. Addison
 * @since 3.0.0
 */
@JsonSerialize(using = X509CertificateCredentialJsonSerializer.class)
@JsonDeserialize(using = X509CertificateCredentialJsonDeserializer.class)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
public class X509CertificateCredential extends AbstractCredential {

    /**
     * Unique Id for serialization.
     */
    private static final long serialVersionUID = 631753409512746474L;

    /**
     * The collection of certificates sent with the request.
     */
    private X509Certificate[] certificates;

    /**
     * The certificate that we actually use.
     */
    private X509Certificate certificate;

    /**
     * Instantiates a new x509 certificate credential.
     *
     * @param certificates the certificates
     */
    public X509CertificateCredential(final X509Certificate[] certificates) {
        this.certificates = ImmutableList.copyOf(certificates).toArray(new X509Certificate[certificates.length]);
    }

    public X509Certificate[] getCertificates() {
        return ImmutableList.copyOf(this.certificates).toArray(new X509Certificate[this.certificates.length]);
    }

    public void setCertificate(final X509Certificate certificate) {
        this.certificate = certificate;
    }

    public X509Certificate getCertificate() {
        return this.certificate;
    }

    @Override
    public String getId() {
        X509Certificate cert = null;
        if (this.certificate != null) {
            cert = this.certificate;
        } else if (this.certificates.length > 0) {
            cert = this.certificates[0];
        }

        if (cert != null) {
            return CertUtils.toString(cert);
        }
        return UNKNOWN_ID;
    }
}

@@ -307,56 +307,8 @@
 ze;%0A
-import com.google.common.collect.ImmutableList;%0A
 impo
@@ -621,16 +621,41 @@
 ificate;
+%0Aimport java.util.Arrays;
 %0A%0A/**%0A *
@@ -1678,29 +1678,22 @@
 cates = 
-ImmutableList
+Arrays
 .copyOf(
@@ -1708,38 +1708,10 @@
 ates
-).toArray(new X509Certificate%5B
+, 
 cert
@@ -1717,33 +1717,32 @@
 tificates.length
-%5D
 );%0A    %7D%0A%0A    pu
@@ -1803,21 +1803,14 @@
 urn 
-ImmutableList
+Arrays
 .cop
@@ -1834,38 +1834,10 @@
 ates
-).toArray(new X509Certificate%5B
+, 
 this
@@ -1856,17 +1856,16 @@
 s.length
-%5D
 );%0A    %7D

Use Java method to copy array (#2180)