3

I want to delete attribute with multiple values but it ignores this while successfully deletes another attribute with specific value in directory.

Attribute attr1 = new BasicAttribute("fooAttr");
ModificationItem attr1mod = new 
ModificationItem(DirContext.REMOVE_ATTRIBUTE,attr1);
Attribute attr2 = new BasicAttribute("barAttr","my value");
ModificationItem attr2mod = new 
ModificationItem(DirContext.REMOVE_ATTRIBUTE,attr2);
try{ 
    ldapTemplate.modifyAttributes("cn=myname,dc=example,dc=com", new 
    ModificationItem[]{attr1mod, attr2mod});
    } catch(Exception e) {
    // 
}

barAttr is deleted with the specified value but multivalued fooAttr is not deleted.

barAttr is also multivalued and the specified value is deleted. I also tried to delete a specified value of fooAttr and still it failed to delete the specified value of fooAttr. I confirmed externally that fooAttr exists in the directory.

ldapTemplate.modifyAttributes executes successfully, does not throw exception.

If I do this:

ldapTemplate.modifyAttributes("cn=myname,dc=example,dc=com", new 
    ModificationItem[]{attr1mod});

then in this case exception is thrown:

LDAP: error code 16 - Failed to modify the underlying source: No such attribute

UPDATE I also tried this approach:

DirContextOperation context = ldaptemplate.lookup(dn);
String [] fooAttrValues = context.getStringAttributes("fooAttr");

for(String value : fooattrValues) {
  context.removeAttributeValue("fooAttr", value);
}
String [] fooAttrValues2 = context.getStringAttributes("fooAttr");

This does not work, after program is run the director still has the same attribute and all the values. I debugged through it, it does get the correct values but does not remove any value, even fooAttrValues2 still has all the values.

How can I fix this problem?

1 Answer 1

2
+250

I think to delete specific attribute values, you better to use the DirContext.REMOVE_ATTRIBUTE flag for each value individually, right now, you are using DirContext.REMOVE_ATTRIBUTE flag for both attributes attr1 and attr2 in your code and it used to remove the entire attribute, not specific attribute values!

something like this:

Attribute attr1 = new BasicAttribute("fooAttr", "value1"); 
ModificationItem attr1mod = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr1);

Attribute attr2 = new BasicAttribute("barAttr", "my value"); 
ModificationItem attr2mod = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr2);

try {
    ldapTemplate.modifyAttributes("cn=myname,dc=example,dc=com", new ModificationItem[]{attr1mod, attr2mod});
} catch (Exception e) {
    //here handle your exceptions
}

Update
and also, instead of using the REMOVE_ATTRIBUTE,you can try using the REPLACE_ATTRIBUTE with the modified list of values for fooAttr.it involves retrieving the existing attribute values, removing the specific value you want to delete, and then using REPLACE_ATTRIBUTE to update fooAttr with the modified list!

ModificationItem attr1mod = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("fooAttr"));

//removing a specific value of the multi-valued attribute
ModificationItem attr2mod = new ModificationItem(DirContext.REMOVE_ATTRIBUTE | DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("barAttr", "my value"));

ldapTemplate.modifyAttributes("cn=myname,dc=example,dc=com", new ModificationItem[]{attr1mod, attr2mod});
Sign up to request clarification or add additional context in comments.

5 Comments

Freeman, thanks but as mentioned in the post ; "I also tried to delete a specified value of fooAttr and still it failed to delete the specified value of fooAttr. I confirmed externally that fooAttr exists in the directory." whic is exactly what your code is doing bu fails to delete even specific value of fooAttr.
so try using a different LDAP client, such as Apache Directory Studio, to perform the same modification and see if you encounter the same issue. and check my answer again plz.
ldaptemplate.getattributes does not exist. I tried similar approach but does not work either.
@ace oh, yes, I apologize. It was a piece of another project that I copied and pasted that night, and it was a mistake. I have updated the code again. Could you please check if the problem is resolved with the new code? thnx
your code is very similar to my code in the post. So still same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.