Example usage for javax.naming.directory DirContext ADD_ATTRIBUTE

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

Introduction

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

Prototype

int ADD_ATTRIBUTE

To view the source code for javax.naming.directory DirContext ADD_ATTRIBUTE.

Click Source Link

Document

This constant specifies to add an attribute with the specified values.

Usage

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

/**
 * Collect all modifications for the changed attribute. If no changes have
 * been made, return immediately. If modifications have been made, and the
 * original size as well as the updated size of the attribute is 1, replace
 * the attribute. If the size of the updated attribute is 0, remove the
 * attribute. Otherwise, the attribute is a multi-value attribute; if it's
 * an ordered one it should be replaced in its entirety to preserve the new
 * ordering, if not all modifications to the original value (removals and
 * additions) will be collected individually.
 * // w ww.  j av a 2s.c  om
 * @param changedAttr the value of the changed attribute.
 * @param modificationList the list in which to add the modifications.
 * @throws NamingException if thrown by called Attribute methods.
 */
private void collectModifications(Attribute changedAttr, List modificationList) throws NamingException {
    Attribute currentAttribute = originalAttrs.get(changedAttr.getID());

    if (changedAttr.equals(currentAttribute)) {
        // No changes
        return;
    } else if (currentAttribute != null && currentAttribute.size() == 1 && changedAttr.size() == 1) {
        // Replace single-vale attribute.
        modificationList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, changedAttr));
    } else if (changedAttr.size() == 0 && currentAttribute != null) {
        // Attribute has been removed.
        modificationList.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE, changedAttr));
    } else if ((currentAttribute == null || currentAttribute.size() == 0) && changedAttr.size() > 0) {
        // Attribute has been added.
        modificationList.add(new ModificationItem(DirContext.ADD_ATTRIBUTE, changedAttr));
    } else if (changedAttr.size() > 0 && changedAttr.isOrdered()) {
        // This is a multivalue attribute and it is ordered - the original
        // value should be replaced with the new values so that the ordering
        // is preserved.
        modificationList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, changedAttr));
    } else if (changedAttr.size() > 0) {
        // Change of multivalue Attribute. Collect additions and removals
        // individually.
        List myModifications = new LinkedList();
        collectModifications(currentAttribute, changedAttr, myModifications);

        if (myModifications.isEmpty()) {
            // This means that the attributes are not equal, but the
            // actual values are the same - thus the order must have
            // changed. This should result in a REPLACE_ATTRIBUTE operation.
            myModifications.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, changedAttr));
        }

        modificationList.addAll(myModifications);
    }
}

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

private void collectModifications(Attribute originalAttr, Attribute changedAttr, List modificationList)
        throws NamingException {

    Attribute originalClone = (Attribute) originalAttr.clone();
    Attribute addedValuesAttribute = new BasicAttribute(originalAttr.getID());

    for (int i = 0; i < changedAttr.size(); i++) {
        Object attributeValue = changedAttr.get(i);
        if (!originalClone.remove(attributeValue)) {
            addedValuesAttribute.add(attributeValue);
        }// w w w  .  ja  v a2s  . c o  m
    }

    // We have now traversed and removed all values from the original that
    // were also present in the new values. The remaining values in the
    // original must be the ones that were removed.
    if (originalClone.size() > 0) {
        modificationList.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE, originalClone));
    }

    if (addedValuesAttribute.size() > 0) {
        modificationList.add(new ModificationItem(DirContext.ADD_ATTRIBUTE, addedValuesAttribute));
    }
}

From source file:org.swordess.ldap.util.ModUtils.java

public static ModificationItem add(String id, Object value) {
    return create(DirContext.ADD_ATTRIBUTE, id, value, null);
}

From source file:org.swordess.ldap.util.ModUtils.java

public static <T> ModificationItem add(String id, Object value, Evaluator<T> evaluator) {
    return create(DirContext.ADD_ATTRIBUTE, id, value, evaluator);
}

From source file:org.swordess.ldap.util.ModUtils.java

public static ModificationItem add(String id, Object[] values) {
    return create(DirContext.ADD_ATTRIBUTE, id, values, null);
}

From source file:org.swordess.ldap.util.ModUtils.java

public static <T> ModificationItem add(String id, Object[] values, Evaluator<T> evaluator) {
    return create(DirContext.ADD_ATTRIBUTE, id, values, evaluator);
}

From source file:org.swordess.ldap.util.ModUtils.java

public static ModificationItem add(String id, Collection<?> values) {
    return create(DirContext.ADD_ATTRIBUTE, id, values, null);
}

From source file:org.swordess.ldap.util.ModUtils.java

public static <T> ModificationItem add(String id, Collection<?> values, Evaluator<T> evaluator) {
    return create(DirContext.ADD_ATTRIBUTE, id, values, evaluator);
}

From source file:org.wso2.carbon.connector.ldap.UpdateEntry.java

@Override
public void connect(MessageContext messageContext) throws ConnectException {
    String attributesString = (String) getParameter(messageContext, LDAPConstants.ATTRIBUTES);
    String dn = (String) getParameter(messageContext, LDAPConstants.DN);
    String mode = (String) getParameter(messageContext, LDAPConstants.MODE);

    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, LDAPConstants.NAMESPACE);
    OMElement result = factory.createOMElement(LDAPConstants.RESULT, ns);
    OMElement message = factory.createOMElement(LDAPConstants.MESSAGE, ns);

    try {/*from w w  w  .  j  a  va  2 s.  co  m*/
        DirContext context = LDAPUtils.getDirectoryContext(messageContext);

        Attributes entry = new BasicAttributes();
        if (StringUtils.isNotEmpty(attributesString)) {
            JSONObject object = new JSONObject(attributesString);
            Iterator keys = object.keys();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                String val = object.getString(key);
                Attribute newAttr = new BasicAttribute(key);
                newAttr.add(val);
                entry.put(newAttr);
            }
        }

        try {
            if (mode.equals(LDAPConstants.REPLACE)) {
                context.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, entry);
            } else if (mode.equals(LDAPConstants.ADD)) {
                context.modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, entry);
            } else if (mode.equals(LDAPConstants.REMOVE)) {
                context.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, entry);
            }
            message.setText(LDAPConstants.SUCCESS);
            result.addChild(message);
            LDAPUtils.preparePayload(messageContext, result);
        } catch (NamingException e) { // LDAP Errors are catched
            LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.UPDATE_ENTRY_ERROR, e);
            throw new SynapseException(e);
        }
    } catch (NamingException e) { //Authentication failures are catched
        LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS, e);
        throw new SynapseException(e);
    } catch (JSONException e) {
        handleException("Error while passing the JSON object", e, messageContext);
    }
}

From source file:org.wso2.carbon.identity.agent.onprem.userstore.manager.ldap.LDAPUserStoreManager.java

@Override
public void doUpdateRoleListOfUser(String userName, String[] deletedRoles, String[] newRoles)
        throws UserStoreException {

    // get the DN of the user entry
    String userNameDN = this.getNameInSpaceForUserName(userName);
    String membershipAttribute = userStoreProperties.get(LDAPConstants.MEMBERSHIP_ATTRIBUTE);
    /*//from  w ww.ja v  a  2s  .  co  m
     * check deleted roles and delete member entries from relevant groups.
     */
    String errorMessage = null;
    String roleSearchFilter = null;

    DirContext mainDirContext = this.connectionSource.getContext();

    try {
        if (deletedRoles != null && deletedRoles.length != 0) {
            // perform validation for empty role occurrences before
            // updating in LDAP
            // check whether this is shared roles and where shared roles are
            // enable

            for (String deletedRole : deletedRoles) {
                String searchFilter = userStoreProperties.get(LDAPConstants.ROLE_NAME_FILTER);
                roleSearchFilter = searchFilter.replace("?", escapeSpecialCharactersForFilter(deletedRole));
                String[] returningAttributes = new String[] { membershipAttribute };
                String searchBase = userStoreProperties.get(LDAPConstants.GROUP_SEARCH_BASE);
                NamingEnumeration<SearchResult> groupResults = searchInGroupBase(roleSearchFilter,
                        returningAttributes, SearchControls.SUBTREE_SCOPE, mainDirContext, searchBase);
                SearchResult resultedGroup = null;
                if (groupResults.hasMore()) {
                    resultedGroup = groupResults.next();
                }
                if (resultedGroup != null && isOnlyUserInRole(userNameDN, resultedGroup)
                        && !emptyRolesAllowed) {
                    errorMessage = userName + " is the only user in the role: " + deletedRole
                            + ". Hence can not delete user from role.";
                    throw new UserStoreException(errorMessage);
                }

                JNDIUtil.closeNamingEnumeration(groupResults);
            }
            // if empty role violation does not happen, continue
            // updating the LDAP.
            for (String deletedRole : deletedRoles) {

                String searchFilter = userStoreProperties.get(LDAPConstants.ROLE_NAME_FILTER);

                if (doCheckExistingRole(deletedRole)) {
                    roleSearchFilter = searchFilter.replace("?", escapeSpecialCharactersForFilter(deletedRole));
                    String[] returningAttributes = new String[] { membershipAttribute };
                    String searchBase = userStoreProperties.get(LDAPConstants.GROUP_SEARCH_BASE);
                    NamingEnumeration<SearchResult> groupResults = searchInGroupBase(roleSearchFilter,
                            returningAttributes, SearchControls.SUBTREE_SCOPE, mainDirContext, searchBase);
                    SearchResult resultedGroup = null;
                    String groupDN = null;
                    if (groupResults.hasMore()) {
                        resultedGroup = groupResults.next();
                        groupDN = resultedGroup.getName();
                    }
                    modifyUserInRole(userNameDN, groupDN, DirContext.REMOVE_ATTRIBUTE, searchBase);
                    JNDIUtil.closeNamingEnumeration(groupResults);
                } else {
                    errorMessage = "The role: " + deletedRole + " does not exist.";
                    throw new UserStoreException(errorMessage);
                }
            }
        }
        if (newRoles != null && newRoles.length != 0) {

            for (String newRole : newRoles) {
                String searchFilter = userStoreProperties.get(LDAPConstants.ROLE_NAME_FILTER);

                if (doCheckExistingRole(newRole)) {
                    roleSearchFilter = searchFilter.replace("?", escapeSpecialCharactersForFilter(newRole));
                    String[] returningAttributes = new String[] { membershipAttribute };
                    String searchBase = userStoreProperties.get(LDAPConstants.GROUP_SEARCH_BASE);

                    NamingEnumeration<SearchResult> groupResults = searchInGroupBase(roleSearchFilter,
                            returningAttributes, SearchControls.SUBTREE_SCOPE, mainDirContext, searchBase);
                    SearchResult resultedGroup = null;
                    // assume only one group with given group name
                    String groupDN = null;
                    if (groupResults.hasMore()) {
                        resultedGroup = groupResults.next();
                        groupDN = resultedGroup.getName();
                    }
                    if (resultedGroup != null && !isUserInRole(userNameDN, resultedGroup)) {
                        modifyUserInRole(userNameDN, groupDN, DirContext.ADD_ATTRIBUTE, searchBase);
                    } else {
                        errorMessage = "User: " + userName + " already belongs to role: " + groupDN;
                        throw new UserStoreException(errorMessage);
                    }

                    JNDIUtil.closeNamingEnumeration(groupResults);

                } else {
                    errorMessage = "The role: " + newRole + " does not exist.";
                    throw new UserStoreException(errorMessage);
                }
            }
        }

    } catch (NamingException e) {
        errorMessage = "Error occurred while modifying the role list of user: " + userName;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeContext(mainDirContext);
    }
}