Java Code Examples for org.apache.directory.api.ldap.model.entry.Entry#getAttributes()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Entry#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: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ServerEntry into a BasicAttributes. The Dn is lost
 * during this conversion, as the Attributes object does not store
 * this element.
 *
 * @return An instance of a AttributesImpl() object
 */
public static Attributes toBasicAttributes( Entry entry )
{
    if ( entry == null )
    {
        return null;
    }

    Attributes attributes = new BasicAttributes( true );

    for ( Attribute attribute : entry.getAttributes() )
    {
        AttributeType attributeType = attribute.getAttributeType();
        Attribute attr = entry.get( attributeType );

        // Deal with a special case : an entry without any ObjectClass
        if ( attributeType.getOid().equals( SchemaConstants.OBJECT_CLASS_AT_OID ) && attr.size() == 0 )
        {
            // We don't have any objectClass, just dismiss this element
            continue;
        }

        attributes.put( toBasicAttribute( attr ) );
    }

    return attributes;
}
 
Example 2
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ServerEntry into a BasicAttributes. The Dn is lost
 * during this conversion, as the Attributes object does not store
 * this element.
 *
 * @return An instance of a AttributesImpl() object
 */
public static Attributes toBasicAttributes( Entry entry )
{
    if ( entry == null )
    {
        return null;
    }

    Attributes attributes = new BasicAttributes( true );

    for ( Attribute attribute : entry.getAttributes() )
    {
        AttributeType attributeType = attribute.getAttributeType();
        Attribute attr = entry.get( attributeType );

        // Deal with a special case : an entry without any ObjectClass
        if ( attributeType.getOid().equals( SchemaConstants.OBJECT_CLASS_AT_OID ) && attr.size() == 0 )
        {
            // We don't have any objectClass, just dismiss this element
            continue;
        }

        attributes.put( toBasicAttribute( attr ) );
    }

    return attributes;
}
 
Example 3
Source File: AuthenticationProviderService.java    From guacamole-client with Apache License 2.0 5 votes vote down vote up
/**
 * Returns parameter tokens generated from LDAP attributes on the user
 * currently bound under the given LDAP connection. The attributes to be
 * converted into parameter tokens must be explicitly listed in
 * guacamole.properties. If no attributes are specified or none are
 * found on the LDAP user object, an empty map is returned.
 *
 * @param ldapConnection
 *     LDAP connection to use to read the attributes of the user.
 *
 * @param username
 *     The username of the user whose attributes are to be queried.
 *
 * @return
 *     A map of parameter tokens generated from attributes on the user
 *     currently bound under the given LDAP connection, as a map of token
 *     name to corresponding value, or an empty map if no attributes are
 *     specified or none are found on the user object.
 *
 * @throws GuacamoleException
 *     If an error occurs retrieving the user DN or the attributes.
 */
private Map<String, String> getAttributeTokens(LdapNetworkConnection ldapConnection,
        Dn userDn) throws GuacamoleException {

    // Get attributes from configuration information
    List<String> attrList = confService.getAttributes();

    // If there are no attributes there is no reason to search LDAP
    if (attrList.isEmpty())
        return Collections.<String, String>emptyMap();

    // Build LDAP query parameters
    String[] attrArray = attrList.toArray(new String[attrList.size()]);

    Map<String, String> tokens = new HashMap<>();
    try {

        // Get LDAP attributes by querying LDAP
        Entry userEntry = ldapConnection.lookup(userDn, attrArray);
        if (userEntry == null)
            return Collections.<String, String>emptyMap();

        Collection<Attribute> attributes = userEntry.getAttributes();
        if (attributes == null)
            return Collections.<String, String>emptyMap();

        // Convert each retrieved attribute into a corresponding token
        for (Attribute attr : attributes) {
            tokens.put(TokenName.canonicalize(attr.getId(),
                    LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getString());
        }

    }
    catch (LdapException e) {
        throw new GuacamoleServerException("Could not query LDAP user attributes.", e);
    }

    return tokens;

}
 
Example 4
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Map<String, String> readStringAttributes( final String entryDN, final Set<String> attributes )
        throws ChaiOperationException, ChaiUnavailableException, IllegalStateException
{
    activityPreCheck();
    getInputValidator().readStringAttributes( entryDN, attributes );

    try
    {
        final EntryCursor entries = connection.search(
                entryDN,
                ChaiConstant.FILTER_OBJECTCLASS_ANY,
                org.apache.directory.api.ldap.model.message.SearchScope.OBJECT,
                attributes.toArray( new String[attributes.size()] )
        );
        final Entry entry = entries.iterator().next();
        final Collection<Attribute> attrs = entry.getAttributes();
        final Map<String, String> returnMap = new LinkedHashMap<>();
        for ( final Attribute attr : attrs )
        {
            final String name = attr.getId();
            final String value = attr.getString();
            returnMap.put( name, value );
        }

        return returnMap;

    }
    catch ( LdapException e )
    {
        throw ChaiOperationException.forErrorMessage( e.getMessage() );
    }
}
 
Example 5
Source File: AuthenticationProviderService.java    From guacamole-client with Apache License 2.0 5 votes vote down vote up
/**
 * Returns parameter tokens generated from LDAP attributes on the user
 * currently bound under the given LDAP connection. The attributes to be
 * converted into parameter tokens must be explicitly listed in
 * guacamole.properties. If no attributes are specified or none are
 * found on the LDAP user object, an empty map is returned.
 *
 * @param ldapConnection
 *     LDAP connection to use to read the attributes of the user.
 *
 * @param username
 *     The username of the user whose attributes are to be queried.
 *
 * @return
 *     A map of parameter tokens generated from attributes on the user
 *     currently bound under the given LDAP connection, as a map of token
 *     name to corresponding value, or an empty map if no attributes are
 *     specified or none are found on the user object.
 *
 * @throws GuacamoleException
 *     If an error occurs retrieving the user DN or the attributes.
 */
private Map<String, String> getAttributeTokens(LdapNetworkConnection ldapConnection,
        Dn userDn) throws GuacamoleException {

    // Get attributes from configuration information
    List<String> attrList = confService.getAttributes();

    // If there are no attributes there is no reason to search LDAP
    if (attrList.isEmpty())
        return Collections.<String, String>emptyMap();

    // Build LDAP query parameters
    String[] attrArray = attrList.toArray(new String[attrList.size()]);

    Map<String, String> tokens = new HashMap<>();
    try {

        // Get LDAP attributes by querying LDAP
        Entry userEntry = ldapConnection.lookup(userDn, attrArray);
        if (userEntry == null)
            return Collections.<String, String>emptyMap();

        Collection<Attribute> attributes = userEntry.getAttributes();
        if (attributes == null)
            return Collections.<String, String>emptyMap();

        // Convert each retrieved attribute into a corresponding token
        for (Attribute attr : attributes) {
            tokens.put(TokenName.canonicalize(attr.getId(),
                    LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getString());
        }

    }
    catch (LdapException e) {
        throw new GuacamoleServerException("Could not query LDAP user attributes.", e);
    }

    return tokens;

}