Java Netscape LDAP Remove One Attribute
I have LDAP schema where are users. I need remove one attribute named "notify" which have va开发者_开发知识库lues: phone number or mail or remove attribute from user. I found method
LDAPConnection myCon = new LDAPConnection("localhost",389);
myCon.delete("uid=test1, ou=People, o=domain.com, o=isp");
but this remove whole user and i need remove only one attribute "notifyTo" of this user. I need remove whole attribute not only its value.
Thanks for reply
You need to call modify method on LDAPConnection class :-)
From the javadocs:
public void modify(java.lang.String DN, LDAPModification mod) throws LDAPException Makes a single change to an existing entry in the directory (for example, changes the value of an attribute, adds a new attribute value, or removes an existing attribute value). Use the LDAPModification object to specify the change to make and the LDAPAttribute object to specify the attribute value to change. The LDAPModification object allows you add an attribute value, change an attibute value, or remove an attribute value.
For example, the following section of code changes Barbara Jensen's email address in the directory to babs@aceindustry.com.
Example code from javadocs:
String myEntryDN = "cn=Barbara Jensen,ou=Product Development,o=Ace Industry,c=US";
LDAPAttribute attrEmail = new LDAPAttribute( "mail", "babs@aceindustry.com" );
LDAPModification singleChange = new LDAPModification( LDAPModification.REPLACE, attrEmail );
myConn.modify( myEntryDN, singleChange );
This sample is for removing one value of one of your entry's attributes. You need to delete all values :-)
Solution without Netscape API:
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
....
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
DirContext dctx = new InitialDirContext(env);
// next 3 lines only if authentication needed
dctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
dctx.addToEnvironment(Context.SECURITY_PRINCIPAL, "<userDN>");
dctx.addToEnvironment(Context.SECURITY_CREDENTIALS, "<password>");
Attributes attrs= new BasicAttributes();
Attribute attr= new BasicAttribute("<attrName>");
attrs.put(attr);
dctx.modifyAttributes ("<entryDN>", DirContext.REMOVE_ATTRIBUTE, attrs);
Old question but good question, from the docs (Directory SDK for Java 4.0 Programmer's Guide) and complementing SourceRebels' answer:
To remove an attribute from an entry, you can do one of the following:
- replace the values of the attribute with no values (construct the LDAPAttribute object with no values)
- specify that you want to remove a value from the attribute, and specify no value (construct the LDAPAttribute object with no values)
- remove all values of the attribute
You can set that attribute as LDAPModification.DELETE in the LDAPModificationSet
If the attribute is "notifyTo",
LDAPConnection myCon = new LDAPConnection("localhost",389);
LDAPModificationSet mods = new LDAPModificationSet();
mods.add(LDAPModification.DELETE, new LDAPAttribute("notifyTo"));
myCon.modify("uid=test1, ou=People, o=domain.com, o=isp", mods);
You can add, replace or delete any number of attributes from the user. All these can be specified in the LDAPModificationSet actions to be performed. If you want to replace an attribute "email" for the user, add it to the LDAPModificationSet and call the modify() method in the end.
mods.add(LDAPModification.REPLACE, new LDAPAttribute("email","mytest@abc.com"));
While deleting an attribute,just make sure that the attribute is already present in the user LDAP entry, otherwise a NO_SUCH_ATTRIBUTE(Error Code 16) LDAPException would be thrown when the modify() method is called.
精彩评论