Example usage for javax.naming.directory Attributes getIDs

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

Introduction

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

Prototype

NamingEnumeration<String> getIDs();

Source Link

Document

Retrieves an enumeration of the ids of the attributes in the attribute set.

Usage

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  .jav a 2 s . com
 */
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:nl.nn.adapterframework.ldap.LdapSender.java

private String performOperationCreate(String entryName, ParameterResolutionContext prc, Map paramValueMap,
        Attributes attrs) throws SenderException, ParameterException {
    if (manipulationSubject.equals(MANIPULATION_ATTRIBUTE)) {
        String result = null;/*from w w  w. j a v a 2  s .  c om*/
        NamingEnumeration na = attrs.getAll();
        while (na.hasMoreElements()) {
            Attribute a = (Attribute) na.nextElement();
            log.debug("Create attribute: " + a.getID());
            NamingEnumeration values;
            try {
                values = a.getAll();
            } catch (NamingException e1) {
                storeLdapException(e1, prc);
                throw new SenderException("cannot obtain values of Attribute [" + a.getID() + "]", e1);
            }
            while (values.hasMoreElements()) {
                Attributes partialAttrs = new BasicAttributes();
                Attribute singleValuedAttribute;
                String id = a.getID();
                Object value = values.nextElement();
                if (log.isDebugEnabled()) {
                    if (id.toLowerCase().contains("password") || id.toLowerCase().contains("pwd")) {
                        log.debug("Create value: ***");
                    } else {
                        log.debug("Create value: " + value);
                    }
                }
                if (unicodePwd && "unicodePwd".equalsIgnoreCase(id)) {
                    singleValuedAttribute = new BasicAttribute(id, encodeUnicodePwd(value));
                } else {
                    singleValuedAttribute = new BasicAttribute(id, value);
                }
                partialAttrs.put(singleValuedAttribute);
                DirContext dirContext = null;
                try {
                    dirContext = getDirContext(paramValueMap);
                    dirContext.modifyAttributes(entryName, DirContext.ADD_ATTRIBUTE, partialAttrs);
                } catch (NamingException e) {
                    // https://wiki.servicenow.com/index.php?title=LDAP_Error_Codes:
                    //   20 LDAP_TYPE_OR_VALUE_EXISTS Indicates that the attribute value specified in a modify or add operation already exists as a value for that attribute.
                    // Sun:
                    //   [LDAP: error code 20 - Attribute Or Value Exists]
                    if (e.getMessage().startsWith("[LDAP: error code 20 - ")) {
                        if (log.isDebugEnabled())
                            log.debug("Operation [" + getOperation() + "] successful: " + e.getMessage());
                        result = DEFAULT_RESULT_CREATE_OK;
                    } else {
                        storeLdapException(e, prc);
                        throw new SenderException(
                                "Exception in operation [" + getOperation() + "] entryName [" + entryName + "]",
                                e);
                    }
                } finally {
                    closeDirContext(dirContext);
                }
            }
        }
        if (result != null) {
            return result;
        }
        return DEFAULT_RESULT;
    } else {
        DirContext dirContext = null;
        try {
            if (unicodePwd) {
                Enumeration enumeration = attrs.getIDs();
                while (enumeration.hasMoreElements()) {
                    String id = (String) enumeration.nextElement();
                    if ("unicodePwd".equalsIgnoreCase(id)) {
                        Attribute attr = attrs.get(id);
                        for (int i = 0; i < attr.size(); i++) {
                            attr.set(i, encodeUnicodePwd(attr.get(i)));
                        }
                    }
                }
            }
            dirContext = getDirContext(paramValueMap);
            dirContext.bind(entryName, null, attrs);
            return DEFAULT_RESULT;
        } catch (NamingException e) {
            // if (log.isDebugEnabled()) log.debug("Exception in operation [" + getOperation()+ "] entryName ["+entryName+"]", e);
            if (log.isDebugEnabled())
                log.debug("Exception in operation [" + getOperation() + "] entryName [" + entryName + "]: "
                        + e.getMessage());
            // https://wiki.servicenow.com/index.php?title=LDAP_Error_Codes:
            //   68 LDAP_ALREADY_EXISTS Indicates that the add operation attempted to add an entry that already exists, or that the modify operation attempted to rename an entry to the name of an entry that already exists.
            // Sun:
            //   [LDAP: error code 68 - Entry Already Exists]
            if (e.getMessage().startsWith("[LDAP: error code 68 - ")) {
                return DEFAULT_RESULT_CREATE_OK;
            } else {
                storeLdapException(e, prc);
                throw new SenderException(e);
            }
        } finally {
            closeDirContext(dirContext);
        }
    }

}

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

/**
 * Convert a <i>Contact</i> into a LDAP inetOrgPerson set of attributes.
 * This method is used in from Client to Server
 * /*w w w  .j  a v a2 s .c  om*/
 * @param contact
 *            contact to transform into Attributes
 * @return Attributes representation of the contact
 */
public Attributes createEntry(Contact contact) {

    if (logger.isTraceEnabled())
        logger.trace("Working on contact:" + contact.getUid());

    Attributes attributeSet = new BasicAttributes();
    Attribute objClass = new BasicAttribute("objectClass");
    if (logger.isDebugEnabled())
        logger.debug("Ok let's add objectclass");

    objClass.add("inetOrgPerson");
    objClass.add("person");

    attributeSet.put(objClass);
    try {

        if (contact.getUid() == null) {
            contact.setUid(createUniqueId(contact));
            logger.info("UID is now: " + contact.getUid());
        }

        // Split contact object into sub-objects
        Name name = contact.getName();
        PersonalDetail personal = contact.getPersonalDetail();
        BusinessDetail business = contact.getBusinessDetail();

        List phones = personal.getPhones();
        List businessPhones = business.getPhones();

        List mails = personal.getEmails();
        List note = contact.getNotes();

        // personal address
        Address addr = personal.getAddress();

        // if displayname doesn't exist and the firstname and the lastname
        // are not both defined, this will result in a NullPointerException
        // I don't want to support any other ways of doing this right now.
        // a solution could be to use an UID for the rdn
        if (name != null) {

            if (propertyCheck(name.getLastName())) {
                attributeSet.put(new BasicAttribute("sn", name.getLastName().getPropertyValueAsString()));
            } else {
                attributeSet.put(new BasicAttribute("sn", ""));
            }

            if (propertyCheck(name.getFirstName())) {
                attributeSet
                        .put(new BasicAttribute("givenName", name.getFirstName().getPropertyValueAsString()));
            } else {
                attributeSet.put(new BasicAttribute("givenName", ""));
            }

            attributeSet.put(new BasicAttribute("cn", name.getFirstName().getPropertyValueAsString() + " "
                    + name.getLastName().getPropertyValueAsString()));
        }

        // Company name
        if (business != null && propertyCheck(business.getCompany())) {
            attributeSet.put(new BasicAttribute("o", business.getCompany().getPropertyValueAsString()));
        }

        // Adding phones
        if (phones != null && !phones.isEmpty()) {

            Iterator iter2 = phones.iterator();
            while (iter2.hasNext()) {
                Phone phone = (Phone) iter2.next();

                // if empty, no need to check type
                if (!propertyCheck(phone))
                    continue;

                // Home phones
                if (phone.getPhoneType().equals("HomeTelephoneNumber")) {
                    attributeSet.put(new BasicAttribute("homePhone", phone.getPropertyValueAsString()));
                }

                // MobilePhones
                if (phone.getPhoneType().equals("MobileTelephoneNumber"))
                    attributeSet.put(new BasicAttribute("mobile", phone.getPropertyValueAsString()));

            }
        }

        // Adding business phones
        if (businessPhones != null && !businessPhones.isEmpty()) {

            Iterator iter2 = businessPhones.iterator();
            while (iter2.hasNext()) {
                Phone phone = (Phone) iter2.next();

                // if empty, no need to check type
                if (!propertyCheck(phone))
                    continue;

                // Business phones
                if (phone.getPhoneType().equals("BusinessTelephoneNumber")) {
                    attributeSet.put(new BasicAttribute("telephoneNumber", phone.getPropertyValueAsString()));
                }
                // Fax
                if (phone.getPhoneType().equals("BusinessFaxNumber")) {
                    attributeSet.put(
                            new BasicAttribute("facsimiletelephonenumber", phone.getPropertyValueAsString()));
                }
            }
        }

        if (mails != null && !mails.isEmpty()) {

            Iterator iter1 = mails.iterator();

            // For each email address, add it
            while (iter1.hasNext()) {
                Email mail = (Email) iter1.next();
                if (propertyCheck(mail))
                    attributeSet.put(new BasicAttribute("mail", mail.getPropertyValueAsString()));
            }
        }

        // Address
        if (addr != null) {
            if (propertyCheck(personal.getAddress().getPostalCode()))
                attributeSet.put(new BasicAttribute("postalCode",
                        personal.getAddress().getPostalCode().getPropertyValueAsString()));

            if (propertyCheck(personal.getAddress().getStreet()))
                attributeSet.put(new BasicAttribute("postalAddress",
                        personal.getAddress().getStreet().getPropertyValueAsString()));

            if (propertyCheck(personal.getAddress().getCity()))
                attributeSet.put(
                        new BasicAttribute("l", personal.getAddress().getCity().getPropertyValueAsString()));
        }

        // Notes
        if (note != null && !note.isEmpty()) {
            Iterator note1 = note.iterator();
            while (note1.hasNext()) {
                Note nota = (Note) note1.next();
                if (propertyCheck(nota))
                    attributeSet.put(new BasicAttribute("description", nota.getPropertyValueAsString()));
            }
        }

        logger.info("Resulting LDAPAttributeSet is:");

        NamingEnumeration<String> ids = attributeSet.getIDs();

        while (ids.hasMoreElements()) {
            String attrID = ids.nextElement();
            logger.info(attrID + ": " + ((String) attributeSet.get(attrID).get()));

        }

        // Create the LDAPEntry with dn and attributes
        // THE DN is the DisplayName
        return attributeSet;

    } catch (Exception e) {
        logger.warn("Unable to create LDAPEntry from Contact: " + e.toString(), e);
        return null;
    }
}