Java Code Examples for org.apache.directory.api.ldap.model.entry.Value#getBytes()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Value#getBytes() . 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: BinaryAnonymizer.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type)
 */
@Override
public Attribute anonymize( Map<Value, Value> valueMap, Set<Value> valueSet, Attribute attribute )
{
    Attribute result = new DefaultAttribute( attribute.getAttributeType() );

    for ( Value value : attribute )
    {
        byte[] bytesValue = value.getBytes();
        byte[] newValue = computeNewValue( bytesValue );
        
        try
        {
            result.add( newValue );
            Value anonValue = new Value( attribute.getAttributeType(), newValue );
            valueMap.put( ( Value ) value, anonValue );
            valueSet.add( anonValue );
        }
        catch ( LdapInvalidAttributeValueException e )
        {
            throw new RuntimeException( I18n.err( I18n.ERR_13436_ERROR_ANONYMIZING_VALUE, value ) );
        }
    }

    return result;
}
 
Example 2
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Check a String attribute to see if there is some byte[] value in it.
 *
 * If this is the case, try to change it to a String value.
 */
private boolean checkHumanReadable( Attribute attribute ) throws LdapException
{
    boolean isModified = false;

    // Loop on each values
    for ( Value<?> value : attribute )
    {
        if ( value instanceof StringValue )
        {
            continue;
        }
        else if ( value instanceof BinaryValue )
        {
            // we have a byte[] value. It should be a String UTF-8 encoded
            // Let's transform it
            try
            {
                String valStr = new String( value.getBytes(), "UTF-8" );
                attribute.remove( value );
                attribute.add( valStr );
                isModified = true;
            }
            catch ( UnsupportedEncodingException uee )
            {
                throw new LdapException( I18n.err( I18n.ERR_281 ) );
            }
        }
        else
        {
            throw new LdapException( I18n.err( I18n.ERR_282 ) );
        }
    }

    return isModified;
}
 
Example 3
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Check a String attribute to see if there is some byte[] value in it.
 *
 * If this is the case, try to change it to a String value.
 */
private boolean checkHumanReadable( Attribute attribute ) throws LdapException
{
    boolean isModified = false;

    // Loop on each values
    for ( Value<?> value : attribute )
    {
        if ( value instanceof StringValue )
        {
            continue;
        }
        else if ( value instanceof BinaryValue )
        {
            // we have a byte[] value. It should be a String UTF-8 encoded
            // Let's transform it
            try
            {
                String valStr = new String( value.getBytes(), "UTF-8" );
                attribute.remove( value );
                attribute.add( valStr );
                isModified = true;
            }
            catch ( UnsupportedEncodingException uee )
            {
                throw new LdapException( I18n.err( I18n.ERR_281 ) );
            }
        }
        else
        {
            throw new LdapException( I18n.err( I18n.ERR_282 ) );
        }
    }

    return isModified;
}
 
Example 4
Source File: SimpleNode.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the value of this node.
 * 
 * @param value the value for this node
 */
public void setValue( Value value )
{
    this.value = value;
    this.bytes = value.getBytes();
}
 
Example 5
Source File: AttributeClassLoader.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Class<?> findClass( String name ) throws ClassNotFoundException
{
    byte[] classBytes;

    Value value = attribute.get();

    if ( value.isHumanReadable() )
    {
        throw new ClassNotFoundException( I18n.err( I18n.ERR_16008_AT_ACCESS_FAILURE ) );
    }

    classBytes = value.getBytes();

    // May be we are dealing with a JAR ?
    try 
    {
        Map<String, Class<?>> classes = loadClasses( classBytes );
        
        if ( classes == null )
        {
            // May be a simple class ?
            return defineClass( name, classBytes, 0, classBytes.length );
        }
        
        for ( Map.Entry<String, Class<?>> entry : classes.entrySet() )
        {
            if ( entry.getKey().contains( name ) )
            {
                return entry.getValue();
            }
        }
    }
    catch ( IOException ioe )
    {
        // Ok, may be a pure class
        return defineClass( name, classBytes, 0, classBytes.length );
    }
    
    return null;
}