# HG changeset patch # User Dirk Olmes # Date 1477407368 0 # Node ID e1a9127a7d57b682398415287795576b09a133b6 # Parent 242ed49b42c99398d45884feee3f7a5df313099d add sample code for writing into LDAP diff -r 242ed49b42c9 -r e1a9127a7d57 jndi/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jndi/pom.xml Tue Oct 25 14:56:08 2016 +0000 @@ -0,0 +1,29 @@ + + 4.0.0 + de.codedo + jndi + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + + + + + junit + junit + 4.12 + test + + + \ No newline at end of file diff -r 242ed49b42c9 -r e1a9127a7d57 jndi/src/test/java/de/codedo/jndi/JndiTestCase.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java Tue Oct 25 14:56:08 2016 +0000 @@ -0,0 +1,79 @@ +package de.codedo.jndi; + +import org.junit.*; +import java.util.*; +import javax.naming.*; +import javax.naming.directory.*; +import javax.naming.ldap.*; + +public class JndiTestCase extends Object +{ + private static final String USER_BASE = "ou=users,dc=ldap,dc=example,dc=org"; + + @Test + @Ignore + public void connectToJndi() throws Exception + { + DirContext context = createContext(); + + NamingEnumeration enumeration = context.list(USER_BASE); + while (enumeration.hasMore()) + { + NameClassPair pair = enumeration.next(); + + String dn = pair.getName() + "," + USER_BASE; + LdapContext ldapContext = (LdapContext) context.lookup(dn); + System.out.println(ldapContext); + + /* + Attributes attributes = context.getAttributes(dn); + NamingEnumeration attributesEnum = attributes.getAll(); + while (attributesEnum.hasMore()) + { + Attribute attribute = (Attribute)attributesEnum.next(); + System.out.println(attribute.getID()); + } + */ + } + + context.close(); + } + + @Test + public void createInLdap() throws Exception + { + DirContext context = createContext(); + + Attribute objectClass = new BasicAttribute("objectClass"); + objectClass.add("inetOrgPerson"); + objectClass.add("organizationalPerson"); + objectClass.add("person"); + objectClass.add("top"); + + Attribute cn = new BasicAttribute("cn", "Peter"); + Attribute sn = new BasicAttribute("sn", "Penis"); + Attribute uid = new BasicAttribute("uid", "ppe"); + + Attributes attributes = new BasicAttributes(); + attributes.put(objectClass); + attributes.put(cn); + attributes.put(sn); + attributes.put(uid); + + String dn = "uid=ppe," + USER_BASE; + context.createSubcontext(dn, attributes); + + context.close(); + } + + private DirContext createContext() throws Exception + { + Hashtable env = new Hashtable<>(); + env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); + env.put(Context.PROVIDER_URL, "ldap://192.168.66.66:389"); + env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=ldap,dc=example,dc=org"); + env.put(Context.SECURITY_CREDENTIALS, "geheim"); + + return new InitialDirContext(env); + } +}