org.apache.directory.shared.ldap.name.DN Java Examples

The following examples show how to use org.apache.directory.shared.ldap.name.DN. 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: EmbeddedLdapServer.java    From codenvy with Eclipse Public License 1.0 6 votes vote down vote up
public EmbeddedLdapServer(
    File workingDir,
    String partitionDn,
    String partitionId,
    int port,
    boolean enableChangelog,
    boolean allowAnonymousAccess,
    long maxSizeLimit)
    throws Exception {
  requireNonNull(partitionDn, "Required non-null partition dn");
  requireNonNull(partitionId, "Required non-null partition id");
  this.workingDir = workingDir;
  this.baseDn = new DN(partitionDn);
  this.port = port > 0 ? port : PORT_SERVICE.acquire();
  this.url = "ldap://localhost:" + this.port;
  ldapServer = new LdapServer();
  ldapServer.setTransports(new TcpTransport(this.port));
  if (maxSizeLimit > 0) {
    ldapServer.setMaxSizeLimit(maxSizeLimit);
  }
  service =
      initDirectoryService(
          workingDir, partitionId, partitionDn, enableChangelog, allowAnonymousAccess);
  ldapServer.setDirectoryService(service);
}
 
Example #2
Source File: EmbeddedADS.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
/**
 * Main class.
 *
 * @param args Not used.
 */
public static void main(String[] args) {
  try {
    File workDir = new File(System.getProperty("java.io.tmpdir") + "/server-work/" + UUID.randomUUID().toString());
    workDir.mkdirs();

    // Create the server
    EmbeddedADS ads = new EmbeddedADS(workDir);

    // Read an entry
    //Entry result = ads.service.getAdminSession().lookup(new DN("dc=foo,dc=com"));

    Entry result = ads.service.getAdminSession().lookup(new DN("ou=users,dc=foo,dc=com"));

    // And print it if available
    System.out.println("Found entry : " + result);

    // optionally we can start a server too
    ads.startServer();
  } catch (Exception e) {
    // Ok, we have something wrong going on ...
    e.printStackTrace();
  }
}
 
Example #3
Source File: LdapTestServer.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void addAttribute(String dn, String attrName, String attrValue)
    throws Exception {
  EntryAttribute attr = new DefaultClientAttribute(attrName, attrValue);
  Modification addValue = new ClientModification(
      ModificationOperation.ADD_ATTRIBUTE, attr);
  service.getAdminSession().modify(new DN(dn),
      Collections.singletonList(addValue));
}
 
Example #4
Source File: LdapTestServer.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void removeAttribute(String dn, String attrName, String attrValue)
    throws Exception {
  EntryAttribute attr = new DefaultClientAttribute(attrName, attrValue);
  Modification removeValue = new ClientModification(
      ModificationOperation.REMOVE_ATTRIBUTE, attr);
  service.getAdminSession().modify(new DN(dn),
      Collections.singletonList(removeValue));
}
 
Example #5
Source File: LdapTestServer.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void addAttribute(String dn, String attrName, String attrValue)
    throws Exception {
  EntryAttribute attr = new DefaultClientAttribute(attrName, attrValue);
  Modification addValue = new ClientModification(
      ModificationOperation.ADD_ATTRIBUTE, attr);
  service.getAdminSession().modify(new DN(dn),
      Collections.singletonList(addValue));
}
 
Example #6
Source File: LdapTestServer.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void removeAttribute(String dn, String attrName, String attrValue)
    throws Exception {
  EntryAttribute attr = new DefaultClientAttribute(attrName, attrValue);
  Modification removeValue = new ClientModification(
      ModificationOperation.REMOVE_ATTRIBUTE, attr);
  service.getAdminSession().modify(new DN(dn),
      Collections.singletonList(removeValue));
}
 
Example #7
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 #8
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 #9
Source File: EmbeddedLdapServer.java    From codenvy with Eclipse Public License 1.0 4 votes vote down vote up
public ServerEntry newEntry(String name, String value, ServerEntry parent) throws Exception {
  return service.newEntry(new DN(name + '=' + value + ',' + parent.getDn()));
}
 
Example #10
Source File: EmbeddedLdapServer.java    From codenvy with Eclipse Public License 1.0 4 votes vote down vote up
/** Removes an entity with rdn {name}={value} in base dn. */
public void removeEntry(String name, String value) throws Exception {
  service.getAdminSession().delete(new DN(name + '=' + value + ',' + baseDn));
}
 
Example #11
Source File: EmbeddedLdapServer.java    From codenvy with Eclipse Public License 1.0 4 votes vote down vote up
/** Applies given modifications on the entity with rdn {rdnKey}={rdnValue} in base dn. */
public void modify(String rdnKey, String rdnValue, Modification... mods) throws Exception {
  service
      .getAdminSession()
      .modify(new DN(rdnKey + '=' + rdnValue + ',' + baseDn), Arrays.asList(mods));
}
 
Example #12
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 #13
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 #14
Source File: EmbeddedLdapServer.java    From codenvy with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new entry in base dn.
 *
 * <p>E.g. if {@code base_dn} is set to <i>dc=codenvy,dc=com</i> for {@code name=cn} and {@code
 * value=admin} the entity dn will be <i>cn=admin,dc=codenvy,dc=com</i>.
 *
 * <p>To add the entity attributes to directory service use {@link #addEntry(ServerEntry)}.
 *
 * @param name the name of the dn attribute e.g. 'cn'
 * @param value the value of the attribute e.g. 'admin'
 * @return a new instance of {@link ServerEntry}
 * @throws Exception when any error occurs
 */
public ServerEntry newEntry(String name, String value) throws Exception {
  return service.newEntry(new DN(name + '=' + value + ',' + baseDn.toString()));
}