Java Code Examples for org.apache.directory.api.ldap.model.entry.Attribute#size()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Attribute#size() . 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: SchemaEntityFactory.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Create a list of string from a multivalued attribute's values
 * 
 * @param attr The Attribute to read
 * @return The list of values as Strings
 */
private List<String> getStrings( Attribute attr )
{
    if ( attr == null )
    {
        return EMPTY_LIST;
    }

    List<String> strings = new ArrayList<>( attr.size() );

    for ( Value value : attr )
    {
        strings.add( value.getString() );
    }

    return strings;
}
 
Example 2
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of {@link java.util.Properties}, convert to raw data name-value format and load into ldap
 * modification set in preparation for ldap add.
 *
 * @param props    contains {@link java.util.Properties} targeted for adding to ldap.
 * @param entry    contains ldap entry to pull attrs from.
 * @param attrName contains the name of the ldap attribute to be added.
 * @throws LdapException If we weren't able to add the properies into the entry
 */
protected void loadProperties( Properties props, Entry entry, String attrName ) throws LdapException
{
    if ( ( props != null ) && ( props.size() > 0 ) )
    {
        Attribute attr = new DefaultAttribute( attrName );

        for ( Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); )
        {
            // This LDAP attr is stored as a name-value pair separated by a ':'.
            String key = ( String ) e.nextElement();
            String val = props.getProperty( key );
            String prop = key + GlobalIds.PROP_SEP + val;

            attr.add( prop );
        }

        if ( attr.size() != 0 )
        {
            entry.add( attr );
        }
    }
}
 
Example 3
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of ARBAC roles, {@link UserAdminRole}, convert to raw data format and load into ldap
 * attribute set in preparation for ldap add.
 *
 * @param list  contains List of type {@link UserAdminRole} targeted for adding to ldap.
 * @param entry collection of ldap attributes containing ARBAC role assignments in raw ldap format.
 * @throws LdapException
 */
private void loadUserAdminRoles( List<UserAdminRole> list, Entry entry ) throws LdapException
{
    if ( list != null )
    {
        Attribute userAdminRoleData = new DefaultAttribute( GlobalIds.USER_ADMINROLE_DATA );
        Attribute userAdminRoleAssign = new DefaultAttribute( GlobalIds.USER_ADMINROLE_ASSIGN );

        for ( UserAdminRole userRole : list )
        {
            userAdminRoleData.add( userRole.getRawData() );
            userAdminRoleAssign.add( userRole.getName() );
        }

        if ( userAdminRoleData.size() != 0 )
        {
            entry.add( userAdminRoleData );
            entry.add( userAdminRoleAssign );
        }
    }
}
 
Example 4
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of RBAC roles, {@link UserRole}, convert to raw data format and load into ldap modification
 * set in preparation for ldap modify.
 *
 * @param list contains List of type {@link UserRole} targeted for updating into ldap.
 * @param mods contains ldap modification set containing RBAC role assignments in raw ldap format to be updated.
 * @throws LdapInvalidAttributeValueException
 */
private void loadUserRoles( List<UserRole> list, List<Modification> mods )
    throws LdapInvalidAttributeValueException
{
    Attribute userRoleData = new DefaultAttribute( GlobalIds.USER_ROLE_DATA );
    Attribute userRoleAssign = new DefaultAttribute( USER_ROLE_ASSIGN );

    if ( list != null )
    {
        for ( UserRole userRole : list )
        {
            userRoleData.add( userRole.getRawData() );
            userRoleAssign.add( userRole.getName() );
        }

        if ( userRoleData.size() != 0 )
        {
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userRoleData ) );
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userRoleAssign ) );
        }
    }
}
 
Example 5
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of RBAC roles, {@link UserRole}, convert to raw data format and load into ldap attribute
 * set in preparation for ldap add.
 *
 * @param list  contains List of type {@link UserRole} targeted for adding to ldap.
 * @param entry ldap entry containing attributes mapping to RBAC role assignments in raw ldap format.
 * @throws LdapException
 */
private void loadUserRoles( List<UserRole> list, Entry entry ) throws LdapException
{
    if ( list != null )
    {
        Attribute userRoleData = new DefaultAttribute( GlobalIds.USER_ROLE_DATA );
        Attribute userRoleAssign = new DefaultAttribute( USER_ROLE_ASSIGN );

        for ( UserRole userRole : list )
        {
            userRoleData.add( userRole.getRawData() );
            userRoleAssign.add( userRole.getName() );
        }

        if ( userRoleData.size() != 0 )
        {
            entry.add( userRoleData, userRoleAssign );
        }
    }
}
 
Example 6
Source File: RangedAttributeInterceptor.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private Entry prepareEntry(Entry e) {
    Attribute attr = e.get(name);
    if (attr != null) {
        int start = (min != null)? min : 0;
        start = (start < attr.size())? start : attr.size() - 1;
        int end = (max != null && max < attr.size() - 1)? max : attr.size() - 1;
        if (start != 0 || end != attr.size() - 1) {
            // some values should be stripped out
            Iterator<Value> it = attr.iterator();
            Set<Value> valuesToRemove = new HashSet<>(end - start + 1);
            for (int i = 0; i < attr.size(); i++) {
                Value v = it.next();
                if (i < start || i > end) {
                    valuesToRemove.add(v);
                }
            }
            attr.setUpId(attr.getUpId() + ";range=" + start + "-" + ((end == attr.size() - 1)? "*" : end));
            attr.remove(valuesToRemove.toArray(new Value[0]));
        } else if (min != null) {
            // range explicitly requested although no value stripped
            attr.setUpId(attr.getUpId() + ";range=0-*");
        }
    }
    return e;
}
 
Example 7
Source File: DefaultSchemaLoader.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Indicates if the given Root DSE corresponds to an ApacheDS server.
 *
 * @param rootDse the Root DSE
 * @return <code>true</code> if this is an ApacheDS server,
 *         <code>false</code> if not.
 * @throws LdapInvalidAttributeValueException If the vendorName attribute contains an invalid value 
 */
private boolean isApacheDs( Entry rootDse ) throws LdapInvalidAttributeValueException
{
    if ( rootDse != null )
    {
        Attribute vendorNameAttribute = rootDse.get( SchemaConstants.VENDOR_NAME_AT );

        if ( ( vendorNameAttribute != null ) && vendorNameAttribute.size() == 1 )
        {
            return DEFAULT_APACHEDS_VENDOR_NAME.equalsIgnoreCase( vendorNameAttribute.getString() );
        }
    }

    return false;
}
 
Example 8
Source File: SearchResultEntryFactory.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Encode the attributes recursively
 *
 * <pre>
 *  0x30 LL partialAttributeList
 *    0x04 LL type
 *    0x31 LL vals
 *      0x04 LL attributeValue
 *      ...
 *      0x04 LL attributeValue
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @param attributes The iterator on the attributes
 */
private void encodeAttributes( Asn1Buffer buffer, Iterator<Attribute> attributes )
{
    if ( attributes.hasNext() )
    {
        Attribute attribute = attributes.next();

        // Recursive call
        encodeAttributes( buffer, attributes );

        int start = buffer.getPos();

        // The values, recursively, if any
        if ( attribute.size() != 0 )
        {
            encodeValues( buffer, attribute.iterator() );
        }

        // The values set
        BerValue.encodeSet( buffer, start );

        // The attribute type
        BerValue.encodeOctetString( buffer, attribute.getUpId() );

        // Attribute sequence
        BerValue.encodeSequence( buffer, start );
    }
}
 
Example 9
Source File: AddRequestFactory.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Encode the attributes, starting from the end. We iterate through the list
 * of attributes, recursively. The last attribute will be encoded first, when
 * the end of the list will be reached, which is what we went, as we encode from
 * the end.
 * <br>
 * An attribute is encoded this way:
 * <pre>
 *     0x30 LL attribute
 *       0x04 LL attributeType
 *       0x31 LL attributeValues
 *         0x04 LL attributeValue
 *         ...
 *         0x04 LL attributeValue
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @param iterator The iterator built on top of the attributes
 */
private void encodeAttributeReverse( Asn1Buffer buffer, Iterator<Attribute> iterator )
{
    if ( iterator.hasNext() )
    {
        Attribute attribute = iterator.next();

        // Recursive call to have the latest attributes encoded first
        encodeAttributeReverse( buffer, iterator );

        // Remind the current position
        int start = buffer.getPos();

        // The attributes values
        if ( attribute.size() == 0 )
        {
            BerValue.encodeOctetString( buffer, Strings.EMPTY_BYTES );
        }
        else
        {
            encodeValueReverse( buffer, attribute.iterator() );
        }

        // Then the values' SET
        BerValue.encodeSet( buffer, start );

        // The attribute type
        BerValue.encodeOctetString( buffer, attribute.getUpId() );

        // The attribute sequence
        BerValue.encodeSequence( buffer, start );
    }
}
 
Example 10
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 11
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see numbers of values of attributes conforms to the schema
 */
private void assertNumberOfAttributeValuesValid( Attribute attribute ) throws LdapInvalidAttributeValueException
{
    if ( attribute.size() > 1 && attribute.getAttributeType().isSingleValued() )
    {
        throw new LdapInvalidAttributeValueException( ResultCodeEnum.CONSTRAINT_VIOLATION, I18n.err( I18n.ERR_278,
            attribute.getUpId() ) );
    }
}
 
Example 12
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 13
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see numbers of values of attributes conforms to the schema
 */
private void assertNumberOfAttributeValuesValid( Attribute attribute ) throws LdapInvalidAttributeValueException
{
    if ( attribute.size() > 1 && attribute.getAttributeType().isSingleValued() )
    {
        throw new LdapInvalidAttributeValueException( ResultCodeEnum.CONSTRAINT_VIOLATION, I18n.err( I18n.ERR_278,
            attribute.getUpId() ) );
    }
}
 
Example 14
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Given a collection of ARBAC roles, {@link UserAdminRole}, convert to raw data format and load into ldap
 * modification set in preparation for ldap modify.
 *
 * @param list contains List of type {@link UserAdminRole} targeted for updating to ldap.
 * @param mods contains ldap modification set containing ARBAC role assignments in raw ldap format to be updated.
 * @throws LdapInvalidAttributeValueException
 */
private void loadUserAdminRoles( List<UserAdminRole> list, List<Modification> mods ) throws
    LdapInvalidAttributeValueException
{
    Attribute userAdminRoleData = new DefaultAttribute( GlobalIds.USER_ADMINROLE_DATA );
    Attribute userAdminRoleAssign = new DefaultAttribute( GlobalIds.USER_ADMINROLE_ASSIGN );

    if ( list != null )
    {
        boolean nameSeen = false;

        for ( UserAdminRole userRole : list )
        {
            userAdminRoleData.add( userRole.getRawData() );

            if ( !nameSeen )
            {
                userAdminRoleAssign.add( userRole.getName() );
                nameSeen = true;
            }
        }

        if ( userAdminRoleData.size() != 0 )
        {
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userAdminRoleData ) );
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userAdminRoleAssign ) );
        }
    }
}
 
Example 15
Source File: LdifUtils.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Converts an EntryAttribute as LDIF
 * 
 * @param attr the EntryAttribute to convert
 * @param length the expected line length
 * @return the corresponding LDIF code as a String
 */
public static String convertToLdif( Attribute attr, int length )
{
    StringBuilder sb = new StringBuilder();
    
    if ( attr.size() == 0 )
    {
        // Special case : we don't have any value
        return "";
    }

    for ( Value value : attr )
    {
        StringBuilder lineBuffer = new StringBuilder();

        lineBuffer.append( attr.getUpId() );

        // First, deal with null value (which is valid)
        if ( value.isNull() )
        {
            lineBuffer.append( ':' );
        }
        else if ( value.isHumanReadable() )
        {
            // It's a String but, we have to check if encoding isn't required
            String str = value.getString();

            if ( !LdifUtils.isLDIFSafe( str ) )
            {
                lineBuffer.append( ":: " ).append( encodeBase64( str ) );
            }
            else
            {
                lineBuffer.append( ':' );

                if ( str != null )
                {
                    lineBuffer.append( ' ' ).append( str );
                }
            }
        }
        else
        {
            // It is binary, so we have to encode it using Base64 before adding it
            char[] encoded = Base64.encode( value.getBytes() );

            lineBuffer.append( ":: " + new String( encoded ) );
        }

        lineBuffer.append( '\n' );
        sb.append( stripLineToNChars( lineBuffer.toString(), length ) );
    }

    return sb.toString();
}