I have some code that's close to what I need but I need help figuring out the following things. They probably aren't that hard, but I can't figure them out. Current code is at the very bottom.
1) I have some other code that searches and returns information and I use:
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
so that it searches the whole tree. How do I modify my code below so I can just specify a cn=[username] and it will search the whole tree? Right now I have to specify the dn from the base.
2) How can I test some other field, like fullName, to make sure that the cn that is found is the correct one? Just in case there is a duplicate or something?
3) Lastly, is there a way to trap if the change was not successful and react appropriately. Maybe I'm doing enough with the exception I'm catching, not sure.
Any help is really appreciated.
James
1) I have some other code that searches and returns information and I use:
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
so that it searches the whole tree. How do I modify my code below so I can just specify a cn=[username] and it will search the whole tree? Right now I have to specify the dn from the base.
2) How can I test some other field, like fullName, to make sure that the cn that is found is the correct one? Just in case there is a duplicate or something?
3) Lastly, is there a way to trap if the change was not successful and react appropriately. Maybe I'm doing enough with the exception I'm catching, not sure.
Code:
try
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + givenValues[1] + ":389/" + givenValues[2]);
// Authenticate
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=adminuser");
env.put(Context.SECURITY_CREDENTIALS, "adminpassword");
DirContext ctx = new InitialDirContext(env);
// Specify the changes to make
ModificationItem[] mods = new ModificationItem[1];
// Replace attribute with a new value
Attribute mod = new BasicAttribute("","");
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userpassword", "mypassword"));
String dn = "cn=MyUsername, ou=MyOU";
ctx.modifyAttributes(dn, mods);
} catch (NamingException e)
{
System.out.println ("Problem looking up " + givenValues[0] + ": " + e);
}
Any help is really appreciated.
James