Example usage for javax.naming.directory Attributes put

List of usage examples for javax.naming.directory Attributes put

Introduction

In this page you can find the example usage for javax.naming.directory Attributes put.

Prototype

Attribute put(Attribute attr);

Source Link

Document

Adds a new attribute to the attribute set.

Usage

From source file:org.apache.jmeter.protocol.ldap.sampler.LDAPExtSampler.java

/***************************************************************************
 * Collect all the values from the table (Arguments), using this create the
 * Attributes, this will create the Attributes for the User
 * defined TestCase for Add Test//from  ww  w.  j av  a 2s.c  o  m
 *
 * @return The Attributes
 **************************************************************************/
private Attributes getUserAttributes() {
    Attributes attrs = new BasicAttributes(true);
    Attribute attr;

    for (JMeterProperty jMeterProperty : getArguments()) {
        Argument item = (Argument) jMeterProperty.getObjectValue();
        attr = attrs.get(item.getName());
        if (attr == null) {
            attr = getBasicAttribute(item.getName(), item.getValue());
        } else {
            attr.add(item.getValue());
        }
        attrs.put(attr);
    }
    return attrs;
}

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

/**
 * @see javax.naming.directory.DirContext#getAttributes(String, String[])
 *///from   w ww  . ja  v  a  2 s  . com
public Attributes getAttributes(String name, String[] attrIds) throws NamingException {
    if (StringUtils.hasLength(name)) {
        throw new NameNotFoundException();
    }

    Attributes a = new BasicAttributes(true);
    Attribute target;
    for (int i = 0; i < attrIds.length; i++) {
        target = originalAttrs.get(attrIds[i]);
        if (target != null) {
            a.put(target);
        }
    }

    return a;
}

From source file:de.fiz.ddb.aas.utils.LDAPEngineUtilityOrganisation.java

private boolean checkAttributeEnum(
        //List<Enum<?>> pOrgAtt, List<Enum<?>> pOldOrgAtt, String ldapAttributeName,
        List<ConstEnumOrgSubSector> pOrgAtt, List<ConstEnumOrgSubSector> pOldOrgAtt, String ldapAttributeName,
        Attributes vOrgAttributes, Attributes vOrgRemoveAttributes, boolean isUpdate) {
    boolean hasChanged = false;
    if (!isUpdate) {
        // is create
        if ((pOrgAtt != null) && (!pOrgAtt.isEmpty())) {
            Attribute vAttr = new BasicAttribute(ldapAttributeName);
            for (Enum<?> iterEnum : pOrgAtt) {
                if (iterEnum != null) {
                    vAttr.add(iterEnum.name());
                }/*  w ww.ja  v a  2s  .  c  o  m*/
            }
            if (vAttr.size() > 0) {
                vOrgAttributes.put(vAttr);
            }
        }
    } else {
        if ((pOrgAtt != null) && (!pOrgAtt.isEmpty())) {
            Attribute vAttr = new BasicAttribute(ldapAttributeName);
            if ((pOrgAtt.isEmpty()) && (pOldOrgAtt != null) && (!pOldOrgAtt.isEmpty())) {
                vOrgRemoveAttributes.put(vAttr);
                hasChanged = true;
            } else {
                for (Enum<?> iterEnum : pOrgAtt) {
                    if (iterEnum != null) {
                        vAttr.add(iterEnum.name());
                    }
                }
                if (vAttr.size() > 0) {
                    vOrgAttributes.put(vAttr);
                    hasChanged = true;
                }
            }
        } else if ((pOrgAtt == null || pOrgAtt.isEmpty()) && (pOldOrgAtt != null && !pOldOrgAtt.isEmpty())) {
            vOrgRemoveAttributes.put(new BasicAttribute(ldapAttributeName));
            hasChanged = true;
        }
    }
    return hasChanged;
}

From source file:de.fiz.ddb.aas.utils.LDAPEngineUtilityOrganisation.java

/**
 * Checks attribute-set if it has to be written to LDAP or removed from LDAP if attribute = 0, its like not set.
 * //ww w. j  a v a  2s.c o m
 * @param pOrganisationAtt
 * @param pOldOrganisationAtt
 * @param ldapAttributeName
 * @param vOrgAttributes
 * @param vOrgRemoveAttributes
 * @param isUpdate
 */
private boolean checkAttribute(List<String> pOrganisationAtt, List<String> pOldOrganisationAtt,
        String ldapAttributeName, Attributes vOrgAttributes, Attributes vOrgRemoveAttributes,
        boolean isUpdate) {
    boolean hasChanged = false;
    if (!isUpdate) {
        // is create
        if ((pOrganisationAtt != null) && (!pOrganisationAtt.isEmpty())) {
            Attribute vAttr = new BasicAttribute(ldapAttributeName);
            for (String url : pOrganisationAtt) {
                if ((url != null) && (url.trim().length() > 0)) {
                    vAttr.add(url);
                }
            }
            if (vAttr.size() > 0) {
                vOrgAttributes.put(vAttr);
            }
        }
    } else {
        if ((pOrganisationAtt != null) && (!pOrganisationAtt.isEmpty())) {
            Attribute vAttr = new BasicAttribute(ldapAttributeName);
            if (pOrganisationAtt.size() == 1 && pOrganisationAtt.contains("") && pOldOrganisationAtt != null
                    && !pOldOrganisationAtt.isEmpty()) {
                vOrgRemoveAttributes.put(vAttr);
                hasChanged = true;
            } else {
                for (String url : pOrganisationAtt) {
                    if ((url != null) && (url.trim().length() > 0)) {
                        vAttr.add(url);
                    }
                }
                if (vAttr.size() > 0) {
                    vOrgAttributes.put(vAttr);
                    hasChanged = true;
                }
            }
        } else if ((pOrganisationAtt == null || pOrganisationAtt.isEmpty())
                && (pOldOrganisationAtt != null && !pOldOrganisationAtt.isEmpty())) {
            vOrgRemoveAttributes.put(new BasicAttribute(ldapAttributeName));
            hasChanged = true;
        }
    }
    return hasChanged;
}

From source file:org.wso2.carbon.user.core.tenant.CommonHybridLDAPTenantManager.java

@Deprecated
protected String createAdminEntry(String dnOfUserContext, Tenant tenant, DirContext initialDirContext)
        throws UserStoreException {
    String userDN = null;/*from w w w  .  j ava  2  s  .  c om*/
    DirContext organizationalUsersContext = null;
    try {
        //get connection to tenant's user context
        organizationalUsersContext = (DirContext) initialDirContext.lookup(dnOfUserContext);
        Attributes userAttributes = new BasicAttributes(true);

        //create person object class attribute
        Attribute objClass = new BasicAttribute(LDAPConstants.OBJECT_CLASS_NAME);
        objClass.add(realmConfig.getUserStoreProperty(LDAPConstants.USER_ENTRY_OBJECT_CLASS));
        if (UserCoreUtil.isKdcEnabled(realmConfig)) {
            // Add Kerberos specific object classes
            objClass.add("krb5principal");
            objClass.add("krb5kdcentry");
            objClass.add("subschema");

            String principal = tenant.getAdminName() + UserCoreConstants.PRINCIPAL_USERNAME_SEPARATOR
                    + tenant.getDomain() + UserCoreConstants.TENANT_DOMAIN_COMBINER + getRealmName();
            Attribute kerberosPrincipalName = new BasicAttribute("krb5PrincipalName");
            kerberosPrincipalName.add(principal);

            Attribute keyVersionNumber = new BasicAttribute("krb5KeyVersionNumber");
            keyVersionNumber.add("0");

            userAttributes.put(kerberosPrincipalName);
            userAttributes.put(keyVersionNumber);
        }
        userAttributes.put(objClass);

        //create user password attribute
        Attribute password = new BasicAttribute(USER_PASSWORD_ATTRIBUTE_NAME);
        String passwordHashMethod = realmConfig.getUserStoreProperty(LDAPConstants.PASSWORD_HASH_METHOD);
        String passwordToStore = UserCoreUtil.getPasswordToStore(tenant.getAdminPassword(), passwordHashMethod,
                isKDCEnabled());
        password.add(passwordToStore);
        userAttributes.put(password);

        //create mail attribute
        Attribute adminEmail = new BasicAttribute(EMAIL_ATTRIBUTE_NAME);
        adminEmail.add(tenant.getEmail());
        userAttributes.put(adminEmail);

        //create compulsory attribute: sn-last name
        Attribute lastName = new BasicAttribute(SN_ATTRIBUTE_NAME);
        lastName.add(tenant.getAdminLastName());
        userAttributes.put(lastName);

        //read user name attribute in user-mgt.xml
        String userNameAttribute = realmConfig.getUserStoreProperty(LDAPConstants.USER_NAME_ATTRIBUTE);

        //if user name attribute is not cn, add it to attribute list
        if (!(CN_ATTRIBUTE_NAME.equals(userNameAttribute))) {
            Attribute firstName = new BasicAttribute(CN_ATTRIBUTE_NAME);
            firstName.add(tenant.getAdminFirstName());
            userAttributes.put(firstName);
        }
        String userRDN = userNameAttribute + "=" + tenant.getAdminName();
        organizationalUsersContext.bind(userRDN, null, userAttributes);
        userDN = userRDN + "," + dnOfUserContext;
        //return (userRDN + dnOfUserContext);
    } catch (NamingException e) {
        String errorMsg = "Error occurred while creating Admin entry";
        if (logger.isDebugEnabled()) {
            logger.debug(errorMsg, e);
        }
        throw new UserStoreException(errorMsg, e);
    } finally {
        closeContext(organizationalUsersContext);
    }

    return userDN;
}

From source file:edu.vt.middleware.ldap.dsml.Dsmlv1.java

/**
 * This will take a DSML <code>Element</code> containing an entry of type
 * <dsml:entry name="name"/> and convert it to a LDAP search result.
 *
 * @param  entryElement  <code>Element</code> of DSML content
 *
 * @return  <code>SearchResult</code>
 *///from  w w  w  .j  a va  2s . c  om
protected SearchResult createSearchResult(final Element entryElement) {
    String name = "";
    final Attributes entryAttributes = new BasicAttributes(true);
    SearchResult attrResults = null;

    if (entryElement != null) {

        name = entryElement.attributeValue("dn");
        if (name == null) {
            name = "";
        }

        if (entryElement.hasContent()) {

            final Iterator<?> ocIterator = entryElement.elementIterator("objectclass");
            while (ocIterator.hasNext()) {
                final Element ocElement = (Element) ocIterator.next();
                if (ocElement != null && ocElement.hasContent()) {
                    final String ocName = "objectClass";
                    final Attribute entryAttribute = new BasicAttribute(ocName);
                    final Iterator<?> valueIterator = ocElement.elementIterator("oc-value");
                    while (valueIterator.hasNext()) {
                        final Element valueElement = (Element) valueIterator.next();
                        if (valueElement != null) {
                            final String value = valueElement.getText();
                            if (value != null) {
                                entryAttribute.add(value);
                            }
                        }
                    }
                    entryAttributes.put(entryAttribute);
                }
            }

            attrResults = super.createSearchResult(entryElement);
        }
    }

    if (attrResults != null) {
        final Attributes attrs = attrResults.getAttributes();
        if (attrs != null) {
            final NamingEnumeration<? extends Attribute> ae = attrs.getAll();
            if (ae != null) {
                try {
                    while (ae.hasMore()) {
                        entryAttributes.put(ae.next());
                    }
                } catch (NamingException e) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Could not read attribute in SearchResult from parent");
                    }
                }
            }
        }
    }
    return new SearchResult(name, null, entryAttributes);
}

From source file:nl.nn.adapterframework.ldap.LdapSender.java

/**
 *Strips all the values from the attributes in <code>input</code>. This is performed to be able to delete 
 *the attributes without having to match the values. If values exist they must be exactly matched too in
 *order to delete the attribute.//from  w  w  w.  ja  va 2 s.c  o  m
 */
protected Attributes removeValuesFromAttributes(Attributes input) {
    Attributes result = new BasicAttributes(true);
    // ignore attribute name case
    NamingEnumeration enumeration = input.getIDs();
    while (enumeration.hasMoreElements()) {
        String attrId = (String) enumeration.nextElement();
        result.put(new BasicAttribute(attrId));
    }
    return result;
}

From source file:org.swordess.ldap.odm.core.SessionImpl.java

private static Attributes fromTransientToAttributes(Object obj) {
    EntityMetaData metaData = EntityMetaData.get(ClassHelper.actualClass(obj));
    Attributes toSaves = new BasicAttributes();
    for (EntityPropertyMetaData propMetaData : metaData) {
        Object propValue = propMetaData.getter().get(obj);
        if (propValue == null) {
            continue;
        }/*  w w w .ja  v a2 s  .c  o  m*/

        // ignore empty list when create
        if (propMetaData.isMultiple() && ((List) propValue).isEmpty()) {
            continue;
        }

        Evaluator<String> evaluator = null;
        if (propMetaData.isReference()) {
            // reference property use dn as attribute value
            final EntityMetaData metaDataOfReferenceProp = EntityMetaData.get(propMetaData.getValueClass());
            evaluator = new Evaluator<String>() {
                public String eval(Object obj) {
                    Object idValue = metaDataOfReferenceProp.getIdProperty().getter().get(obj);
                    if (null == idValue) {
                        return null;
                    }
                    return DnHelper.build((String) idValue, metaDataOfReferenceProp.getManagedClass());
                }
            };
        } else {
            evaluator = createPropEvaluator(propMetaData);
        }

        if (!propMetaData.isMultiple()) {
            AttrUtils.putIfNotNull(toSaves,
                    AttrUtils.create(propMetaData.getLdapPropName(), propValue, evaluator));
        } else {
            AttrUtils.putIfNotNull(toSaves,
                    AttrUtils.create(propMetaData.getLdapPropName(), (List) propValue, evaluator));
        }
    }

    toSaves.put(AttrUtils.create("objectclass", metaData.objectClasses()));
    return toSaves;
}

From source file:com.funambol.LDAP.dao.impl.ContactDAO.java

/**
 * Compares two attribute sets/*from   ww w . jav a  2  s  . c  o  m*/
 * 
 * @param authoritativeSet
 *            reference set
 * @param compareSet
 *            comparative set
 * @return list of modifications to commit
 * @throws NamingException
 */
public Map<String, Attributes> compareAttributeSets(Attributes authoritativeSet, Attributes compareSet)
        throws NamingException {

    Map<String, Attributes> modifications = new HashMap<String, Attributes>();
    Attributes delAttributes = new BasicAttributes();
    Attributes addAttributes = new BasicAttributes();
    Attributes replaceAttributes = new BasicAttributes();
    // List<LDAPModification> modifications = new
    // ArrayList<LDAPModification>();
    List<String> supportedAttrs = Arrays.asList(getSupportedAttributes());

    Iterator<String> it = supportedAttrs.iterator();

    // loop over supported attributes
    while (it.hasNext()) {
        String attribute = it.next();

        // skip unmodifiable attrs
        if (attribute.equals("modifyTimestamp"))
            continue;

        Attribute authoritaveAttribute = authoritativeSet.get(attribute);
        Attribute compareAttribute = compareSet.get(attribute);

        if (authoritaveAttribute == null || compareAttribute == null) {
            // remove an old attribute
            if (authoritaveAttribute == null && compareAttribute != null) {
                delAttributes.put(compareAttribute);
            }

            // add a new attribute
            if (authoritaveAttribute != null && compareAttribute == null) {
                addAttributes.put(authoritaveAttribute);
            }
        } else {
            // replace an attribute
            String authValue = (String) authoritaveAttribute.get();
            String compareValue = (String) compareAttribute.get();
            if (!authValue.equals(compareValue)) {
                replaceAttributes.put(authoritaveAttribute);
            }
        }
    }
    modifications.put(DEL_ATTRIBUTE, delAttributes);
    modifications.put(REPLACE_ATTRIBUTE, replaceAttributes);
    modifications.put(ADD_ATTRIBUTE, addAttributes);

    return modifications;
}

From source file:edu.internet2.middleware.subject.provider.LdapSourceAdapter.java

protected Attributes getLdapUnique(Search search, String searchValue, String[] attributeNames)
        throws SubjectNotFoundException, SubjectNotUniqueException {
    Attributes attributes = null;
    Iterator<SearchResult> results = getLdapResults(search, searchValue, attributeNames);

    if (results == null || !results.hasNext()) {
        String errMsg = "No results: " + search.getSearchType() + " filter:" + search.getParam("filter")
                + " searchValue: " + searchValue;
        throw new SubjectNotFoundException(errMsg);
    }/*w w w. j  ava 2s  . co  m*/

    SearchResult si = (SearchResult) results.next();
    attributes = si.getAttributes();
    if (results.hasNext()) {
        si = (SearchResult) results.next();
        if (!multipleResults) {
            String errMsg = "Search is not unique:" + si.getName() + "\n";
            throw new SubjectNotUniqueException(errMsg);
        }
        Attributes attr = si.getAttributes();
        NamingEnumeration<? extends Attribute> n = attr.getAll();
        try {
            while (n.hasMore()) {
                Attribute a = n.next();
                log.debug("checking attribute " + a.getID());
                if (attributes.get(a.getID()) == null) {
                    log.debug("adding " + a.getID());
                    attributes.put(a);
                }
            }
        } catch (NamingException e) {
            log.error("ldap excp: " + e);
        }
    }
    return attributes;
}