Java Code Examples for javax.naming.directory.DirContext#rename()

The following examples show how to use javax.naming.directory.DirContext#rename() . 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: LdapDao.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public void move(final DirContext ctx, final T obj, final String newOrganizationalUnit) throws NamingException
{
  final Object id = getId(obj);
  // The dn is may-be changed, so find the original dn by id:
  final T origObject = findById(id, obj.getOrganizationalUnit());
  if (origObject == null) {
    throw new RuntimeException("Object with id "
        + id
        + " not found in search base '"
        + StringHelper.listToString(",", obj.getOrganizationalUnit())
        + "'. Can't move the object: "
        + obj);
  }
  final String ou = LdapUtils.getOrganizationalUnit(newOrganizationalUnit);
  final String origOu = LdapUtils.getOu(origObject.getOrganizationalUnit());
  if (StringUtils.equals(origOu, ou) == false) {
    log.info("Move object with id '" + obj.getId() + "' from '" + origOu + "' to '" + ou);
    final String dnIdentifier = buildDnIdentifier(obj);
    ctx.rename(dnIdentifier + "," + origOu, dnIdentifier + "," + ou);
  }
}
 
Example 2
Source File: LdapDao.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public void rename(final DirContext ctx, final T obj, final T oldObj) throws NamingException
{
  final String newDnIdentifier = buildDnIdentifier(obj);
  final String oldDnIdentifier = buildDnIdentifier(oldObj);
  if (StringUtils.equals(newDnIdentifier, oldDnIdentifier) == true) {
    // Nothing to rename.
    return;
  }
  final Object id = getId(obj);
  // The dn is may-be changed, so find the original dn by id:
  final T origObject = findById(id, obj.getOrganizationalUnit());
  if (origObject == null) {
    throw new RuntimeException("Object with id "
        + id
        + " not found in search base '"
        + StringHelper.listToString(",", obj.getOrganizationalUnit())
        + "'. Can't rename the object: "
        + obj);
  }
  final String ou = LdapUtils.getOu(origObject.getOrganizationalUnit());
  log.info("Rename object with id '" + obj.getId() + "' from '" + oldDnIdentifier + "' to '" + newDnIdentifier);
  ctx.rename(oldDnIdentifier + "," + ou, newDnIdentifier + "," + ou);
}
 
Example 3
Source File: ldapConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * modifies the dn to the one provided by the attributes.
 * No attempt is made to verify the attributes are valid i.e
 * in the format dn = ....
 */

public void modifyDN() throws NamingException{
  DirContext ctx = new InitialDirContext(env);
  ctx.rename(dn, attributes[0]);
  ctx.close();

}
 
Example 4
Source File: LdifScript.java    From scriptella-etl with Apache License 2.0 5 votes vote down vote up
/**
 * Adds/modifies ctx using entry information.
 *
 * @param ctx directory context to use for change.
 * @param e   entry with change description.
 * @throws NamingException if operation with directory failed.
 */
static void modify(DirContext ctx, final Entry e) throws NamingException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Processing " + e);
    }
    Attributes atts = e.getAttributes();
    final String rootDn = ctx.getNameInNamespace();
    if (atts != null) { //If add entry
        ctx.createSubcontext(getRelativeDN(rootDn, e.getDn()), e.getAttributes());
    } else if (e.isChangeDelete()) {
        ctx.destroySubcontext(getRelativeDN(rootDn, e.getDn()));
    } else if (e.isChangeModDn() || e.isChangeModRdn()) {
        Name newRdn;
        if (e.getNewSuperior() != null) { //If new superior
            newRdn = getRelativeDN(rootDn, e.getNewSuperior());
        } else { //otherwise use DN as a base
            newRdn = getRelativeDN(rootDn, e.getDn());
            newRdn.remove(newRdn.size() - 1);
        }
        newRdn.add(e.getNewRdn());
        ctx.addToEnvironment("java.naming.ldap.deleteRDN", String.valueOf(e.isDeleteOldRdn()));
        ctx.rename(getRelativeDN(rootDn, e.getDn()), newRdn);
        ctx.removeFromEnvironment("java.naming.ldap.deleteRDN");//a better solution to use the previous value

    } else {
        List<ModificationItem> items = e.getModificationItems();
        ctx.modifyAttributes(getRelativeDN(rootDn, e.getDn()),
                items.toArray(new ModificationItem[items.size()]));
    }
}
 
Example 5
Source File: LdapUtil.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 重命名节点
 * 
 * @param oldDN
 * @param newDN
 * @return
 */
public static boolean renameEntry(String oldDN, String newDN, DirContext dc) {
	try {
		dc.rename(oldDN, newDN);
		return true;
	} catch (NamingException ne) {
		System.err.println("Error: " + ne.getMessage());
		return false;
	}
}