PDA

View Full Version : Modifying LDAP Context's Base?



khardiman
Feb 26th, 2007, 05:18 PM
I am successfully able to create an LDAP context from my Spring xml file, and would like to modify the base attribute of the context. My base is pointing to the highest point in my LDAP tree, and I would like to add objects at points deeper in the tree (additional levels in the tree already exist). Example:

base: ou=Test,dc=example,dc=com
exists: ou=Users,ou=Test,dc=example,dc=com
looking to add: uid=admin,ou=Users,ou=Test,dc=example,dc=com

Unfortunately if I try to add the last entry above with the current base, the operation will fail saying that ou=Users already exists. As such, I'd like to change the base for the context to ou=Users,ou=Test,dc=example,dc=com so that the add will succeed. I have tried the following, without much luck:


ctx.setBase("ou=Users," + ctx.getReadOnlyContext().getNameInNamespace());


Here is the bulk of the code:

FileSystemResource in =
new FileSystemResource("spring-ldapContext.xml");
BeanFactory factory =
new XmlBeanFactory(in);
LdapTemplate ldapTemplate = null;

try {
LdapContextSource ctx = (LdapContextSource)factory.getBean("contextSource");
String base = "ou=Users," + ctx.getReadOnlyContext().getNameInNamespace();
ctx.setBase(base);

ldapTemplate = new LdapTemplate(ctx);
}
catch (NamingException e) {
System.err.println(e.getMessage());
System.exit(1);
}



Any help would be greatly appreciated!

Kevin Hardiman

rasky
Feb 27th, 2007, 12:20 AM
There should be no need to modify the base of the ContextSource to be able to add entries. I suspect the problem is in the code that attempts to add the sub-entry (uid=admin,ou=Users,ou=Test,dc=example,dc=com). If you'll post that code along with the stack trace, I'll be happy to take a look at it.

khardiman
Feb 27th, 2007, 10:14 AM
You were correct - there was a problem elsewhere. I created a DistinguishedName, added the RDNs from the base including the OUs that already existed, and finally the object to be added with success.


DistinguishedName dn = new DistinguishedName();
dn.add("ou", "Users");
dn.add("uid", u.getUserId());