The class javax.naming.ldap.Rdn represents a Relative Distinguished name (RDN) as specified in RFC2253. An RDN represents a component of a DN as explained in the Manipulating LdapName lesson. An RDN is made up of type and value pair(s). Examples of RDNs are:
The Rdn class is immutable.
Rdn rdn1 = new Rdn("ou= Juicy\\, Fruit"); System.out.println("rdn1:" + rdn1.toString()); Rdn rdn2 = new Rdn(rdn1); System.out.println("rdn2:" + rdn2.toString()); Attributes attrs = new BasicAttributes(); attrs.put("ou", "Juicy, Fruit"); attrs.put("cn", "Mango"); Rdn rdn3 = new Rdn(attrs); System.out.println("rdn3:" + rdn3.toString()); Rdn rdn4 = new Rdn("ou", "Juicy, Fruit"); System.out.println("rdn4:" + rdn4.toString());
getType()
getValue()
toAttributes()
For a an RDN made up of single type/value pair, the getType()method returns the type and the getValue() method returns the value of the RDN. The method toAttributes() returns the attribute view of the type/value pairs. The example below prints the type/value pairs of RDNs.
Attributes attrs = new BasicAttributes(); attrs.put("o", "Yellow"); attrs.put("cn", "Mango"); // create a binary value for the RDN byte[] mangoJuice = new byte[6]; for (int i = 0; i < mangoJuice.length; i++) { mangoJuice[i] = (byte) i; } attrs.put("ou", mangoJuice); Rdn rdn = new Rdn(attrs); System.out.println(); System.out.println("size:" + rdn.size()); System.out.println("getType(): " + rdn.getType()); System.out.println("getValue(): " + rdn.getValue()); // test toAttributes System.out.println(); System.out.println("toAttributes(): " + rdn.toAttributes());
When you use the Rdn constructor that takes a String argument, you supply the string representation of an RDN, and get back an Rdn instance. To do the reverse, that is, to get the string representation of a Rdn instance, you use toString(). The result of toString() can be fed back to the Rdn constructor to produce an Rdn instance that is equal to the original Rdn instance.
Here's an example:
Rdn rdn = new Rdn("cn=Juicy\\,Fruit"); String str = rdn.toString(); System.out.println(str); Rdn rdn2 = new Rdn(str); System.out.println(rdn.equals(rdn2)); // true
equals(Object Rdn)
compareTo(Object Rdn)
You can use compareTo() to sort a list of Rdn instances. equals() lets you determine whether two Rdns are syntactically equal. Two Rdns are equal if they both have the same (case-exact matched) type/value pairs. The order of components in a multi-valued RDNs is not significant.
Here's an example:
Rdn one = new Rdn("ou=Sales+cn=Bob"); Rdn two = new Rdn("cn=Bob+ou=Sales"); Rdn three = new Rdn("ou=Sales+cn=Bob+c=US"); Rdn four = new Rdn("cn=lowercase"); Rdn five = new Rdn("cn=LowerCASE"); System.out.println(one.equals(two)); // true System.out.println(two.equals(three)); // false System.out.println(one.equals(three)); // false System.out.println(four.equals(five)); // true
For example, consider an RDN:
cn=Juicy, Fruit
The character , (comma) appearing between Juicy and Fruit is a special character that
needs to be escaped by a '\' (blackslash). The resulting syntactically
formatted RDN looks as below:
cn=Juicy\, Fruit
However, the '\' (backslash) character itself is a special character according
to the Java Language String syntax, and it needs to escaped with a '\' (backslash) again.
The Java Language String format and RFC2253 both use '\' (backslash) to escape
the special characters.
And therefore the Java formatted RDN String looks as below:
cn=Juicy\\, Fruit
Note that, the above mentioned formatting rules apply only to the value component of an Rdn.
The Rdn class provides two static methods to handle
automatic escaping and unescaping of the RDN values:
The example below shows how to get the string representation of a DN without having to deal with the syntax for handling special characters defined in RFC2253.
// DN with ',' (comma) String unformatted = "Juicy, Fruit"; String formatted = Rdn.escapeValue(unformatted); LdapName dn = new LdapName("cn=" + formatted); System.out.println("dn:" + dn); unformatted = "true+false"; formatted = Rdn.escapeValue(unformatted); dn = new LdapName("cn=" + formatted); System.out.println("dn:" + dn); // DN with a binary value as one one of its attribute value byte[] bytes = new byte[] {1, 2, 3, 4}; formatted = Rdn.escapeValue(bytes); System.out.println("Orig val: " + bytes + " Escaped val: " + formatted);
// DN with ',' (comma) String unformatted = "Juicy, Fruit"; String formatted = Rdn.escapeValue(unformatted); System.out.println("Formatted:" + formatted); Object original = Rdn.unescapeValue(formatted); System.out.println("Original:" + original); // DN with a '+' (plus) unformatted = "true+false"; formatted = Rdn.escapeValue(unformatted); System.out.println("Formatted:" + formatted); original = Rdn.unescapeValue(formatted); System.out.println("Original:" + original); // DN with a binary value as one one of its attribute value byte[] bytes = new byte[] {1, 2, 3, 4}; formatted = Rdn.escapeValue(bytes); System.out.println("Formatted:" + formatted); original = Rdn.unescapeValue(formatted); System.out.println("Original:" + original);