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

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

Introduction

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

Prototype

public X500Principal(String name, Map<String, String> keywordMap) 

Source Link

Document

Creates an X500Principal from a string representation of an X.500 distinguished name (ex: "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US").

Usage

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

/**
 * Converts Globus DN format "/O=C/OU=B/CN=A" into an X500Principal
 * representation, which accepts RFC 2253 or 1779 formatted DN's and also
 * attribute types as defined in RFC 2459 (e.g. "CN=A,OU=B,O=C"). This
 * method should allow the forward slash, "/", to occur in attribute values
 * (see GFD.125 section 3.2.2 -- RFC 2252 allows "/" in PrintableStrings).
 * @param globusID DN in Globus format// w  w w .  j  av  a2  s . c  o  m
 * @return the X500Principal representation of the given DN
 */
public static X500Principal toPrincipal(String globusID) {

    if (globusID == null) {
        return null;
    }
    String id = globusID.trim();
    StringBuilder buf = new StringBuilder(id.length());

    if (!id.isEmpty()) {

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

        int state = IDLE;

        int cEnd = 0;
        char[] asChars = id.toCharArray();

        /*
         * walk in reverse order and split into RDN
         */
        for (int i = asChars.length - 1; i >= 0; i--) {

            char c = asChars[i];
            switch (state) {
            case KEY:
                if (c == '/' || c == ' ') {
                    /*
                      handle names with comma according rfc1779
                     */
                    String s = id.substring(i + 1, cEnd + 1);
                    int commaIndex = s.indexOf(',');
                    if (commaIndex != -1) {
                        s = s.substring(0, commaIndex) + "\\" + s.substring(commaIndex);
                    }
                    buf.append(s).append(',');
                    state = IDLE;
                }
                break;
            case VALUE:
                if (c == '=') {
                    state = KEY;
                }
                break;
            case IDLE:
            default:
                // idle
                if (c == '/' || c == ' ') {
                    continue;
                } else {
                    cEnd = i;
                    state = VALUE;
                }
            }
        }

        // delete last extra comma
        buf.deleteCharAt(buf.length() - 1);
    }

    String dn = buf.toString();

    return new X500Principal(dn, KEYWORD_MAP);
}