Java Code Examples for org.ldaptive.LdapEntry#getAttributes()

The following examples show how to use org.ldaptive.LdapEntry#getAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapUser.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
public static Map<String, String> extractLdapAttributes(String originalUsername, final LdapEntry userEntry
        , int customAttrMaxValueLen, List<String> whiteListedAttributes) {
    Map<String, String> attributes = new HashMap<>();
    attributes.put("ldap.original.username", originalUsername);
    attributes.put("ldap.dn", userEntry.getDn());

    if (customAttrMaxValueLen > 0) {
        for (LdapAttribute attr : userEntry.getAttributes()) {
            if (attr != null && !attr.isBinary() && !attr.getName().toLowerCase().contains("password")) {
                final String val = Utils.getSingleStringValue(attr);
                // only consider attributes which are not binary and where its value is not
                // longer than customAttrMaxValueLen characters
                if (val != null && val.length() > 0 && val.length() <= customAttrMaxValueLen) {
                    if (whiteListedAttributes != null && !whiteListedAttributes.isEmpty()) {
                        if (WildcardMatcher.matchAny(whiteListedAttributes, attr.getName())) {
                            attributes.put("attr.ldap." + attr.getName(), val);
                        }
                    } else {
                        attributes.put("attr.ldap." + attr.getName(), val);
                    }
                }
            }
        }
    }
    return Collections.unmodifiableMap(attributes);
}
 
Example 2
Source File: LdapPersonAttributeDao.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
/**
 * Converts an ldaptive <code>LdapEntry</code> containing result entry attributes into an attribute map as needed
 * by Person Directory components.
 *
 * @param entry Ldap entry.
 *
 * @return Attribute map.
 */
private Map<String, List<Object>> convertLdapEntryToMap(final LdapEntry entry) {
    final Map<String, List<Object>> attributeMap = new LinkedHashMap<>(entry.size());
    for (final LdapAttribute attr : entry.getAttributes()) {
        attributeMap.put(attr.getName(), new ArrayList<Object>(attr.getStringValues()));
    }
    logger.debug("Converted ldap DN entry [{}] to attribute map {}", entry.getDn(), attributeMap.toString());
    return attributeMap;
}
 
Example 3
Source File: LdapTestUtils.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
/**
 * Creates the given LDAP entries.
 *
 * @param connection Open LDAP connection used to connect to directory.
 * @param entries Collection of LDAP entries.
 *
 * @throws Exception On LDAP errors.
 */
public static void createLdapEntries(final LDAPConnection connection, final Collection<LdapEntry> entries) throws Exception {
    for (final LdapEntry entry : entries) {
        final Collection<Attribute> attrs = new ArrayList<>(entry.getAttributeNames().length);
        for (final LdapAttribute a : entry.getAttributes()) {
            attrs.add(new Attribute(a.getName(), a.getStringValues()));
        }
        connection.add(new AddRequest(entry.getDn(), attrs));
    }
}
 
Example 4
Source File: LdapPersonAttributeDao.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an ldaptive <code>LdapEntry</code> containing result entry attributes into an attribute map as needed
 * by Person Directory components.
 *
 * @param entry Ldap entry.
 *
 * @return Attribute map.
 */
private Map<String, List<Object>> convertLdapEntryToMap(final LdapEntry entry) {
    final Map<String, List<Object>> attributeMap = new LinkedHashMap<String, List<Object>>(entry.size());
    for (final LdapAttribute attr : entry.getAttributes()) {
        attributeMap.put(attr.getName(), new ArrayList<Object>(attr.getStringValues()));
    }
    logger.debug("Converted ldap DN entry [{}] to attribute map {}", entry.getDn(), attributeMap.toString());
    return attributeMap;
}
 
Example 5
Source File: LdapServiceRegistryDao.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
private RegisteredService update(final RegisteredService rs) {
    Connection searchConnection = null;
    try {
        searchConnection = this.connectionFactory.getConnection();
        final Response<SearchResult> response = searchForServiceById(searchConnection, rs.getId());
        if (hasResults(response)) {
            final String currentDn = response.getResult().getEntry().getDn();

            Connection modifyConnection = null;
            try {
                modifyConnection = this.connectionFactory.getConnection();
                final ModifyOperation operation = new ModifyOperation(searchConnection);

                final List<AttributeModification> mods = new ArrayList<AttributeModification>();

                final LdapEntry entry = this.ldapServiceMapper.mapFromRegisteredService(this.searchRequest.getBaseDn(), rs);
                for (final LdapAttribute attr : entry.getAttributes()) {
                    mods.add(new AttributeModification(AttributeModificationType.REPLACE, attr));
                }
                final ModifyRequest request = new ModifyRequest(currentDn, mods.toArray(new AttributeModification[] {}));
                operation.execute(request);
            } finally {
                LdapUtils.closeConnection(modifyConnection);
            }
        }
    } catch (final LdapException e) {
        logger.error(e.getMessage(), e);
    } finally {
        LdapUtils.closeConnection(searchConnection);
    }
    return rs;
}