How to show all objectclasses description of ldap directory using jndi
I want to show all objectclasses present in schema of LDAP Directory to help user to input available objectclasses for ad开发者_JAVA技巧ding new entry.
DirContext schema = ctx.getSchema("");
Attributes answer = schema.getAttributes("ClassDefinition/person");
but that shows information about person only.
DirContext schema=dcx.getSchema("");
NamingEnumeration bindings = schema.listBindings("ClassDefinition");
while (bindings.hasMore())
{
Binding bd = (Binding)bindings.next();
System.out.println(bd.getName() + ": " + bd.getObject());
}
You can use various other bindings like
- AttributeDefinition
- ClassDefinition
- SyntaxDefinition
Schema context may also provide bindings like
- MatchingRule
- ExtensionDefinition
- ControlDefinition
- SASLDefinition
You have to query the subschema subentry i.e cn=schema (the below code has been tested against the Apache Directory Server)
DirContext ctx = new InitialLdapContext( env, null );
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope( SearchControls.OBJECT_SCOPE );
searchControls.setReturningAttributes( new String[]
{ "objectClasses" } );
NamingEnumeration<SearchResult> results = ctx.search( "cn=schema", "(ObjectClass=*)", searchControls );
SearchResult result = results.next();
Attributes entry = result.getAttributes();
Attribute objectClasses = entry.get( "objectClasses" );
System.out.println( objectClasses );
精彩评论