blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

LDAP Operation: Modify attributes

One way to modify the attributes of an object is to supply a list of modification requests ( ModificationItem). Each ModificationItem consists of a numeric constant indicating the type of modification to make and an Attribute describing the modification to make. Following are the three types of modifications:
ADD_ATTRIBUTE
REPLACE_ATTRIBUTE
REMOVE_ATTRIBUTE
Modifications are applied in the order in which they appear in the list. Either all of the modifications are executed, or none are.

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
....
try {
 //In the main() method of the program, create an initial directory context. 
 //This is similar to creating an initial context in the previous naming example, 
 //except that you use the constructor for InitialDirContext
 Hashtable env = new Hashtable();
 env.put(Context.INITIAL_CONTEXT_FACTORY,
  "com.sun.jndi.ldap.LdapCtxFactory");
 env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=csRepository");

 DirContext ctx = new InitialDirContext(env);
 
 String dn = "cn=John Eipe, ou=employees";
 
 // Specify the changes to make
 ModificationItem[] mods = new ModificationItem[2];

 // Replace the "mail" attribute with a new value
 mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  new BasicAttribute("mail", "ldapemail@mail.com"));

 // Add an additional value to "telephonenumber"
 mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
  new BasicAttribute("telephonenumber", "+918939302763"));

 // Perform the requested modifications on the named object
 ctx.modifyAttributes(dn, mods);
 
}catch(NamingException e){
 e.printStackTrace();
}
.....

Output:


Another way to make modifications is to use the overloaded method,
void modifyAttributes(Name name,
int mod_op,
Attributes attrs)
throws NamingException
name - the name of the object whose attributes will be updated
mod_op - the modification operation, one of: ADD_ATTRIBUTE, REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE.
attrs - the attributes to be used for the modification; may not be null

Continue reading: LDAP Operations

No comments:

Post a Comment