Mercurial > hg > test-repo
view jndi/src/test/java/de/codedo/jndi/JndiTestCase.java @ 21:e1a9127a7d57
add sample code for writing into LDAP
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Tue, 25 Oct 2016 14:56:08 +0000 |
parents | |
children | 886a28c5898c |
line wrap: on
line source
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<NameClassPair> 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<String, Object> 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); } }