A browser with JavaScript enabled is required for this page to operate properly.
Trail: Java Naming and Directory Interface(TM).
Lesson: Advanced Topics for LDAP Users
More LDAP Operations
Home Page > Java Naming and Directory Interface(TM). > Advanced Topics for LDAP Users

More LDAP Operations

The rest of the LDAP lesson covers how the JNDI provides ability to perform certain interesting LDAP operations.

Renaming Objects

You use Context.rename() to rename an object in the directory. In the LDAP v2, this corresponds to the "modify RDN" operation that renames an entry within the same context (that is, renaming a sibling). In the LDAP v3, this corresponds to the "modify DN" operation, which is like "modify RDN," except that the old and new entries need not be in the same context. You can use Context.rename() to rename a leaf entry or an interior node. The example shown in the Naming and Directory Operations lesson renames a leaf entry. The following code renames an interior node from "ou=NewHires" to "ou=OldHires":
ctx.rename("ou=NewHires", "ou=OldHires");


Note: The Sun Java Directory Server v5.2 does not support renaming interior nodes. If you run this example, then you will get a ContextNotEmptyException.

Renaming to a Different Part of the DIT

With the LDAP v3, you can rename an entry to a different part of the DIT. To do this by using Context.rename(), you must use a context that is the common ancestor for both the new and the old entries. For example, to rename "cn=C. User, ou=NewHires, o=JNDITutorial" to "cn=C. User, ou=People, o=JNDITutorial", you must use the context named by "o=JNDITutorial". Following is an example that demonstrates this. If you try to run this example against an LDAP v2 server, then you will get an InvalidNameException because version 2 does not support this feature.

ctx.rename("cn=C. User, ou=NewHires", "cn=C. User, ou=People");

Note: The Sun Java Directory Server v5.2 does not support renaming with different parent nodes. If you run this example by using that server, then you will get a OperationNotSupportedException (indicating a "protocol error").

Keeping the Old Name Attributes

In the LDAP, when you rename an entry, you have the option of keeping the entry's old RDN as an attribute of the updated entry. For example, if you rename the entry "cn=C. User" to "cn=Claude User", you can specify whether you want the old RDN "cn=C. User" to be kept as an attribute.

To specify whether you want to keep the old name attribute when you use Context.rename(), use the "java.naming.ldap.deleteRDN" environment property. If this property's value is "true" (the default), the old RDN is removed. If its value is "false", then the old RDN is kept as an attribute of the updated entry. The complete example is here.

// Set the property to keep RDN
env.put("java.naming.ldap.deleteRDN", "false");

// Create the initial context
DirContext ctx = new InitialDirContext(env);

// Perform the rename
ctx.rename("cn=C. User, ou=NewHires", "cn=Claude User,ou=NewHires");

Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.

Previous page: SSL and Custom Sockets
Next page: LDAP Compare