How to show all objectclasses description of ldap

2020-07-13 07:58发布

问题:

I want to show all objectclasses present in schema of LDAP Directory to help user to input available objectclasses for adding new entry.

DirContext schema = ctx.getSchema("");
Attributes answer = schema.getAttributes("ClassDefinition/person");

but that shows information about person only.

回答1:

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 );


回答2:

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



标签: java ldap jndi