Based on this I use following code to start in memory ldap server
// Create the configuration to use for the server.
InMemoryDirectoryServerConfig config =
new InMemoryDirectoryServerConfig("dc=example,dc=com");
config.addAdditionalBindCredentials("cn=Directory Manager", "password");
config.setSchema(null); //!!! without this line I get an error thanks for https://stackoverflow.com/a/17921875/2674303
// Create the directory server instance, populate it with data from the
// "test-data.ldif" file, and start listening for client connections.
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
ds.importFromLDIF(true, "test-data.ldif");
ds.startListening();
// Get a client connection to the server and use it to perform various
// operations.
LDAPConnection conn = ds.getConnection();
SearchResultEntry entry = conn.getEntry("dc=example,dc=com");
// Do more stuff here....
// Disconnect from the server and cause the server to shut down.
conn.close();
ds.shutDown(true);
I want to import structure from exsiting ldap server.
I use apach directory studio for that:
I save the content to the file with name test-data.ldif
And when I run the code above I get an error:
Exception in thread "main" LDAPException(resultCode=32 (no such object), errorMessage='Unable to add entry 'CN=8437C3D8-7689-4200-BF38-79E4AC33DFA0,CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com' because its parent entry 'CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com' does not exist in the server.', matchedDN='DC=example,DC=com', ldapSDKVersion=6.0.9, revision=42839ddf0d77d954805fbbe3cce73a792af40474)
I've found out that the root cause that entry with dn
CN=8437C3D8-7689-4200-BF38-79E4AC33DFA0,CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com
is the first entry in the exported file and if to rearrange entries manually to make sure that all parents are created above - export will be successful. So for that entry we should place defiition of entry
DC=example,DC=com
then
CN=System,DC=example,DC=com
then
CN=DomainUpdates,CN=System,DC=example,DC=com
then
CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com
and finally
CN=8437C3D8-7689-4200-BF38-79E4AC33DFA0,CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com'
So I neded either have feature in unboundId ldap Sdk to be a bit more intelligent or have a feature in apache directory studio to export parents first.