Example usage for javax.security.auth.x500 X500Principal getName

List of usage examples for javax.security.auth.x500 X500Principal getName

Introduction

In this page you can find the example usage for javax.security.auth.x500 X500Principal getName.

Prototype

public String getName(String format, Map<String, String> oidMap) 

Source Link

Document

Returns a string representation of the X.500 distinguished name using the specified format.

Usage

From source file:org.globus.gsi.util.CertificateUtil.java

/**
 * Converts DN of the form "CN=A, OU=B, O=C" into Globus format
 * "/O=C/OU=B/CN=A" <BR> This function might return incorrect
 * Globus-formatted ID when one of the RDNs in the DN contains commas.
 *
 * @return the converted DN in Globus format.
 *//*  w  w w .  ja v a2s .c o m*/
public static String toGlobusID(X500Principal principal) {

    if (principal == null) {
        return null;
    }

    String dn = principal.getName(X500Principal.RFC2253, OID_MAP);

    StringBuilder buf = new StringBuilder();

    final int IDLE = 0;
    final int VALUE = 1;
    final int KEY = 2;

    int state = IDLE;

    int cEnd = 0;
    char[] asChars = dn.toCharArray();
    /*
     * walk in reverse order and merge RDN
     */
    for (int i = asChars.length - 1; i >= 0; i--) {

        char c = asChars[i];
        switch (state) {
        case KEY:
            if (c == ',') {
                String s = dn.substring(i + 1, cEnd + 1);
                buf.append('/').append(s);
                state = IDLE;
            }
            break;
        case VALUE:
            if (c == '=') {
                state = KEY;
            }
            break;
        case IDLE:
        default:
            cEnd = i;
            state = VALUE;
        }
    }

    String s = dn.substring(0, cEnd + 1);
    buf.append('/').append(s);

    // remove comma escaping as some other components may use string comparison.
    return buf.toString().replace("\\,", ",");
}

From source file:mitm.common.security.ca.handlers.ejbca.EJBCACertificateRequestHandler.java

private String subjectDNToString(X500Principal subject) {
    return subject.getName(X500Principal.RFC1779, oidMapping);
}