tags:

views:

65

answers:

1

Hi, I have created an attribute in LDAP using the following code.

attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.3.1.1.9");
            attrs.put("NAME", "myattribute");
            attrs.put("DESC", "for JNDITutorial example only");
            attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
            DirContext schema = context.getSchema("");
           DirContext newAttr = schema.createSubcontext  ("AttributeDefinition/myattribute1", attrs);   

The attribute is created successfully, Now I am trying to add this attribute to a user say "user1"(uid).

Attributes attributeslist = context.getAttributes(ld.getUserDN(username));
            attributeslist.put("myattribute1", "");
            context.modifyAttributes(ld.getUserDN("test5"), DirContext.REPLACE_ATTRIBUTE, attributeslist);

But it gives me object class violation error.

Can anyone help me to solve this? I need to add an user defined attribut to the user using java code.

A: 

You create the attribute, then you choose a class to which it should belong, and update the class.

You could modify a base class (comes with the LDAP servers base schema), an effective class (a class of object that can exist on its own, like inetOrgPerson), or an auxiliary class (cannot make an object of this type by itself, but it extends another object class).

So define an aux class to 'hold' your new attribute, then add an entry to the Object Class of your target object, with the name of your class, and then you can add the attribute to the user.

We do this all the time.

geoffc
Hi geoffc, First of all I would like to thank you for your help. I still face a problem in adding that to perticular schema object. I am using the below code to add DirContext personSchema = (DirContext)schema.lookup("ClassDefinition/inetOrgPerson"); attributeslist.put("myattribute3", "test"); personSchema.modifyAttributes(ld.getUserDN("test5"), DirContext.ADD_ATTRIBUTE, attributeslist); But I am getting "name not found exception". Even if I tried with "createSubcontext" method there also I am getting the same error. can you help me to solve this?
Arnab Das
This is interesting, are you extending the schema in code? Usually I see this done via an LDIF file imported, at install time.
geoffc