Search code examples
javaldapjndi

LDAP replace a list values for one attribute


I have an attribute, like telephonenumber, which appears several times on a person. Now I want to replace all numbers by a list of new numbers:

<person>  
<telephonnumber>12345</telephonnumber>  
<telephonnumber>23456</telephonnumber>  
</person>  

replace by:

<person>  
<telephonnumber>56789</telephonnumber>  
<telephonnumber>78901</telephonnumber>  
</person>  

How can I do this in Java?

Using

mods.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("telephonnumber", "56789")));  
mods.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("telephonnumber", "78901")));  

ends up with all values will be replaced by the last ModificationItem. Well I could work around by removing all number, and adding all new values from the list. But I think Java LDAP supports it directly.


Solution

  • You want to create a single replace with your multi-valued telephone attribute. See the Oracle LDAP attributes tutorial.

    // Create a multivalued attribute that has four String values
    BasicAttribute oc = new BasicAttribute("objectClass", "top");
    oc.add("person");
    oc.add("organizationalPerson");
    oc.add("inetOrgPerson");
    

    Tip: First try out your LDAP operations via an LDIF file before you start coding.