Java调用Domino LDAP

import java.util.Hashtable;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class TestLDAP {

	/**
	 * @param args
	 */

	public static void main(String[] args) throws Exception {
		Hashtable env = new Hashtable(11);
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.SECURITY_PRINCIPAL, "test1");
		env.put(Context.SECURITY_CREDENTIALS, "password");
		env.put(Context.PROVIDER_URL, "ldap://107.252.253.239:389");
		 
		try {

			DirContext ctx = new InitialDirContext(env);			
			System.out.println("Context Namespace = " + ctx.getNameInNamespace());

			String name = "CN=admin,O=ABCSDC";
			System.out.println(name);
			ctx.lookup(name);
			Attributes attrs = ctx.getAttributes(name);
			System.out.println("Mailfile: " + attrs.get("mailfile").get());
			System.out.println("Mailserver: " + attrs.get("mailserver").get());

			NamingEnumeration<NameClassPair> iter = ctx.list(name);

			while (iter.hasMore()) {
				NameClassPair p = iter.next();
				System.out.println(p.getName() + " " + p.getNameInNamespace()
						+ " " + p.getClassName());
			}

			name = "CN=分行测试用户1,OU=分行测试部门,OU=分行,O=ABCSDC";
			System.out.println(name);
			Attributes attrs_new = ctx.getAttributes(name);
			System.out.println("Mailfile: " + attrs_new.get("mailfile").get());
			System.out.println("Mailserver: "
					+ attrs_new.get("mailserver").get());

			iter = ctx.list(name);

			while (iter.hasMore()) {
				NameClassPair p = iter.next();
				System.out.println(p.getName() + " " + p.getNameInNamespace()
						+ " " + p.getClassName());
			}

			String filter = "(CN=*)";
			SearchControls controls = new SearchControls();
			controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
			NamingEnumeration ne = ctx.search("O=ABCSDC", filter, controls);
			
			System.out.println("To list all user in O=ABCSDC ");
			while (ne.hasMore()) {
				SearchResult result = (SearchResult) ne.next();
				System.out.println("Name = " + result.getName()
						+ ", Namespace = " + result.getNameInNamespace());
			}
			
			
			System.out.println("直接从O=ABCSDC目录搜索");
			
			env.put(Context.PROVIDER_URL,"ldap://107.252.253.239:389/O=ABCSDC");			
			ctx = new InitialDirContext(env);			
			System.out.println(ctx.getNameInNamespace());
			name = "CN=分行测试用户1,OU=分行测试部门,OU=分行";
			System.out.println("name = " + name);
			ctx.lookup(name);
			attrs = ctx.getAttributes(name);
			System.out.println("Mailfile: " + attrs.get("mailfile").get());
			System.out.println("Mailserver: " + attrs.get("mailserver").get());			
			System.out.println();
			
			System.out.println("从第一级目录 OU=分行,O=ABCSDC搜索");
			
			env.put(Context.PROVIDER_URL, new String(
					 "ldap://107.252.253.239:389/OU=分行,O=ABCSDC"
					 .getBytes("UTF-8"), "ISO8859-1"));
			
			ctx = new InitialDirContext(env);			
			System.out.println(ctx.getNameInNamespace());
			name = "CN=分行测试用户1,OU=分行测试部门";			
			System.out.println("name = " + name);
			ctx.lookup(name);
			attrs = ctx.getAttributes(name);
			System.out.println("Mailfile: " + attrs.get("mailfile").get());
			System.out.println("Mailserver: " + attrs.get("mailserver").get());
			System.out.println();
			
			System.out.println("从第二级目录 OU=分行测试部门, OU=分行,O=ABCSDC搜索");	
			
			env.put(Context.PROVIDER_URL, new String(
					 "ldap://107.252.253.239:389/OU=分行测试部门,OU=分行,O=ABCSDC"
					 .getBytes("UTF-8"), "ISO8859-1"));			
			ctx = new InitialDirContext(env);			
			System.out.println(ctx.getNameInNamespace());
			name = "CN=分行测试用户1";			
			System.out.println("name = " + name);
			ctx.lookup(name);
			attrs = ctx.getAttributes(name);
			System.out.println("Mailfile: " + attrs.get("mailfile").get());
			System.out.println("Mailserver: " + attrs.get("mailserver").get());			
			System.out.println();
			
			System.out.println("全目录完整搜索");	
			env.put(Context.PROVIDER_URL, new String(
					 "ldap://107.252.253.239:389/CN=分行测试用户1,OU=分行测试部门,OU=分行,O=ABCSDC"
					 .getBytes("UTF-8"), "ISO8859-1"));			
			ctx = new InitialDirContext(env);			
			System.out.println(ctx.getNameInNamespace());
			name = "";			
			System.out.println("name = " + name);
			ctx.lookup(name);
			attrs = ctx.getAttributes(name);
			System.out.println("Mailfile: " + attrs.get("mailfile").get());
			System.out.println("Mailserver: " + attrs.get("mailserver").get());			
			System.out.println();
			
			
			
			
			ctx.close();

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

你可能感兴趣的:(java,LDAP,domino)