Uploaded image for project: 'Directory Client API'
  1. Directory Client API
  2. DIRAPI-191

How to get attributes list according to objectClass

    Details

    • Type: Question
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.0.0-M21
    • Fix Version/s: 1.0.0-M32
    • Labels:
      None

      Description

      I want to get attributes list according to specified objectclass, how to achieve this purpose using Apahce Directory API? Could anyone share sample code here?

        Activity

        Hide
        hxbon1y Robert Hou added a comment -

        Especially when DS is not ApahceDS, how to do?

        Show
        hxbon1y Robert Hou added a comment - Especially when DS is not ApahceDS, how to do?
        Hide
        elecharny Emmanuel Lecharny added a comment -

        There is no simple way to do that.

        All the information is available in at least two places, now the format is really dependent on the server you are using.
        In apacheDS, you can read the schema from cn=schema, or from ou=schema. They offer a different vision of the same thing.

        ou=schema is the static schema, containing every elements that the server knows about, but which are not necessary activated (ie, you may not use all of them). It's ApacheDS specific

        cn=schema contains the information about the available elements (ie, those you can use)

        In any case, if you want to get the list of attributes for a given object class, you first have to load the entry associated to this object class and read the m-must and m-may attributes (they contain the list of attributes you can use in an ObjectClass). But this is far from being enough : you have to do so for every ObjectClass the current objectClass extends... And you may have a lot !

        This is also very specific to ApacheDS.

        Another way, which is supposed to be working with any server, would be to load the cn=schema entry, which contains the list of all the ObjectClasses in the objectClasses attribute. You will get the result in a format you will have to parse :
        ( 0.9.2342.19200300.100.4.18 NAME 'friendlyCountry' SUP country STRUCTURAL MUST co X-SCHEMA 'cosine' )

        You can use the ObjectClassDescriptionSchemaParser.parse( String ) method to parse such an ObjectClass attribute, it will return an instance of teh ObjectClass java class, and you'll again have to read the may and must values using the getMayAttributeTypes() and getMustAttributeTypes() methods. Also keep in mind to prcess the inherited ObjectClasses using the getSuperiors() method.

        Last, not least, the cn=schema entry might not be present at the top level : in LDAP, you may have more than one schema...

        Good luck

        Side Note : it took us months to implement the schema browser in Apache Directory Studio, for the exact same reasons plus some other I haven't describe here...

        Show
        elecharny Emmanuel Lecharny added a comment - There is no simple way to do that. All the information is available in at least two places, now the format is really dependent on the server you are using. In apacheDS, you can read the schema from cn=schema, or from ou=schema. They offer a different vision of the same thing. ou=schema is the static schema, containing every elements that the server knows about, but which are not necessary activated (ie, you may not use all of them). It's ApacheDS specific cn=schema contains the information about the available elements (ie, those you can use) In any case, if you want to get the list of attributes for a given object class, you first have to load the entry associated to this object class and read the m-must and m-may attributes (they contain the list of attributes you can use in an ObjectClass). But this is far from being enough : you have to do so for every ObjectClass the current objectClass extends... And you may have a lot ! This is also very specific to ApacheDS. Another way, which is supposed to be working with any server, would be to load the cn=schema entry, which contains the list of all the ObjectClasses in the objectClasses attribute. You will get the result in a format you will have to parse : ( 0.9.2342.19200300.100.4.18 NAME 'friendlyCountry' SUP country STRUCTURAL MUST co X-SCHEMA 'cosine' ) You can use the ObjectClassDescriptionSchemaParser.parse( String ) method to parse such an ObjectClass attribute, it will return an instance of teh ObjectClass java class, and you'll again have to read the may and must values using the getMayAttributeTypes() and getMustAttributeTypes() methods. Also keep in mind to prcess the inherited ObjectClasses using the getSuperiors() method. Last, not least, the cn=schema entry might not be present at the top level : in LDAP, you may have more than one schema... Good luck Side Note : it took us months to implement the schema browser in Apache Directory Studio, for the exact same reasons plus some other I haven't describe here...
        Hide
        seelmann Stefan Seelmann added a comment -

        The second approach Emmanuel described can be achieved using the LDAP API, here is just a quick hint.

                LdapConnection connection = ...;
                connection.loadSchema();
                SchemaManager schemaManager = connection.getSchemaManager();
        
                // Lookup up the attributes of the object class
                ObjectClass oc = schemaManager.getObjectClassRegistry().lookup( "inetOrgPerson" );
                System.out.println(oc.getMustAttributeTypeOids());
                System.out.println(oc.getMayAttributeTypeOids());
        
                // TODO: recursively loop over all superior object classes and lookup their attributes too...
                List<ObjectClass> superiors = oc.getSuperiors();
        
        Show
        seelmann Stefan Seelmann added a comment - The second approach Emmanuel described can be achieved using the LDAP API, here is just a quick hint. LdapConnection connection = ...; connection.loadSchema(); SchemaManager schemaManager = connection.getSchemaManager(); // Lookup up the attributes of the object class ObjectClass oc = schemaManager.getObjectClassRegistry().lookup( "inetOrgPerson" ); System .out.println(oc.getMustAttributeTypeOids()); System .out.println(oc.getMayAttributeTypeOids()); // TODO: recursively loop over all superior object classes and lookup their attributes too... List<ObjectClass> superiors = oc.getSuperiors();
        Hide
        elecharny Emmanuel Lecharny added a comment -

        Thanks Stefan. This is an option I forgot to mention...

        Show
        elecharny Emmanuel Lecharny added a comment - Thanks Stefan. This is an option I forgot to mention...
        Hide
        hxbon1y Robert Hou added a comment -

        Thanks Emmanuel and Stefan.I tested above code with AD Server and Apahce DS. It can work well with Aapche DS, but not work well with AD Server. With AD Server, connection.loadSchema() can' t get effective information on connection. looks like I need to do more action on it before I get the attributes.But I don't still know how to do.

        Show
        hxbon1y Robert Hou added a comment - Thanks Emmanuel and Stefan.I tested above code with AD Server and Apahce DS. It can work well with Aapche DS, but not work well with AD Server. With AD Server, connection.loadSchema() can' t get effective information on connection. looks like I need to do more action on it before I get the attributes.But I don't still know how to do.
        Hide
        elecharny Emmanuel Lecharny added a comment -

        We have added a loadSchemaRelaxed() that can be used to load a schema from a server which is not LDAP complient. That shoudl work for the user's need.

        Show
        elecharny Emmanuel Lecharny added a comment - We have added a loadSchemaRelaxed() that can be used to load a schema from a server which is not LDAP complient. That shoudl work for the user's need.
        Hide
        elecharny Emmanuel Lecharny added a comment -

        Should be fixed with the latest change where we can load schema in a relaxed mode.

        Show
        elecharny Emmanuel Lecharny added a comment - Should be fixed with the latest change where we can load schema in a relaxed mode.

          People

          • Assignee:
            Unassigned
            Reporter:
            hxbon1y Robert Hou
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development