Example usage for org.apache.commons.ssl Certificates getCN

List of usage examples for org.apache.commons.ssl Certificates getCN

Introduction

In this page you can find the example usage for org.apache.commons.ssl Certificates getCN.

Prototype

public static String getCN(X509Certificate cert) 

Source Link

Usage

From source file:org.fedoraproject.eclipse.packager.FedoraSSL.java

/**
 * Determine FAS username from fedora cert file.
 * //from w  w  w  . j  av  a 2s  . com
 * @return Username if retrieval is successful.
 *         {@link FedoraSSL#UNKNOWN_USER} otherwise.
 */
public String getUsernameFromCert() {
    if (fedoraCert.exists()) {
        KeyMaterial kmat;
        try {
            kmat = new KeyMaterial(fedoraCert, fedoraCert, new char[0]);
            List<?> chains = kmat.getAssociatedCertificateChains();
            Iterator<?> it = chains.iterator();
            ArrayList<String> cns = new ArrayList<String>();
            while (it.hasNext()) {
                X509Certificate[] certs = (X509Certificate[]) it.next();
                if (certs != null) {
                    for (int i = 0; i < certs.length; i++) {
                        cns.add(Certificates.getCN(certs[i]));
                    }
                }
            }
            return cns.get(0);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return UNKNOWN_USER;
}