JNDI: Delete only one attribute when there are several different values for the same attribute

B

bsporb

Hi;

My user entry has several role attributes, each with different values.

Can I use context.modifyAttributes() to remove only one of these
values?

Example:

DN uid=foo,ou=People,o=us.org

Attributes:
....
sysRole developer
sysRole user
sysRole tester
....

how would I remove only the user role attribute?

thanks for any help.

-sporb
 
B

bsporb

There's something creative about posting to usenet :)

I think i figured it out just after posting the above. What seems to
work:


// attribute, value are the values that I want to remove...
BasicAttribute ba = new BasicAttribute(attribute, value);
BasicAttributes bas = new BasicAttributes();
bas.put(ba);
context.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, bas);


I would still be interested to hear of other approaches, or thoughts
on this.

thanks again
-sporb
 
B

Brandon McCombs

There's something creative about posting to usenet :)

I think i figured it out just after posting the above. What seems to
work:


// attribute, value are the values that I want to remove...
BasicAttribute ba = new BasicAttribute(attribute, value);
BasicAttributes bas = new BasicAttributes();
bas.put(ba);
context.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, bas);


I would still be interested to hear of other approaches, or thoughts
on this.

thanks again
-sporb

I haven't ever tried your idea with my own app that utilizes JNDI but I
would think that on the surface, your code would remove the attribute
and any values it contained but if you say it works then it must be okay.

I, on the other hand, take the opposite approach. I ignore any values
that will be removed and create a new BasicAttribute that contains only
the values that will be kept and write that back to the directory store
using modifyAttributes().
 
B

Brandon McCombs

There's something creative about posting to usenet :)

I think i figured it out just after posting the above. What seems to
work:


// attribute, value are the values that I want to remove...
BasicAttribute ba = new BasicAttribute(attribute, value);
BasicAttributes bas = new BasicAttributes();
bas.put(ba);
context.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, bas);


I would still be interested to hear of other approaches, or thoughts
on this.

thanks again
-sporb

Just wanted to say that I was re-reading your initial post and was
originally thinking you were wanting to delete a value from an attribute
when it had multiple values but it seems you want to delete a whole
attribute. You seem to have been using the word "value" in multiple ways
but in LDAP terminology it means something specific so it threw me off
until I re-read your post.

Going back to your original post, you wanted to only remove the the
sysRole user attribute. Given your example, sysRole is the attribute
but 'user' is the data (you called 'user' an attribute), specifically it
is the 2nd value for the sysRole attribute based on your example.
Depending on whether or not I understand you correctly this time and
whether you correctly used the terms you wanted to use I'll say this:

You are on the right track but you don't have to specify any value to
the BasicAttribute() constructor to delete an attribute. It has another
constructor that takes only an attribute name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top