Example usage for javax.naming.directory DirContext unbind

List of usage examples for javax.naming.directory DirContext unbind

Introduction

In this page you can find the example usage for javax.naming.directory DirContext unbind.

Prototype

public void unbind(Name name) throws NamingException;

Source Link

Document

Unbinds the named object.

Usage

From source file:org.springframework.ldap.core.LdapTemplate.java

private void doUnbind(final String dn) {
    executeReadWrite(new ContextExecutor() {
        public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
            ctx.unbind(dn);
            return null;
        }/*from   ww w  . j a v a 2  s .  c  om*/
    });
}

From source file:org.springframework.ldap.core.LdapTemplate.java

/**
 * Delete all subcontexts including the current one recursively.
 * /*from   www .java  2 s  .  co m*/
 * @param ctx The context to use for deleting.
 * @param name The starting point to delete recursively.
 * @throws NamingException if any error occurs
 */
protected void deleteRecursively(DirContext ctx, DistinguishedName name) {

    NamingEnumeration enumeration = null;
    try {
        enumeration = ctx.listBindings(name);
        while (enumeration.hasMore()) {
            Binding binding = (Binding) enumeration.next();
            DistinguishedName childName = new DistinguishedName(binding.getName());
            childName.prepend((DistinguishedName) name);
            deleteRecursively(ctx, childName);
        }
        ctx.unbind(name);
        if (log.isDebugEnabled()) {
            log.debug("Entry " + name + " deleted");
        }
    } catch (javax.naming.NamingException e) {
        throw LdapUtils.convertLdapException(e);
    } finally {
        try {
            enumeration.close();
        } catch (Exception e) {
            // Never mind this
        }
    }
}

From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java

public void delete(Person person) {
    DirContext ctx = createAuthenticatedContext();
    String dn = buildDn(person);/*ww w.  jav  a 2s  .  co m*/
    try {
        ctx.unbind(dn);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
}

From source file:org.springframework.ldap.test.unboundid.LdapTestUtils.java

/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name./*from  ww w  .ja  v  a  2  s  .  com*/
 *
 * @param ctx  The DirContext to use for cleaning the tree.
 * @param name the distinguished name of the root node.
 * @throws NamingException if anything goes wrong removing the sub-tree.
 */
public static void clearSubContexts(DirContext ctx, Name name) throws NamingException {

    NamingEnumeration<?> enumeration = null;
    try {
        enumeration = ctx.listBindings(name);
        while (enumeration.hasMore()) {
            Binding element = (Binding) enumeration.next();
            Name childName = LdapUtils.newLdapName(element.getName());
            childName = LdapUtils.prepend(childName, name);

            try {
                ctx.unbind(childName);
            } catch (ContextNotEmptyException e) {
                clearSubContexts(ctx, childName);
                ctx.unbind(childName);
            }
        }
    } catch (NamingException e) {
        LOGGER.debug("Error cleaning sub-contexts", e);
    } finally {
        try {
            enumeration.close();
        } catch (Exception e) {
            // Never mind this
        }
    }
}

From source file:org.wso2.carbon.directory.server.manager.internal.LDAPServerStoreManager.java

public void deleteServicePrinciple(String serverName) throws DirectoryServerManagerException {

    DirContext dirContext;
    try {/* w  ww  . j  ava2  s.c  o  m*/
        dirContext = this.connectionSource.getContext();
    } catch (UserStoreException e) {
        throw new DirectoryServerManagerException("Unable to retrieve directory connection.", e);
    }

    String searchBase = this.realmConfiguration.getUserStoreProperty(LDAPConstants.USER_SEARCH_BASE);

    String userId = lookupUserId(serverName);

    if (userId == null) {
        throw new DirectoryServerManagerException(
                "Could not find user id for given server principle " + serverName);
    }

    try {
        dirContext = (DirContext) dirContext.lookup(searchBase);
        dirContext.unbind("uid=" + userId);

    } catch (NamingException e) {
        log.error("Could not remove service principle " + serverName, e);
        throw new DirectoryServerManagerException("Could not remove service principle " + serverName, e);
    } finally {
        try {
            JNDIUtil.closeContext(dirContext);
        } catch (UserStoreException e) {
            log.error("Unable to close directory context.", e);
        }
    }

}

From source file:org.wso2.carbon.user.core.ldap.ActiveDirectoryUserStoreManager.java

/**
 *
 */// ww  w . ja  va2s .c  o m
public void doAddUser(String userName, Object credential, String[] roleList, Map<String, String> claims,
        String profileName, boolean requirePasswordChange) throws UserStoreException {

    boolean isUserBinded = false;

    /* getting search base directory context */
    DirContext dirContext = getSearchBaseDirectoryContext();

    /* getting add user basic attributes */
    BasicAttributes basicAttributes = getAddUserBasicAttributes(userName);

    if (!isADLDSRole) {
        // creating a disabled user account in AD DS
        BasicAttribute userAccountControl = new BasicAttribute(
                LDAPConstants.ACTIVE_DIRECTORY_USER_ACCOUNT_CONTROL);
        userAccountControl.add(LDAPConstants.ACTIVE_DIRECTORY_DISABLED_NORMAL_ACCOUNT);
        basicAttributes.put(userAccountControl);
    }

    /* setting claims */
    setUserClaims(claims, basicAttributes, userName);

    Name compoundName = null;
    try {
        NameParser ldapParser = dirContext.getNameParser("");
        compoundName = ldapParser.parse("cn=" + escapeSpecialCharactersForDN(userName));

        /* bind the user. A disabled user account with no password */
        dirContext.bind(compoundName, null, basicAttributes);
        isUserBinded = true;

        /* update the user roles */
        doUpdateRoleListOfUser(userName, null, roleList);

        /* reset the password and enable the account */
        if (!isSSLConnection) {
            logger.warn("Unsecured connection is being used. Enabling user account operation will fail");
        }

        ModificationItem[] mods = new ModificationItem[2];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute(LDAPConstants.ACTIVE_DIRECTORY_UNICODE_PASSWORD_ATTRIBUTE,
                        createUnicodePassword((String) credential)));
        if (isADLDSRole) {
            mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    new BasicAttribute(LDAPConstants.ACTIVE_DIRECTORY_MSDS_USER_ACCOUNT_DISSABLED, "FALSE"));
        } else {
            mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(
                    LDAPConstants.ACTIVE_DIRECTORY_USER_ACCOUNT_CONTROL, userAccountControl));
        }
        dirContext.modifyAttributes(compoundName, mods);

    } catch (NamingException e) {
        String errorMessage = "Error while adding the user to the Active Directory for user : " + userName;
        if (isUserBinded) {
            try {
                dirContext.unbind(compoundName);
            } catch (NamingException e1) {
                errorMessage = "Error while accessing the Active Directory for user : " + userName;
                throw new UserStoreException(errorMessage, e);
            }
            errorMessage = "Error while enabling the user account. Please check password policy at DC for user : "
                    + userName;
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeContext(dirContext);
    }
}