Java Code Examples for org.apache.directory.shared.ldap.entry.ServerEntry#add()

The following examples show how to use org.apache.directory.shared.ldap.entry.ServerEntry#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApacheDirectoryPartitionManager.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private static void addObjectClasses(ServerEntry serverEntry, List<String> objectClasses)
        throws DirectoryServerException {

    for (String objectClass : objectClasses) {
        try {
            serverEntry.add("objectClass", objectClass);
        } catch (LdapException e) {
            throwDirectoryServerException("Could not add class to partition " +
                    serverEntry.getDn().getName(), e);
        }
    }
}
 
Example 2
Source File: ApacheDirectoryPartitionManager.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private void addPartitionAttributes(String partitionDN, List<String> objectClasses,
                                    String realm, String dc)
        throws DirectoryServerException {

    try {
        DN adminDN = new DN(partitionDN);
        ServerEntry serverEntry = this.directoryService.newEntry(adminDN);

        addObjectClasses(serverEntry, objectClasses);

        serverEntry.add("o", realm);

        if (dc == null) {
            logger.warn("Domain component not found for partition with DN - " + partitionDN +
                    ". Not setting domain component.");
        } else {
            serverEntry.add("dc", dc);
        }

        addAccessControlAttributes(serverEntry);

        this.directoryService.getAdminSession().add(serverEntry);

    } catch (Exception e) {

        String msg = "Could not add partition attributes for partition - " + partitionDN;
        throwDirectoryServerException(msg, e);
    }

}
 
Example 3
Source File: LdapTestServer.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the server. It creates the partition, injects the context
 * entries for the created partitions, and loads an LDIF file (
 * {@link #ldifLoadFile}) for initial entries.
 *
 * @param workDir
 *          the directory to be used for storing the data
 * @throws Exception
 *           if there were some problems while initializing the system
 */
private void initDirectoryService(File workDir) throws Exception {
  // Initialize the LDAP service
  service = new DefaultDirectoryService();
  service.setWorkingDirectory(workDir);

  // first load the schema
  initSchemaPartition();

  // then the system partition
  // this is a MANDATORY partition
  Partition systemPartition = addPartition("system",
      ServerDNConstants.SYSTEM_DN);
  service.setSystemPartition(systemPartition);

  // create the partition for testing
  Partition testingPartition = addPartition("ldapTesting",
      "ou=ldapTesting,dc=pune,dc=gemstone,dc=com");

  // Disable the shutdown hook
  service.setShutdownHookEnabled(false);
  // Disable the ChangeLog system
  service.getChangeLog().setEnabled(false);
  service.setDenormalizeOpAttrsEnabled(true);

  // And start the service
  service.startup();

  // inject the entry for testing
  if (!service.getAdminSession().exists(testingPartition.getSuffixDn())) {
    DN dnTesting = new DN("ou=ldapTesting,dc=pune,dc=gemstone,dc=com");
    ServerEntry entryTesting = service.newEntry(dnTesting);
    entryTesting.add("objectClass", "top", "domain", "extensibleObject");
    entryTesting.add("dc", "pune");
    service.getAdminSession().add(entryTesting);
  }

  // load schema from LDIF
  if (ldifLoadFile != null) {
    LdifFileLoader ldifLoader = new LdifFileLoader(
        service.getAdminSession(), ldifLoadFile);
    int numLoaded = ldifLoader.execute();
    if (numLoaded <= 0) {
      throw new Exception(
          "Failed to load any entries from " + ldifLoadFile);
    } else {
      System.out.println(
          "LDAP loaded " + numLoaded + " entries from " + ldifLoadFile);
    }
  }
}
 
Example 4
Source File: AuthenticationTest.java    From codenvy with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Ups ldap test server & initializes the following directory structure:
 *
 * <pre>
 * dc=codenvy,dc=com
 *   ou=developers
 *     cn=mike
 *      -objectClass=inetOrgPerson
 *      -uid=user1
 *      -cn=mike
 *      -sn=mike
 *      -userPassword=sha(mike)
 *     cn=john
 *      -objectClass=inetOrgPerson
 *      -uid=user2
 *      -cn=john
 *      -sn=john
 *      -userPassword=sha(john)
 *   ou=managers
 *     cn=brad
 *      -objectClass=inetOrgPerson
 *      -uid=user3
 *      -cn=brad
 *      -sn=brad
 *      -userPassword=sha(brad)
 *     cn=ivan
 *      -objectClass=inetOrgPerson
 *      -uid=user4
 *      -cn=ivan
 *      -sn=ivan
 *      -userPassword=sha(ivan)
 * </pre>
 */
@BeforeMethod
public void startServer() throws Exception {
  server =
      EmbeddedLdapServer.builder()
          .setPartitionId("codenvy")
          .setPartitionDn("dc=codenvy,dc=com")
          .useTmpWorkingDir()
          .setMaxSizeLimit(1000)
          .build();
  server.start();

  // developers
  ServerEntry ouDevelopers = server.newEntry("ou", "developers");
  ouDevelopers.add("objectClass", "organizationalUnit");
  ouDevelopers.add("ou", "developers");
  server.addEntry(ouDevelopers);

  ServerEntry mike = server.newEntry("cn", "mike", ouDevelopers);
  mike.add("objectClass", "inetOrgPerson");
  mike.add("uid", "user1");
  mike.add("cn", "mike");
  mike.add("sn", "mike");
  mike.add("userPassword", encryptor.encrypt("mike".getBytes(UTF_8)));
  server.addEntry(mike);

  ServerEntry john = server.newEntry("cn", "john", ouDevelopers);
  john.add("objectClass", "inetOrgPerson");
  john.add("uid", "user2");
  john.add("cn", "john");
  john.add("sn", "john");
  john.add("userPassword", encryptor.encrypt("john".getBytes(UTF_8)));
  server.addEntry(john);

  // managers
  ServerEntry ouManagers = server.newEntry("ou", "managers");
  ouManagers.add("objectClass", "organizationalUnit");
  ouManagers.add("ou", "managers");
  server.addEntry(ouManagers);

  ServerEntry brad = server.newEntry("cn", "brad", ouManagers);
  brad.add("objectClass", "inetOrgPerson");
  brad.add("uid", "user3");
  brad.add("cn", "brad");
  brad.add("sn", "brad");
  brad.add("userPassword", encryptor.encrypt("brad".getBytes(UTF_8)));
  server.addEntry(brad);

  ServerEntry ivan = server.newEntry("cn", "ivan", ouManagers);
  ivan.add("objectClass", "inetOrgPerson");
  ivan.add("uid", "user4");
  ivan.add("cn", "ivan");
  ivan.add("sn", "ivan");
  ivan.add("userPassword", encryptor.encrypt("ivan".getBytes(UTF_8)));
  server.addEntry(ivan);
}
 
Example 5
Source File: LdapTestServer.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the server. It creates the partition, injects the context
 * entries for the created partitions, and loads an LDIF file (
 * {@link #ldifLoadFile}) for initial entries.
 *
 * @param workDir
 *          the directory to be used for storing the data
 * @throws Exception
 *           if there were some problems while initializing the system
 */
private void initDirectoryService(File workDir) throws Exception {
  // Initialize the LDAP service
  service = new DefaultDirectoryService();
  service.setWorkingDirectory(workDir);

  // first load the schema
  initSchemaPartition();

  // then the system partition
  // this is a MANDATORY partition
  Partition systemPartition = addPartition("system",
      ServerDNConstants.SYSTEM_DN);
  service.setSystemPartition(systemPartition);

  // create the partition for testing
  Partition testingPartition = addPartition("ldapTesting",
      "ou=ldapTesting,dc=pune,dc=gemstone,dc=com");

  // Disable the shutdown hook
  service.setShutdownHookEnabled(false);
  // Disable the ChangeLog system
  service.getChangeLog().setEnabled(false);
  service.setDenormalizeOpAttrsEnabled(true);

  // And start the service
  service.startup();

  // inject the entry for testing
  if (!service.getAdminSession().exists(testingPartition.getSuffixDn())) {
    DN dnTesting = new DN("ou=ldapTesting,dc=pune,dc=gemstone,dc=com");
    ServerEntry entryTesting = service.newEntry(dnTesting);
    entryTesting.add("objectClass", "top", "domain", "extensibleObject");
    entryTesting.add("dc", "pune");
    service.getAdminSession().add(entryTesting);
  }

  // load schema from LDIF
  if (ldifLoadFile != null) {
    LdifFileLoader ldifLoader = new LdifFileLoader(
        service.getAdminSession(), ldifLoadFile);
    int numLoaded = ldifLoader.execute();
    if (numLoaded <= 0) {
      throw new Exception(
          "Failed to load any entries from " + ldifLoadFile);
    } else {
      System.out.println(
          "LDAP loaded " + numLoaded + " entries from " + ldifLoadFile);
    }
  }
}
 
Example 6
Source File: ApacheDirectoryPartitionManager.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
private void addAccessControlAttributes(ServerEntry serverEntry)
        throws LdapException {
    serverEntry.add("administrativeRole", "accessControlSpecificArea");
}
 
Example 7
Source File: EmbeddedADS.java    From vertx-auth with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the server. It creates the partition, adds the index, and
 * injects the context entries for the created partitions.
 *
 * @param workDir the directory to be used for storing the data
 * @throws Exception if there were some problems while initializing the system
 */
private void initDirectoryService(File workDir) throws Exception {
  // Initialize the LDAP service
  service = new DefaultDirectoryService();
  service.setWorkingDirectory(workDir);

  // first load the schema
  initSchemaPartition();

  // then the system partition
  // this is a MANDATORY partition
  Partition systemPartition = addPartition("system", ServerDNConstants.SYSTEM_DN);
  service.setSystemPartition(systemPartition);

  // Disable the ChangeLog system
  service.getChangeLog().setEnabled(false);
  service.setDenormalizeOpAttrsEnabled(true);

  // Now we can create as many partitions as we need
  // Create some new partitions named 'foo', 'bar' and 'apache'.
  Partition fooPartition = addPartition("foo", "dc=foo,dc=com");

  // Index some attributes on the apache partition
  addIndex(fooPartition, "objectClass", "ou", "uid");

  // And start the service
  service.startup();

  DN dnFoo = new DN("dc=foo,dc=com");
  ServerEntry entryFoo = service.newEntry(dnFoo);
  entryFoo.add("objectClass", "top", "domain", "extensibleObject");
  entryFoo.add("dc", "foo");
  service.getAdminSession().add(entryFoo);

  DN usersDN=new DN("ou=users,dc=foo,dc=com");
  ServerEntry usersEntry=service.newEntry(usersDN);
  usersEntry.add("objectClass","organizationalUnit","top");
  usersEntry.add("ou","users");
  service.getAdminSession().add(usersEntry);

}
 
Example 8
Source File: EmbeddedLdapServer.java    From codenvy with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Creates a new group which matches default schema pattern, which is:
 *
 * <ul>
 *   <li>objectClass=groupOfNames
 *   <li>rdn - ou={name}
 *   <li>cn={name}
 *   <li>members.foreach(m -> member={m})
 * </ul>
 *
 * @param name a name of a group
 * @return newly created and added group entry
 * @throws Exception when any error occurs
 */
public ServerEntry addDefaultLdapGroup(String name, List<String> members) throws Exception {
  final ServerEntry group = newEntry("ou", name);
  group.put("objectClass", "top", "groupOfNames");
  group.put("cn", name);
  group.put("ou", name);
  for (String member : members) {
    group.add("member", member);
  }
  addEntry(group);
  return group;
}