org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException. 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: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Tests to make sure the hashCode method is working properly.
 * @throws Exception on errors
 */
@Test
public void testHashCode() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();
    Value v0 = new Value( attribute, new byte[]
        { 0x01, 0x02 } );
    Value v1 = new Value( attribute, new byte[]
        { ( byte ) 0x81, ( byte ) 0x82 } );
    Value v2 = new Value( attribute, new byte[]
        { 0x01, 0x02 } );
    assertNotSame( v0.hashCode(), v1.hashCode() );
    assertNotSame( v1.hashCode(), v2.hashCode() );
    assertEquals( v0.hashCode(), v2.hashCode() );
    assertNotSame( v0, v1 );
    assertEquals( v0, v2 );
    assertNotSame( v1, v2 );

    Value v3 = new Value( attribute, new byte[]
        { 0x01, 0x03 } );
    assertFalse( v3.equals( v0 ) );
    assertFalse( v3.equals( v1 ) );
    assertFalse( v3.equals( v2 ) );
}
 
Example #3
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private Value createStringValue( AttributeType attributeType, String value )
{
    Value newValue;

    if ( attributeType != null )
    {
        try
        {
            newValue = new Value( attributeType, value );
        }
        catch ( LdapInvalidAttributeValueException iae )
        {
            return null;
        }
    }
    else
    {
        newValue = new Value( value );
    }

    return newValue;
}
 
Example #4
Source File: StringValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the compareTo method
 */
@Test
public void testCompareTo() throws LdapInvalidAttributeValueException
{
    AttributeType at1 = EntryUtils.getCaseIgnoringAttributeNoNumbersType();
    Value v0 = new Value( at1, "Alex" );
    Value v1 = new Value( at1, "ALEX" );

    assertEquals( 0, v0.compareTo( v1 ) );
    assertEquals( 0, v1.compareTo( v0 ) );

    Value v2 = new Value( at1, (String)null );

    assertEquals( 1, v0.compareTo( v2 ) );
    assertEquals( -1, v2.compareTo( v0 ) );
}
 
Example #5
Source File: StringValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Tests to make sure the hashCode method is working properly.
 * @throws Exception on errors
 */
@Test
public void testHashCode() throws LdapInvalidAttributeValueException
{
    AttributeType at1 = EntryUtils.getCaseIgnoringAttributeNoNumbersType();
    Value v0 = new Value( at1, "Alex" );
    Value v1 = new Value( at1, "ALEX" );
    Value v2 = new Value( at1, "alex" );

    assertEquals( v0.hashCode(), v1.hashCode() );
    assertEquals( v0.hashCode(), v2.hashCode() );
    assertEquals( v1.hashCode(), v2.hashCode() );

    assertEquals( v0, v1 );
    assertEquals( v0, v2 );
    assertEquals( v1, v2 );

    Value v3 = new Value( at1, "Timber" );

    assertNotSame( v0.hashCode(), v3.hashCode() );

    Value v4 = new Value( at, "Alex" );

    assertNotSame( v0.hashCode(), v4.hashCode() );
}
 
Example #6
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the isValid method
 * 
 * The SyntaxChecker does not accept values longer than 5 chars.
 */
@Test
public void testIsValid() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();

    new Value( attribute, ( byte[] ) null );
    new Value( attribute, Strings.EMPTY_BYTES );
    new Value( attribute, new byte[]
        { 0x01, 0x02 } );

    try
    {
        new Value( attribute, new byte[]
            { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 } );
        fail();
    }
    catch ( LdapInvalidAttributeValueException liave )
    {
        assertTrue( true );
    }
}
 
Example #7
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance of a schema aware Attribute, without value.
 * 
 * @param upId the ID for the added attributeType
 * @param attributeType the added AttributeType
 */
public DefaultAttribute( String upId, AttributeType attributeType )
{
    if ( attributeType == null )
    {
        String message = I18n.err( I18n.ERR_13203_ATTRIBUTE_TYPE_NULL_NOT_ALLOWED );
        LOG.error( message );
        throw new IllegalArgumentException( message );
    }

    try
    {
        apply( attributeType );
    }
    catch ( LdapInvalidAttributeValueException liave )
    {
        // Do nothing, it can't happen, there is no value
    }

    setUpId( upId, attributeType );
}
 
Example #8
Source File: StringValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the isValid method
 * 
 * The SyntaxChecker does not accept values longer than 5 chars.
 */
@Test
public void testIsValid() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getIA5StringAttributeType();

    new Value( attribute, (String)null );
    new Value( attribute, "" );
    new Value( attribute, "TEST" );

    try
    {
        new Value( attribute, "testlong" );
        fail();
    }
    catch ( LdapInvalidAttributeValueException liave )
    {
        assertTrue( true );
    }
}
 
Example #9
Source File: StringValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the getNormValue method
 */
@Test
public void testGetNormalizedValue() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getIA5StringAttributeType();

    Value sv = new Value( attribute, (String)null );

    assertTrue( sv.isSchemaAware() );
    assertNull( sv.getString() );
    assertTrue( sv.isSchemaAware() );

    sv = new Value( attribute, "" );
    assertTrue( sv.isSchemaAware() );
    assertEquals( 0, sv.compareTo( "  " ) );
    assertTrue( sv.isSchemaAware() );

    sv = new Value( attribute, "TEST" );
    assertTrue( sv.isSchemaAware() );
    assertEquals( 0, sv.compareTo( " test " ) );
}
 
Example #10
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private Value createBinaryValue( AttributeType attributeType, byte[] value )
    throws LdapInvalidAttributeValueException
{
    Value binaryValue;

    if ( attributeType != null )
    {
        binaryValue = new Value( attributeType, value );
    }
    else
    {
        binaryValue = new Value( value );
    }

    return binaryValue;
}
 
Example #11
Source File: Value.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Uses the syntaxChecker associated with the attributeType to check if the
 * value is valid.
 * 
 * @param syntaxChecker the SyntaxChecker to use to validate the value
 * @return <code>true</code> if the value is valid
 * @exception LdapInvalidAttributeValueException if the value cannot be validated
 */
public final boolean isValid( SyntaxChecker syntaxChecker ) throws LdapInvalidAttributeValueException
{
    if ( syntaxChecker == null )
    {
        String message = I18n.err( I18n.ERR_13219_NULL_SYNTAX_CHECKER, toString() );
        LOG.error( message );
        throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, message );
    }

    // No attributeType, or it's in relaxed mode
    if ( isHR )
    {
        // We need to prepare the String in this case
        return syntaxChecker.isValidSyntax( getString() );
    }
    else
    {
        return syntaxChecker.isValidSyntax( bytes );
    }
}
 
Example #12
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the compareTo method
 */
@Test
public void testCompareTo() throws LdapInvalidAttributeValueException
{
    AttributeType at1 = EntryUtils.getBytesAttributeType();
    Value v0 = new Value( at1, BYTES1 );
    Value v1 = new Value( at1, BYTES1 );

    assertEquals( 0, v0.compareTo( v1 ) );
    assertEquals( 0, v1.compareTo( v0 ) );

    Value v2 = new Value( at1, ( byte[] ) null );

    assertEquals( 1, v0.compareTo( v2 ) );
    assertEquals( -1, v2.compareTo( v0 ) );
}
 
Example #13
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the constructor with a value
 * @throws LdapInvalidAttributeValueException 
 */
@Test
public void testServerBinaryValueNoValue() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();
    byte[] val = new byte[]
        { 0x01 };
    Value bv = new Value( attribute, val );
    assertTrue( Arrays.equals( val, bv.getBytes() ) );
    assertFalse( bv.isNull() );
}
 
Example #14
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isInstanceOf( AttributeType attributeType ) throws LdapInvalidAttributeValueException
{
    return ( attributeType != null )
        && ( this.attributeType.equals( attributeType ) || this.attributeType.isDescendantOf( attributeType ) );
}
 
Example #15
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the constructor with an empty value
 */
@Test
public void testServerBinaryValueEmptyValue() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();

    Value value = new Value( attribute, Strings.EMPTY_BYTES );

    assertTrue( Arrays.equals( Strings.EMPTY_BYTES, value.getBytes() ) );
    assertFalse( value.isNull() );
}
 
Example #16
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the constructor with a null value
 */
@Test
public void testServerBinaryValueNullValue() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();

    Value value = new Value( attribute, ( byte[] ) null );

    assertNull( value.getBytes() );
    assertTrue( value.isNull() );
}
 
Example #17
Source File: StringValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the equals method
 */
@Test
public void testEquals() throws LdapInvalidAttributeValueException
{
    AttributeType at1 = EntryUtils.getIA5StringAttributeType();
    AttributeType at2 = EntryUtils.getBytesAttributeType();

    Value value1 = new Value( at1, "test" );
    Value value2 = new Value( at1, "test" );
    Value value3 = new Value( at1, "TEST" );
    Value value4 = new Value( at1, "tes" );
    Value value5 = new Value( at1, (byte[])null );
    Value valueBytes = new Value( at2, new byte[]
        { 0x01 } );
    Value valueString = new Value( at, "test" );

    assertTrue( value1.equals( value1 ) );
    assertTrue( value1.equals( value2 ) );
    assertTrue( value1.equals( value3 ) );
    assertFalse( value1.equals( value4 ) );
    assertFalse( value1.equals( value5 ) );
    assertTrue( value1.equals( "test" ) );
    assertFalse( value1.equals( null ) );

    assertFalse( value1.equals( valueString ) );
    assertFalse( value1.equals( valueBytes ) );
}
 
Example #18
Source File: StringValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the constructor with a null value
 */
@Test
public void testClientStringValueNullValue() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getIA5StringAttributeType();

    Value value = new Value( attribute, (String)null );

    assertNull( value.getString() );
    assertTrue( value.isNull() );
}
 
Example #19
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public byte[] getBytes() throws LdapInvalidAttributeValueException
{
    Value value = get();

    if ( !isHumanReadable() && ( value != null ) )
    {
        return value.getBytes();
    }

    String message = I18n.err( I18n.ERR_13214_VALUE_EXPECT_BYTES );
    LOG.error( message );
    throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, message );
}
 
Example #20
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the getNormValue method
 */
@Test
public void testGetNormalizedValue() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();

    Value value = new Value( attribute, ( byte[] ) null );
    assertNull( value.getBytes() );

    value = new Value( attribute, Strings.EMPTY_BYTES );
    assertTrue( Arrays.equals( Strings.EMPTY_BYTES, value.getBytes() ) );

    value = new Value( attribute, BYTES2 );
    assertTrue( Arrays.equals( BYTES2, value.getBytes() ) );
}
 
Example #21
Source File: LdifEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Check that we can't create an empty LdifEntry
 */
@Test
public void testLdifEntryEmpty() throws Exception
{
    assertThrows( LdapInvalidAttributeValueException.class, () ->
    {
        new LdifEntry( "", "" );
    } );
}
 
Example #22
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the constructor with a value
 */
@Test
public void testServerBinaryValue() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();
    byte[] val = new byte[]
        { 0x01 };
    Value value = new Value( attribute, val );

    assertTrue( Arrays.equals( val, value.getBytes() ) );
    assertFalse( value.isNull() );
    assertTrue( Arrays.equals( val, value.getBytes() ) );
}
 
Example #23
Source File: AttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * test that we properly throw an exception, and not a NPE, when no AttributeType is provided.
 */
@Test
public void testNullAT() throws LdapInvalidAttributeValueException
{
    AttributeType attributeType = new AttributeType( "mail" );
    assertThrows( IllegalArgumentException.class, () -> {
        new Value( attributeType, "[email protected]" );
    } );
}
 
Example #24
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 #25
Source File: Value.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a schema aware StringValue with an initial user provided String value and 
 * its normalized Value
 *
 * @param attributeType the schema type associated with this StringValue
 * @param upValue the value to wrap
 * @param normValue the normalized value to wrap
 * @throws LdapInvalidAttributeValueException If the added value is invalid accordingly
 * to the schema
 */
public Value( AttributeType attributeType, String upValue, String normValue ) throws LdapInvalidAttributeValueException
{
    init( attributeType );
    this.upValue = upValue;
    
    if ( upValue != null )
    {
        bytes = Strings.getBytesUtf8( upValue );
    }
    else
    {
        bytes = null;
    }
    
    this.normValue = normValue;
    
    if ( !attributeType.isRelaxed() )
    {
        // Check the value
        if ( attributeType.getSyntax().getSyntaxChecker() != null )
        {
            if ( !attributeType.getSyntax().getSyntaxChecker().isValidSyntax( upValue ) )
            {
                throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, 
                    I18n.err( I18n.ERR_13246_INVALID_VALUE_PER_SYNTAX ) );
            }
        }
        else
        {
            // We should always have a SyntaxChecker
            throw new IllegalArgumentException( I18n.err( I18n.ERR_13219_NULL_SYNTAX_CHECKER, normValue ) );
        }
    }
    
    hashCode();
}
 
Example #26
Source File: BinaryValueAttributeTypeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the getAttributeType method
 * @throws LdapInvalidAttributeValueException 
 */
@Test
public void testgetAttributeType() throws LdapInvalidAttributeValueException
{
    AttributeType attribute = EntryUtils.getBytesAttributeType();
    Value sbv = Value.createValue( attribute );

    assertEquals( attribute, sbv.getAttributeType() );
}
 
Example #27
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getString() throws LdapInvalidAttributeValueException
{
    Value value = get();

    if ( isHumanReadable() )
    {
        if ( value != null )
        {
            return value.getString();
        }
        else
        {
            return "";
        }
    }
    
    if ( attributeType == null )
    {
        // Special case : the Attribute is not schema aware.
        // The value is binary, we will try to convert it to a String
        return Strings.utf8ToString( value.getBytes() );
    }

    String message = I18n.err( I18n.ERR_13215_VALUE_EXPECT_STRING );
    LOG.error( message );
    throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, message );
}
 
Example #28
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method getBytes()
 */
@Test
public void testGetBytes() throws LdapInvalidAttributeValueException
{
    Attribute attr1 = new DefaultAttribute( atPwd );

    attr1.add( ( byte[] ) null );
    assertNull( attr1.getBytes() );

    Attribute attr2 = new DefaultAttribute( atPwd );

    attr2.add( BYTES1, BYTES2 );
    assertTrue( Arrays.equals( BYTES1, attr2.getBytes() ) );

    Attribute attr3 = new DefaultAttribute( atCN );

    attr3.add( "a", "b" );

    try
    {
        attr3.getBytes();
        fail();
    }
    catch ( LdapInvalidAttributeValueException ivae )
    {
        assertTrue( true );
    }
}
 
Example #29
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of a schema aware Attribute, with some byte[] values, and
 * a user provided ID.
 * 
 * @param upId the ID for the added attribute
 * @param attributeType the AttributeType to be added
 * @param vals the binary values for the added attribute
 * @throws LdapInvalidAttributeValueException If any of the
 * added values is not valid
 */
public DefaultAttribute( String upId, AttributeType attributeType, byte[]... vals )
    throws LdapInvalidAttributeValueException
{
    if ( attributeType == null )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_13203_ATTRIBUTE_TYPE_NULL_NOT_ALLOWED ) );
    }

    apply( attributeType );
    add( vals );
    setUpId( upId, attributeType );
}
 
Example #30
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of an Attribute, with some binary values, and a user provided ID.
 * 
 * @param upId the ID of the created attribute
 * @param vals an initial set of binary values for this attribute
 */
public DefaultAttribute( String upId, byte[]... vals )
{
    try
    {
        add( vals );
    }
    catch ( LdapInvalidAttributeValueException liave )
    {
        // Do nothing, this can't happen
    }

    setUpId( upId );
}