X.500 DNs

An X.500 distiguished name (DN) is just a series of key/value pairs providing a unique name for a single entity. Such an entity is often called a principal. Various string keys are standard. Among others: O is organisation, OU is organisational unit, C is country, CN is canonical name.

X.500 DNs are much used in Jtrix, so we have a class org.jtrix.base.X500DN to help us.


// Make a name using org.jtrix.base.Attribute.  Attribute order is not
// important in an X500DN. For Attribute keys, case is not important,
// but it is for Attribute values.

Attribute a1 = new Attribute("o", "jtrix.org");
Attribute a2 = new Attribute("uid", "jim");
Attribute a3 = new Attribute("cn", "Jim Chapman");

// Create a new X.500 DN, and turn it into a String

X500DN jim = new X500DN(new Attribute[] {a1, a2, a3});
String s1 = jim.toString();

// Now s1 is the following String:
//     O=jtrix.org,UID=jim,CN="Jim Chapman"
//
// Demonstrate conversion from a String.  Spaces, commas and semicolons
// act as Attribute separators, so quote marks needed around Attribute values
// which include these (though there's no harm using them otherwise).

String s2 = "cn=\"Jim Chapman\",o=jtrix.org,uid=\"jim\"";
X500DN jim2 = X500DN.parsePrincipal(s2);
boolean test = jim.equals(jim2);                      // test is true

Nik Silver 2002-03-09