views:

190

answers:

4

Hi, I have LDAP schema where are users. I need remove one attribute named "notify" which have values: 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

+1  A: 

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 [email protected].

Example code from javadocs:

String myEntryDN = "cn=Barbara Jensen,ou=Product Development,o=Ace Industry,c=US";
LDAPAttribute attrEmail = new LDAPAttribute( "mail", "[email protected]" );
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 :-)

SourceRebels
+1  A: 

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);
Miklos
A: 

to SourceRebels:

Thanks this solution I tried. This method only removes value of attribute, but I need remove whole attribute. Like when I get in LDAP browser right mouse click on attribute and select Delete attribute.

Any other solution?

to Miklos: thank i try

spex
A: 

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
Damien B