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
// ldaptemplateCode:<?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>
// jndiCode:private int writeTPIN(String externalKey,int tpin){ Name dn = buildDn(tpin); ldapTemplate.bind(dn,null,buildAttributes(externalKey,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; }
Code: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(); } }


Reply With Quote