The following example uses the standard edition of the UnboundID LDAP SDK to add an entry using LDIF. The standard edition of the LDAP SDK is free and the source code is supplied.
Using the LDAP ADD request:
- The distinguished name must not already exist
- The immediate superior of the distinguished name must exist
- The server will not dereference aliases
Example Code
package samplecode.add;
import com.unboundid.ldap.sdk.AddRequest;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPConnectionOptions;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldif.LDIFException;
/** demonstrate the {@link AddRequest} */
public final class AddExample
{
/**
* demonstrate the {@link AddRequest}
*
* @throws LDIFException
* @throws LDAPException
*/
public static void main(final String... args) throws LDAPException,LDIFException
{
/*
* Use connection options to specify that the connection attempt
* should be 1 second and if the ADD request times out, the request
* should be abandoned
*/
final LDAPConnectionOptions connectionOptions =
new LDAPConnectionOptions();
connectionOptions.setAbandonOnTimeout(true);
int connectionTimeoutMillis = 1000;
connectionOptions.setConnectTimeoutMillis(connectionTimeoutMillis);
/*
* LDIF lines for the add request
*/
final String[] ldifLines =
{
"dn: uid=user,dc=example,dc=com",
"changetype: add",
"cn: Joe User",
"sn: User",
"uid: user",
"userPassword: password"
};
final String host = "ldap.example.com";
final int port = 389;
LDAPConnection ldapConnection =
new LDAPConnection(connectionOptions,host,port);
ldapConnection.add(new AddRequest(ldifLines));
ldapConnection.close();
}
}
See Also
- UnboundID LDAP SDK
- The section of RFC4511 that defines ADD requests and ADD results