자바 호출 Domino LDAP

5261 단어 자바LDAPdomino
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();
		}
	}
}

좋은 웹페이지 즐겨찾기