Java Utililty Methods X500Principal

List of utility methods to do X500Principal

Description

The list of methods to do X500Principal are organized into topic(s).

Method

StringconvX509Name(X500Principal principal)
conv X Name
String sName = principal.getName(X509_NAME_RFC);
return sName;
SubjectcreateSubject(GSSName principals, GSSCredential credentials)
Use this method to convert a GSSName and GSSCredential into a Subject.
return sun.security.jgss.GSSUtil.getSubject(principals, credentials);
StringextractName(final X500Principal dname)
extract Name
return extractName(dname.getName());
StringextractRDN(String rdn, X500Principal dn)
extract RDN
String cn = null;
Matcher m = Pattern.compile("(" + rdn + "=[^,]+)").matcher(dn.getName());
if (m.find())
    cn = m.group(1);
return cn == null ? "" : cn;
StringgetDNField(String fieldID, X500Principal principal)
Return the commonName of the past in X.500 principal.
StringTokenizer st = new StringTokenizer(principal.toString(), "=,");
while (st.hasMoreTokens()) {
    String tok = st.nextToken().trim();
    if (tok.equalsIgnoreCase(fieldID)) {
        return st.nextToken().trim();
return null;
...
booleanisTGSPrincipal(KerberosPrincipal principal)
TGS must have the server principal of the form "krbtgt/FOO@FOO".
if (principal == null)
    return false;
if (principal.getName().equals("krbtgt/" + principal.getRealm() + "@" + principal.getRealm())) {
    return true;
return false;
booleanisTicketGrantingServerPrincipal(KerberosPrincipal principal)
TGS must have the server principal of the form "krbtgt/FOO@FOO".
if (principal == null) {
    return false;
if (principal.getName().equals("krbtgt/" + principal.getRealm() + "@" + principal.getRealm())) {
    return true;
return false;
LoginContextserverLogin(final String serverPrincipal, final String serverPassword)
Create server side Kerberos login context for provided credentials.
LoginContext serverLoginContext = new LoginContext("spnego-server", new CallbackHandler() {
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
                final NameCallback nameCallback = (NameCallback) callback;
                nameCallback.setName(serverPrincipal);
            } else if (callback instanceof PasswordCallback) {
                final PasswordCallback passCallback = (PasswordCallback) callback;
...
StringtoGlobusID(X500Principal principal)
Converts DN of the form "CN=A, OU=B, O=C" into Globus format "/O=C/OU=B/CN=A"
This function might return incorrect Globus-formatted ID when one of the RDNs in the DN contains commas.
if (principal == null) {
    return null;
String dn = principal.getName();
StringTokenizer tokens = new StringTokenizer(dn, ",");
StringBuffer buf = new StringBuffer();
String token;
while (tokens.hasMoreTokens()) {
...
X500PrincipaltoPrincipal(String globusID)
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.
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;
...