changeset 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 242ed49b42c9
children 886a28c5898c
files jndi/pom.xml jndi/src/test/java/de/codedo/jndi/JndiTestCase.java
diffstat 2 files changed, 108 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>de.codedo</groupId>
+	<artifactId>jndi</artifactId>
+	<version>1.0-SNAPSHOT</version>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+
+	<dependencies>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.12</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+</project>
\ No newline at end of file
--- /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<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);
+	}
+}