Example usage for javax.naming.directory DirContext destroySubcontext

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

Introduction

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

Prototype

public void destroySubcontext(Name name) throws NamingException;

Source Link

Document

Destroys the named context and removes it from the namespace.

Usage

From source file:CreateCorbaSchema.java

/**
 * Add new attributes: corbaIor corbaRepositoryId
 *///from w  w  w .  j av a  2 s.  c  o m
protected void updateAttributes(DirContext attrRoot, String[] attrIDs) throws NamingException {

    /* Get rid of old attr IDs */
    for (int i = 0; i < attrIDs.length; i++) {
        attrRoot.destroySubcontext(attrIDs[i]);
    }

    /* Add new and updated attr definitions */
    // corbaIor
    Attributes attrs = new BasicAttributes(true); // ignore case
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.14");
    attrs.put("NAME", "corbaIor");
    attrs.put("DESC", "Stringified interoperable object reference of a CORBA object");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.26");
    attrs.put("EQUALITY", "caseIgnoreIA5Match");
    attrs.put("SINGLE-VALUE", "true");
    attrRoot.createSubcontext("corbaIor", attrs);
    System.out.println("Created corbaIor attribute");

    // corbaRepositoryId
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.15");
    attrs.put("NAME", "corbaRepositoryId");
    attrs.put("DESC", "Repository ids of interfaces implemented by a CORBA object");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
    attrs.put("EQUALITY", "caseExactMatch");
    attrRoot.createSubcontext("corbaRepositoryId", attrs);
    System.out.println("Created corbaRepositoryId attribute");
}

From source file:CreateCorbaSchema.java

protected void updateObjectClasses(DirContext ocRoot, String[] ocIDs) throws NamingException {

    /* Get rid of old OCs - reverse order */
    for (int i = ocIDs.length - 1; i >= 0; i--) {
        ocRoot.destroySubcontext(ocIDs[i]);
    }/*from ww  w. j a  v  a  2  s  .  c om*/

    // corbaObject
    Attributes attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.9");
    attrs.put("NAME", "corbaObject");
    attrs.put("DESC", "CORBA object representation");
    attrs.put("SUP", "top");
    attrs.put("ABSTRACT", "true");
    Attribute optional = new BasicAttribute("MAY", "corbaRepositoryId");
    optional.add("description");
    attrs.put(optional);
    ocRoot.createSubcontext("corbaObject", attrs);
    System.out.println("Created corbaObject object class");

    // corbaObjectReference
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.11");
    attrs.put("NAME", "corbaObjectReference");
    attrs.put("DESC", "CORBA interoperable object reference");
    attrs.put("SUP", "corbaObject");
    attrs.put("AUXILIARY", "true");
    Attribute corMust = new BasicAttribute("MUST", "corbaIor");

    if (netscape41bug) {
        corMust.add("objectclass");
    }

    if (netscapebug) {
        // Netscape ignores 'SUP' so we must add explicitly
        attrs.put(optional);
    }
    attrs.put(corMust);
    ocRoot.createSubcontext("corbaObjectReference", attrs);
    System.out.println("Created corbaObjectReference object class");

    // corbaContainer
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.10");
    attrs.put("NAME", "corbaContainer");
    attrs.put("DESC", "Container for a CORBA object");
    attrs.put("SUP", "top");
    attrs.put("STRUCTURAL", "true");
    Attribute ccMust = new BasicAttribute("MUST", "cn");

    if (netscape41bug) {
        ccMust.add("objectclass");
    }

    attrs.put(ccMust);
    ocRoot.createSubcontext("corbaContainer", attrs);
    System.out.println("Created corbaContainer object class");
}

From source file:org.nuxeo.ecm.directory.ldap.LDAPDirectoryTestCase.java

protected void destroyRecursively(String dn, DirContext ctx, int limit) throws NamingException {
    if (limit == 0) {
        log.warn("Reach recursion limit, stopping deletion at" + dn);
        return;//from ww w .j a  v a 2s  .co m
    }
    SearchControls scts = new SearchControls();
    scts.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    String providerUrl = (String) ctx.getEnvironment().get(Context.PROVIDER_URL);
    NamingEnumeration<SearchResult> children = ctx.search(dn, "(objectClass=*)", scts);
    try {
        while (children.hasMore()) {
            SearchResult child = children.next();
            String subDn = child.getName();
            if (!USE_EXTERNAL_TEST_LDAP_SERVER && subDn.endsWith(providerUrl)) {
                subDn = subDn.substring(0, subDn.length() - providerUrl.length() - 1);
            } else {
                subDn = subDn + ',' + dn;
            }
            destroyRecursively(subDn, ctx, limit);
        }
    } catch (SizeLimitExceededException e) {
        log.warn("SizeLimitExceededException: trying again on partial results " + dn);
        if (limit == -1) {
            limit = 100;
        }
        destroyRecursively(dn, ctx, limit - 1);
    }
    ctx.destroySubcontext(dn);
}

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

protected void deleteLDAPRole(RoleContext context) throws UserStoreException {

    String roleName = context.getRoleName();
    String groupSearchFilter = ((LDAPRoleContext) context).getSearchFilter();
    groupSearchFilter = groupSearchFilter.replace("?", escapeSpecialCharactersForFilter(context.getRoleName()));
    String[] returningAttributes = { ((LDAPRoleContext) context).getRoleNameProperty() };
    String searchBase = ((LDAPRoleContext) context).getSearchBase();

    DirContext mainDirContext = null;
    DirContext groupContext = null;
    NamingEnumeration<SearchResult> groupSearchResults = null;

    try {/* w w  w  .  ja v a 2 s . c o m*/

        mainDirContext = this.connectionSource.getContext();
        groupSearchResults = searchInGroupBase(groupSearchFilter, returningAttributes,
                SearchControls.SUBTREE_SCOPE, mainDirContext, searchBase);
        SearchResult resultedGroup = null;
        while (groupSearchResults.hasMoreElements()) {
            resultedGroup = groupSearchResults.next();
        }

        if (resultedGroup == null) {
            throw new UserStoreException("Could not find specified group/role - " + roleName);
        }

        String groupName = resultedGroup.getName();

        groupContext = (DirContext) mainDirContext.lookup(groupSearchBase);
        String groupNameAttributeValue = (String) resultedGroup.getAttributes()
                .get(realmConfig.getUserStoreProperty(LDAPConstants.GROUP_NAME_ATTRIBUTE)).get();
        if (groupNameAttributeValue.equals(roleName)) {
            groupContext.destroySubcontext(groupName);
        }
    } catch (NamingException e) {
        String errorMessage = "Error occurred while deleting the role: " + roleName;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeNamingEnumeration(groupSearchResults);
        JNDIUtil.closeContext(groupContext);
        JNDIUtil.closeContext(mainDirContext);
    }

}

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

@SuppressWarnings("deprecation")
@Override//from   w  w  w.  ja  va 2  s. c  o m
public void doDeleteUser(String userName) throws UserStoreException {

    boolean debug = log.isDebugEnabled();

    if (debug) {
        log.debug("Deleting user: " + userName);
    }
    // delete user from LDAP group if read-write enabled.
    String userNameAttribute = realmConfig.getUserStoreProperty(LDAPConstants.USER_NAME_ATTRIBUTE);
    String searchFilter = realmConfig.getUserStoreProperty(LDAPConstants.USER_NAME_SEARCH_FILTER);
    searchFilter = searchFilter.replace("?", escapeSpecialCharactersForFilter(userName));
    String[] returningUserAttributes = new String[] { userNameAttribute };

    DirContext mainDirContext = this.connectionSource.getContext();

    NamingEnumeration<SearchResult> userResults = searchInUserBase(searchFilter, returningUserAttributes,
            SearchControls.SUBTREE_SCOPE, mainDirContext);
    NamingEnumeration<SearchResult> groupResults = null;

    DirContext subDirContext = null;
    try {
        SearchResult userResult = null;
        String userDN = null;
        // here we assume only one user
        // TODO: what to do if there are more than one user
        while (userResults.hasMore()) {
            userResult = userResults.next();
            userDN = userResult.getName();
            log.debug("User DN: " + userDN);
        }

        // LDAP roles of user to delete the mapping

        List<String> roles = new ArrayList<String>();
        String[] externalRoles = doGetExternalRoleListOfUser(userName, "*");
        roles.addAll(Arrays.asList(externalRoles));
        if (isSharedGroupEnabled()) {
            String[] sharedRoles = doGetSharedRoleListOfUser(null, userName, "*");
            if (sharedRoles != null) {
                roles.addAll(Arrays.asList(sharedRoles));
            }
        }
        String[] rolesOfUser = roles.toArray(new String[roles.size()]);

        if (rolesOfUser.length != 0) {

            String[] returningGroupAttributes = new String[] {
                    realmConfig.getUserStoreProperty(LDAPConstants.MEMBERSHIP_ATTRIBUTE) };
            for (String role : rolesOfUser) {

                RoleContext context = createRoleContext(role);
                String searchBase = ((LDAPRoleContext) context).getSearchBase();
                searchFilter = ((LDAPRoleContext) context).getSearchFilter();
                role = context.getRoleName();

                if (role.indexOf("/") > -1) {
                    role = (role.split("/"))[1];
                }
                String grpSearchFilter = searchFilter.replace("?", escapeSpecialCharactersForFilter(role));
                groupResults = searchInGroupBase(grpSearchFilter, returningGroupAttributes,
                        SearchControls.SUBTREE_SCOPE, mainDirContext, searchBase);
                SearchResult groupResult = null;
                while (groupResults.hasMore()) {
                    groupResult = groupResults.next();
                }
                if (isOnlyUserInRole(userDN, groupResult) && !emptyRolesAllowed) {
                    String errorMessage = "User: " + userName + " is the only user " + "in " + role + "."
                            + "There should be at " + "least one user" + " in the role. Hence can"
                            + " not delete the user.";
                    throw new UserStoreException(errorMessage);
                }
            }
            // delete role list
            doUpdateRoleListOfUser(userName, rolesOfUser, new String[] {});
        }

        // delete user entry if it exist
        if (userResult != null && userResult.getAttributes().get(userNameAttribute).get().toString()
                .toLowerCase().equals(userName.toLowerCase())) {
            if (log.isDebugEnabled()) {
                log.debug("Deleting " + userDN + " with search base " + userSearchBase);
            }
            subDirContext = (DirContext) mainDirContext.lookup(userSearchBase);
            subDirContext.destroySubcontext(userDN);
        }
        userCache.remove(userName);
    } catch (NamingException e) {
        String errorMessage = "Error occurred while deleting the user : " + userName;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeNamingEnumeration(groupResults);
        JNDIUtil.closeNamingEnumeration(userResults);

        JNDIUtil.closeContext(subDirContext);
        JNDIUtil.closeContext(mainDirContext);
    }
}