Modifying Directory Entries

Modifying the attribute values of a directory entry involves using the modifyAttributes( ) method of DirContext. One variant of this method takes the name of a directory entry, a modification type, and an Attributes object that contains modified Attribute objects; another variant takes a name and an array of objects. A ModificationItem encapsulates a modified Attribute object and a modification type.

The only part of this operation that warrants much explanation is the creation of modified Attribute objects. The and classes implement the Attributes and Attribute interfaces, respectively. These are the classes you’ll typically use to create modified attribute values.

For example, let’s say we want to remove the phone number “303 444 6633” from a user entry’s “telephonenumber” attribute and replace it with the new number “520 765 4321.” In the following code, we create two BasicAttributes objects, newNumber and oldNumber, and use them in calls to modifyAttributes( ):

DirContext user ... ; // Created somewhere else in the program BasicAttribute newAttr = new BasicAttribute( ); newAttr.add("telephonenumber", "520 765 4321"); BasicAttributes newNumber = new BasicAttributes( ); newNumber.put(newAttr); BasicAttributes oldNumber = new BasicAttributes("telephonenumber", "303 444 6633"); user.modifyAttributes("", DirContext.REMOVE_ATTRIBUTE, oldNumber); ...

Get Java Enterprise in a Nutshell, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.