Steven Markey
Steven Markey

Reputation: 21

How to set the multiple attributes of objectClass for UnboundID & OpenLDAP via Java 7

I'm not sure how to properly pass the multiple attributes needed for an OpenLDAP insert via UnboundID. I have omitted the objectClass attributes & received a "no objectClass" error. I have also tried comma-separated & the bracket/array route like below & received the "value #0 invalid per syntax" error.

String[] ldifLines = {"dn: ou=users,dc=sub,dc=domain,dc=com", "cn: " + uid, "userPassword: " + pw, "description: user", "uidNumber: " + lclDT, "gidNumber: 504", "uid: " + uid, "homeDirectory: " + File.separator + "home" + File.separator + this.getStrippedUser(), "objectClass: {posixAccount, top}"};
LDAPResult ldapResult = lclLC.add(new AddRequest(ldifLines));

So, the question is, how do I successfully pass these objectClass attributes in the string array included above? Again, I have tried: "objectClass: top, posixAccount" as well. Thanks in advance!

Upvotes: 2

Views: 1302

Answers (1)

Neil Wilson
Neil Wilson

Reputation: 1736

It uses an LDIF representation, so if an attribute has multiple values, then the attribute appears multiple times. Like:

 String[] ldifLines = 
 {
   "dn: ou=users,dc=sub,dc=domain,dc=com",
   "objectClass: top",
   "objectClass: posixAccount"
   "cn: " + uid,
   "userPassword: " + pw,
   "description: user",
   "uidNumber: " + lclDT,
   "gidNumber: 504",
   "uid: " + uid,
   "homeDirectory: " + File.separator + "home" +
        File.separator + this.getStrippedUser(),
 };
 LDAPResult ldapResult = lclLC.add(new AddRequest(ldifLines));

Also, the LDAP SDK allows you to use a shortcut and just do it in a single call without the need to create the array or the AddRequest object, like:

 LDAPResult ldapResult = lclLC.add(
      "dn: ou=users,dc=sub,dc=domain,dc=com",
      "objectClass: top",
      "objectClass: posixAccount"
      "cn: " + uid,
      "userPassword: " + pw,
      "description: user",
      "uidNumber: " + lclDT,
      "gidNumber: 504",
      "uid: " + uid,
      "homeDirectory: " + File.separator + "home" +
           File.separator + this.getStrippedUser());

Upvotes: 3

Related Questions