Java Reference
In-Depth Information
Modifying Information Stored in LDAP
Modifying the attributes of an existing object in an LDAP server requires slightly more
thought than the rest of the operations. When changing the values for the attributes for an
object, there are three ways in which the modification can be performed.
11
The three possible modification operations are:
ADD
,
REPLACE
, and
REMOVE
. When the
ADD_ATTRIBUTE
operator is used, any values that are specified for an attribute are added to the
existing values already stored in the LDAP object.
The
REPLACE_ATTRIBUTE
operator specifies that any values already present for the attribute
should be replaced with the new values. No matter how many values are already assigned to
the attribute in the LDAP server, they will all be removed and replaced with the values that are
given to the
modify
method.
Finally, you can remove values and entire attributes from an existing LDAP object by using the
REMOVE_ATTRIBUTE
operator. When using the
REMOVE
operator, if a value is specified for the
attribute that is passed into the
modify
method, only those given values will be removed from
the LDAP object. On the other hand if no value is given for the attribute in conjunction with a
REMOVE
operator, all values will be removed from the attribute.
Now let's take a look at the method in the
LDAPManager
that modifies an existing LDAP object.
Here is what it looks like:
public void modify(String dn, HashMap attributes, int modOp)
throws NamingException {
// perform the modification on all of the given attributes
// based on the modOp that was passed in
DirContext ctx = getInitialContext(getEnvironment());
ctx.modifyAttributes(dn, modOp, parseAttributes(attributes));
}
There is not a lot to this method. The DN for the object that you are modifying must be present
so the LDAP server knows where to make the changes. The modify operation that we dis-
cussed earlier is specified so the LDAP server knows how to make the changes. The attributes
to be modified must be given so that the LDAP server knows what changes to make. Notice
that you use the
parseAttributes()
method, defined earlier, to build the list of attributes from
a Java
HashMap
.
Here is an example of how you might use this method to change the value for someone's e-
mail address
LDAPManager lm = new LDAPManager();
try {