Java/JNDI LDAP/Name
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Getting an Object"s Fully Qualified Name
- 2 Parsing a JNDI Composite Name
- 3 Parsing a JNDI Compound Name
- 4 Replace the placeholders in the filter with the proper values from the userName
- 5 Show only the common name (cn) and e-mail address (mail) attributes
- 6 Using a URL as a Name to the Initial Context
Getting an Object"s Fully Qualified Name
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "iiop://localhost/";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, url);
Context ctx = new InitialContext(env);
// Create a subcontext.
Context childCtx = ctx.createSubcontext("child");
// Destroy the subcontext.
ctx.destroySubcontext("child");
Context obj = (Context) childCtx.lookup("grandChild");
String fullname = obj.getNameInNamespace();
}
}
Parsing a JNDI Composite Name
import javax.naming.rupositeName;
public class Main {
public static void main(String[] argv) throws Exception {
CompositeName composite = new CompositeName("cn=John,o=hits/summary.txt");
String first = composite.get(0);
String last = composite.get(composite.size() - 1);
composite.add(0, "yourname.ru");
composite.remove(2);
}
}
Parsing a JNDI Compound Name
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NameParser;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "iiop://localhost/";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, url);
Context ctx = new InitialContext(env);
NameParser parser = ctx.getNameParser("");
Name dn = parser.parse("cn=John, ou=People, o=JNDITutorial");
dn.remove(1);
dn.add(0, "c=us");
dn.add("cn=fs");
}
}
Replace the placeholders in the filter with the proper values from the userName
/*
* Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license
* distributed with this file and available online at
* http://www.ja-sig.org/products/cas/overview/license/
*/
import java.util.HashMap;
import java.util.Map;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
/**
* Utilities related to LDAP functions.
*
* @author Scott Battaglia
* @version $Revision: 42053 $ $Date: 2007-06-10 09:17:55 -0400 (Sun, 10 Jun 2007) $
* @since 3.0
*/
public final class LdapUtils {
private LdapUtils() {
// private constructor so that no one can instantiate.
}
/**
* Utility method to replace the placeholders in the filter with the proper
* values from the userName.
*
* @param filter
* @param userName
* @return the filtered string populated with the username
*/
public static String getFilterWithValues(final String filter,
final String userName) {
final Map<String, String> properties = new HashMap<String, String>();
final String[] userDomain;
String newFilter = filter;
properties.put("%u", userName.replace("\\", "\\\\"));
userDomain = userName.split("@");
properties.put("%U", userDomain[0]);
if (userDomain.length > 1) {
properties.put("%d", userDomain[1]);
final String[] dcArray = userDomain[1].split("\\.");
for (int i = 0; i < dcArray.length; i++) {
properties.put("%" + (i + 1), dcArray[dcArray.length
- 1 - i]);
}
}
for (final String key : properties.keySet()) {
final String value = properties.get(key);
newFilter = newFilter.replaceAll(key, value);
}
return newFilter;
}
/**
* Close the given context and ignore any thrown exception. This is useful
* for typical finally blocks in manual Ldap statements.
*
* @param context the Ldap context to close
*/
public static void closeContext(final DirContext context) {
if (context != null) {
try {
context.close();
} catch (NamingException ex) {
}
}
}
}
Show only the common name (cn) and e-mail address (mail) attributes
import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
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 Main {
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://localhost:1389";
public static String MY_SEARCHBASE = "o=server.ru";
public static String MY_FILTER = "(sn=Carter)";
public static String MY_ATTRS[] = { "cn", "mail" };
public static void main(String args[]) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
env.put(Context.PROVIDER_URL, MY_HOST);
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);
while (results != null && results.hasMore()) {
SearchResult sr = (SearchResult) results.next();
String dn = sr.getName() + ", " + MY_SEARCHBASE;
System.out.println("Distinguished Name is " + dn);
Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
if (ar == null) {
System.out.println("Entry " + dn + " has none of the specified attributes\n");
return;
}
for (int i = 0; i < MY_ATTRS.length; i++) {
Attribute attr = ar.get(MY_ATTRS[i]);
if (attr == null) {
continue;
}
System.out.println(MY_ATTRS[i] + ":");
for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
System.out.println("\t" + vals.nextElement());
}
}
}
}
}
Using a URL as a Name to the Initial Context
import javax.naming.InitialContext;
public class Main {
public static void main(String[] argv) throws Exception {
Object obj = new InitialContext().lookup("iiop://localhost/Sample");
}
}