PDA

View Full Version : ldaptemplate.bind EntryNotFoundException


bdrhoa
Jul 21st, 2006, 08:29 AM
I'm using Spring with Xfire and LDAPTemplate to build a web service
that given some "external key (e.g. ssn)", creates a unique id in form
of a random 9 digit number (called a TPIN). The service creates
entries in a Sun LDAP directory.

I'm getting the following error:

net.sf.ldaptemplate.EntryNotFoundException: Entry not found; nested
exception is javax.naming.NameNotFoundException:
[LDAP: error code 32 - No Such Object]; remaining name 'uid=778259765,
ou=7, ou=identities, o=TAP'

Below is my applicationContext.xml, followed by my ldaptempplate code,
followed by JNDI code that works correctly.

What am I doing wrong?

Thanks for the help!

// applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="contextSource"
class="net.sf.ldaptemplate.support.LdapContextSource">
<property name="url" value="ldap://myldap.com:14000/o=tap" />
<property name="base" value="o=tap" />
<property name="userName" value="uid=TPINservice, ou=Service
Accounts, ou=Principals,o=tap" />
<property name="password" value="somepassword" />
</bean>

<bean id="ldapTemplate"
class="net.sf.ldaptemplate.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>

<bean id="tpinBean"
class="com.tap.tpin.TPINImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
</beans>


// ldaptemplate



private int writeTPIN(String externalKey,int tpin){
Name dn = buildDn(tpin);

ldapTemplate.bind(dn,null,buildAttributes(external Key,tpin));

return tpin;

}

protected Name buildDn(int tpin){
DistinguishedName dn = new DistinguishedName (BASE_DN);
dn.add("ou","identities");
dn.add("ou",String.valueOf(String.valueOf(tpin).charAt(0)));
dn.add("uid",String.valueOf(tpin));
return dn;
}

private Attributes buildAttributes(String externalKey,int tpin) {
Attributes attrs = new BasicAttributes();
BasicAttribute ocattr = new BasicAttribute("objectclass");
ocattr.add("top");
ocattr.add("person");
ocattr.add("organizationalPerson");
ocattr.add("inetOrgPerson");
ocattr.add("TAPPerson");
attrs.put(ocattr);
attrs.put("TAPexternalkey", externalKey);
attrs.put("givenName", "TPIN");
attrs.put("sn","Service");
attrs.put("cn","TPIN Service");

return attrs;
}



// jndi


public static void main(String[] args) {


Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://myldap.com:14000/o=tap");
env.put(Context.SECURITY_PRINCIPAL, "uid=TPINService,ou=Service
Accounts,OU=Principals,o=TAP");
env.put(Context.SECURITY_CREDENTIALS,"somepassword");

try {

// Connect to the Directory Server
DirContext ctx = new InitialDirContext(env);

int TPIN = 300000000;

BasicAttribute objClasses = new BasicAttribute("objectclass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("inetOrgPerson");
objClasses.add("TAPPerson");

BasicAttributes attrs = new BasicAttributes();
attrs.put(objClasses);
attrs.put("TAPexternalkey", "999887777");
attrs.put("givenName", "Bill");
attrs.put("sn","Smith");
attrs.put("cn","Bill Smith");

boolean added = false;

do {
try {
String DN = "uid=" + Integer.toString(TPIN) + ",ou=3,ou=identities";
ctx.createSubcontext(DN, attrs);
System.out.println("Successfully added: " + DN);
added = true;
}
catch (NameAlreadyBoundException e) {
System.out.println("TPIN: " + Integer.toString(TPIN) + " is
already in use.");
TPIN+=1;
}

} while (!added);


// Close the context when we're done
ctx.close();


}
catch (Exception e) {
e.printStackTrace();
}

}

bdrhoa
Jul 21st, 2006, 12:55 PM
I got to the LDAP server access log and that when I use ldamptemplate, the server is getting:

ADD dn="uid=991999723, ou=9, ou=identities, o=TAP,o=tap/o=tap"

instead of

ADD dn="uid=300000007,ou=3,ou=identities,o=tap"

which is what my jndi code sends.

bdrhoa
Jul 21st, 2006, 01:51 PM
I got it working!

I changed my applicationcontext.xml to


...
<property name="url" value="ldap://myldap.com:14000/" />
<property name="base" value="o=tap" />
...


and at the moment I'm creating my dn by hand:


tempDn = "uid=" + String.valueOf(tpin) + ",ou=" +
String.valueOf(String.valueOf(tpin).charAt(0)) + ",ou=identities";

dn = new DistinguishedName (tempDn);


but I'm pretty sure what I had before will work if just change my code to:


...
public static final String BASE_DN = ""
...


or I could probably drop that line completely and just do


...
DistinguishedName dn = new DistinguishedName ();
...



I'll verify when I get back from vacation.

rasky
Jul 22nd, 2006, 04:24 AM
Quite right, there's no need to manually build the DN; it should work if you start with an empty DistinguishedName.