Java by API/javax.naming.ldap/LdapContext — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
LdapContext: getResponseControls()
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.HasControls;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "ldap://localhost/o=JNDITutorial";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "userDN");
env.put(Context.SECURITY_CREDENTIALS, "secret");
LdapContext ctx = new InitialLdapContext(env, null);
NamingEnumeration answer = ctx.search("ou=People", "(cn=*)", null);
System.out.println(ctx.getResponseControls());
while (answer.hasMore()) {
SearchResult si = (SearchResult) answer.next();
if (si instanceof HasControls) {
System.out.println(((HasControls) si).getControls());
}
}
System.out.println(ctx.getResponseControls());
}
}
LdapContext: setRequestControls(Control[] requestControls)
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.SortControl;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "ldap://localhost/o=JNDITutorial";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "userDN");
env.put(Context.SECURITY_CREDENTIALS, "secret");
Control[] connectCtls = new Control[] { null };
LdapContext ctx = new InitialLdapContext(env, null);
Control[] ctxCtls = new Control[] { new SortControl(new String[] { "cn" }, Control.CRITICAL) };
ctx.setRequestControls(ctxCtls);
NamingEnumeration answer = ctx.list("");
while (answer.hasMore()) {
NameClassPair item = (NameClassPair) answer.next();
}
}
}