Example usage for javax.naming.directory Attributes get

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

Introduction

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

Prototype

Attribute get(String attrID);

Source Link

Document

Retrieves the attribute with the given attribute id from the attribute set.

Usage

From source file:org.sipfoundry.sipxconfig.bulk.ldap.UserMapper.java

/**
 * Returns all string values for an attribute with a given name, ignores the values that are
 * not string values//from   w  w w .j av a  2  s .co m
 *
 * @param attrs collection of attributes
 * @param attr attribute name
 */
private Set<String> getValues(Attributes attrs, String attrName) throws NamingException {
    Attribute attribute = attrs.get(attrName);
    if (attribute == null) {
        return null;
    }
    Set<String> values = new TreeSet<String>();
    NamingEnumeration<?> allValues = attribute.getAll();
    while (allValues.hasMore()) {
        Object object = allValues.nextElement();
        if (object instanceof String) {
            values.add((String) object);
        }
    }
    return values;
}

From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java

private void assertExist(DirContext context, String dn, String attribute, String value) throws NamingException {
    SearchControls ctls = new SearchControls();

    ctls.setDerefLinkFlag(true);//from  www .  j  a  va 2s .  c  o m
    ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    ctls.setReturningAttributes(new String[] { "*" });

    BasicAttributes matchingAttributes = new BasicAttributes();
    matchingAttributes.put(attribute, value);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("inetOrgPerson");
    matchingAttributes.put(objectClass);

    NamingEnumeration<SearchResult> results = context.search(suffix, matchingAttributes);

    assertTrue(results.hasMoreElements());
    SearchResult result = results.nextElement();
    Attributes attrs = result.getAttributes();
    Attribute testAttr = attrs.get(attribute);
    assertEquals(value, testAttr.get());

}

From source file:org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper.java

public Object mapAttributes(String dn, Attributes attributes) throws NamingException {
    LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();

    essence.setDn(dn);//from  w w w. j a v a 2s .com
    essence.setAttributes(attributes);

    Attribute passwordAttribute = attributes.get(passwordAttributeName);

    if (passwordAttribute != null) {
        essence.setPassword(mapPassword(passwordAttribute));
    }

    // Map the roles
    for (int i = 0; (roleAttributes != null) && (i < roleAttributes.length); i++) {
        Attribute roleAttribute = attributes.get(roleAttributes[i]);

        if (roleAttribute == null) {
            logger.debug("Couldn't read role attribute '" + roleAttributes[i] + "' for user " + dn);
            continue;
        }

        NamingEnumeration attributeRoles = roleAttribute.getAll();

        while (attributeRoles.hasMore()) {
            GrantedAuthority authority = createAuthority(attributeRoles.next());

            if (authority != null) {
                essence.addAuthority(authority);
            } else {
                logger.debug(
                        "Failed to create an authority value from attribute with Id: " + roleAttribute.getID());
            }
        }
    }

    return essence;
}

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

/**
 * Creates a subject with a custom implementation.
 * //w  w  w. java2 s  .c o  m
 * @param attrs
 *            The attributes associated to the subject.
 * @return The Subject.
 */
private Subject createSubject(final Attributes attrs) {
    String subjectName = "";
    String subjectID = "";
    String description = "";
    try {
        Attribute attribute = attrs.get(this.subjectIDAttributeName);
        if (attribute == null) {
            LOGGER.error("The LDAP attribute \"" + this.subjectIDAttributeName + "\" does not have a value. "
                    + "It is beging used as the Grouper special attribute \"SubjectID\".");
            return null;
        }
        subjectID = (String) attribute.get();
        attribute = attrs.get(this.nameAttributeName);
        if (attribute == null) {
            LOGGER.error("The LDAP attribute \"" + this.nameAttributeName + "\" does not have a value. z"
                    + "It is being used as the Grouper special attribute \"name\".");
            return null;
        }
        subjectName = (String) attribute.get();
        attribute = attrs.get(this.descriptionAttributeName);
        if (attribute == null) {
            LOGGER.error("The LDAP attribute \"" + this.descriptionAttributeName + "\" does not have a value. "
                    + "It is being used as the Grouper special attribute \"description\".");
        } else {
            description = (String) attribute.get();
        }
    } catch (NamingException ex) {
        LOGGER.error("LDAP Naming Except: " + ex.getMessage(), ex);
    }

    return new ESCOJNDISubject(subjectID, subjectName, description, this.getSubjectType(), this);

}

From source file:com.photon.phresco.ldap.impl.LDAPManagerImpl.java

private String getMailId(Attributes attrs) throws NamingException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method LDAPManagerImpl.getMailId(Attributes attrs");
    }//from  w ww  .  jav  a 2  s  .co  m
    String mailId = null;
    Attribute attribute = attrs.get(ldapConfig.getMailIdAttribute());
    if (attribute != null) {
        mailId = (String) attribute.get();
    }
    return mailId;
}

From source file:org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl.java

public List getAllPersonNames() {
    DirContext ctx = createAnonymousContext();

    LinkedList list = new LinkedList();
    NamingEnumeration results = null;
    try {/*from w w w  . j a  v  a2 s  .co  m*/
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        results = ctx.search("", "(objectclass=person)", controls);

        while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            Attribute attr = attributes.get("cn");
            String cn = (String) attr.get();
            list.add(cn);
        }
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
    return list;
}

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

public List<String> getAllPersonNames() {
    DirContext ctx = createAnonymousContext();

    LinkedList<String> list = new LinkedList<String>();
    NamingEnumeration<?> results = null;
    try {//  w  w  w. j a  va2 s  .  c o m
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        results = ctx.search("", "(objectclass=person)", controls);

        while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            Attribute attr = attributes.get("cn");
            String cn = (String) attr.get();
            list.add(cn);
        }
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
    return list;
}

From source file:com.photon.phresco.ldap.impl.LDAPManagerImpl.java

private String getDisplayName(Attributes attrs) throws NamingException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method LDAPManagerImpl.getDisplayName(Attributes attrs)");
    }/*  w w  w .jav a2 s  .  co m*/
    String displayName = null;
    Attribute attribute = attrs.get(ldapConfig.getDisplayNameAttribute());
    if (attribute != null) {
        displayName = (String) attribute.get();
    }
    return displayName;
}

From source file:org.sipfoundry.sipxconfig.bulk.ldap.LdapRowInserterTest.java

public void testCheckRowData() throws Exception {
    IMocksControl control = org.easymock.classextension.EasyMock.createNiceControl();
    UserMapper userMapper = control.createMock(UserMapper.class);
    SearchResult searchResult = control.createMock(SearchResult.class);
    Attributes attributes = control.createMock(Attributes.class);
    Attribute attribute = control.createMock(Attribute.class);

    searchResult.getAttributes();// w w  w.  j  av a2s  .  co  m
    control.andReturn(attributes);
    attributes.get("identity");
    control.andReturn(null);
    control.replay();
    m_rowInserter.setUserMapper(userMapper);
    assertEquals(RowStatus.FAILURE, m_rowInserter.checkRowData(searchResult).getRowStatus());

    control.reset();
    searchResult.getAttributes();
    control.andReturn(attributes);
    attributes.get("identity");
    control.andReturn(attribute);
    userMapper.getUserName(attributes);
    control.andReturn("@McQueen");
    userMapper.getGroupNames(searchResult);
    control.andReturn(Collections.singleton(SALES));
    control.replay();
    m_rowInserter.setUserMapper(userMapper);
    assertEquals(RowStatus.FAILURE, m_rowInserter.checkRowData(searchResult).getRowStatus());

    control.reset();
    searchResult.getAttributes();
    control.andReturn(attributes);
    attributes.get("identity");
    control.andReturn(attribute);
    userMapper.getUserName(attributes);
    control.andReturn("McQueen");
    userMapper.getGroupNames(searchResult);
    control.andReturn(Collections.singleton(SALES));
    control.replay();
    m_rowInserter.setUserMapper(userMapper);
    assertEquals(RowStatus.SUCCESS, m_rowInserter.checkRowData(searchResult).getRowStatus());
}

From source file:org.gbif.portal.registration.LDAPUtils.java

/**
 * Creates a user. String array contains:
 * 1) first name/*from   www.ja va2 s  . co  m*/
 * 2) surname
 * 3) email
 * 4) username
 * 5) password
 * 
 * @param userDetails
 * @return
 * @throws NamingException
 */
public boolean createNewUser(UserLogin userLogin) throws NamingException {
    DirContext ctx = getUserContext();
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute("sn", userLogin.getSurname()));
    attributes.put(new BasicAttribute("givenName", userLogin.getFirstName()));
    attributes.put(new BasicAttribute("cn", userLogin.getFirstName() + " " + userLogin.getSurname()));
    attributes.put(new BasicAttribute("mail", userLogin.getEmail()));
    if (userLogin.getTelephone() != null) {
        attributes.put(new BasicAttribute("telephoneNumber", userLogin.getTelephone()));
    }
    attributes.put(new BasicAttribute("userPassword", userLogin.getPassword()));
    attributes.put(new BasicAttribute("objectClass", "top"));
    attributes.put(new BasicAttribute("objectClass", "person"));
    attributes.put(new BasicAttribute("objectClass", "organizationalPerson"));
    attributes.put(new BasicAttribute("objectClass", "inetorgperson"));
    String contextName = "uid=" + userLogin.getUsername();
    String fullContextName = contextName + "," + ctx.getNameInNamespace();

    //add the user to ldap
    ctx.createSubcontext(contextName, attributes);

    //need to add user to group
    for (int i = 0; i < userGroups.length; i++) {
        DirContext groupContext = getGroupContext();
        Attributes groupAttributes = groupContext.getAttributes(userGroups[i]);
        groupAttributes.get("uniqueMember").add(fullContextName);
        groupContext.modifyAttributes(userGroups[i], DirContext.REPLACE_ATTRIBUTE, groupAttributes);
    }
    return true;
}