Java Code Examples for org.apache.directory.api.ldap.model.entry.Modification#getAttribute()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Modification#getAttribute() . 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: ModificationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "test1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example 2
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to extract a modification item from an array of modifications.
 * 
 * @param mods the array of ModificationItems to extract the Attribute from.
 * @param type the attributeType spec of the Attribute to extract
 * @return the modification item on the attributeType specified
 */
public static final Modification getModificationItem( List<Modification> mods, AttributeType type )
{
    for ( Modification modification : mods )
    {
        Attribute attribute = modification.getAttribute();

        if ( attribute.getAttributeType() == type )
        {
            return modification;
        }
    }

    return null;
}
 
Example 3
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element
 * @throws NamingException
 */
@Test
public void testRequestWith1Modification() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_1_modification.xml" ).openStream(),
            "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();
    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals( 1, modifications.size() );

    Modification modification = modifications.iterator().next();

    assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );

    Attribute attribute = modification.getAttribute();

    assertEquals( "directreport", attribute.getId() );
    assertEquals( "CN=John Smith, DC=microsoft, DC=com", attribute.get().getString() );
}
 
Example 4
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a Modification to an instance of a ServerModification object.
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( Modification modification, AttributeType attributeType )
    throws LdapException
{
    Modification serverModification = new DefaultModification(
        modification.getOperation(),
        new DefaultAttribute( attributeType, modification.getAttribute() ) );

    return serverModification;

}
 
Example 5
Source File: LdifAnonymizerTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnonymizerModifyBinaryOptionAttribute() throws LdapException, IOException
{
    String ldif = 
        "dn: cn=Acme certificate,o=Acme,c=US,ou=IT Infrastructure,o=acme.com\n" +
        "changetype: modify\n" +
        "replace: certificateRevocationList;binary\n" +
        "certificateRevocationList;binary::YmxhaCBibGFo\n" +
        "-";

    LdifAnonymizer anonymizer = new LdifAnonymizer( schemaManager );
    anonymizer.addNamingContext( "o=acme.com" );
    String result = anonymizer.anonymize( ldif );
    
    List<LdifEntry> entries = ldifReader.parseLdif( result );
    
    assertEquals( 1, entries.size() );
    
    LdifEntry entry = entries.get( 0 );
    assertTrue( entry.isChangeModify() );
    assertEquals( 1, entry.getModifications().size() );
    
    Modification modification = entry.getModifications().get( 0 );
    assertEquals( ModificationOperation.REPLACE_ATTRIBUTE, modification.getOperation() );

    Attribute attribute = modification.getAttribute();
    assertEquals( "certificateRevocationList;binary", attribute.getUpId() );
    assertEquals( 1, attribute.size() );
    
    for ( Value value : attribute )
    {
        String str = value.getString();
        
        // We can only test the length and the fact the values are not equal (as the vale has been anonymized)
        assertNotSame( 0, value.length() );
        assertEquals( str.length(), value.length() );
    }
}
 
Example 6
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element without Value element
 * @throws NamingException
 */
@Test
public void testRequestWithModificationWithoutValue() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_modification_without_value.xml" )
            .openStream(), "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();

    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals( 1, modifications.size() );

    Modification modification = modifications.iterator().next();

    assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
    Attribute attribute = modification.getAttribute();

    assertEquals( "directreport", attribute.getId() );
    assertEquals( 0, attribute.size() );
}
 
Example 7
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element
 * @throws NamingException
 */
@Test
public void testRequestWithModificationWith2Values() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_modification_with_2_values.xml" )
            .openStream(), "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();

    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals( 1, modifications.size() );

    Modification modification = modifications.iterator().next();

    assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
    Attribute attribute = modification.getAttribute();

    assertEquals( "directreport", attribute.getId() );

    assertEquals( 2, attribute.size() );

    assertTrue( attribute.contains( "CN=John Smith, DC=microsoft, DC=com" ) );
    assertTrue( attribute.contains( "CN=Steve Jobs, DC=apple, DC=com" ) );
}
 
Example 8
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element with an empty value
 * @throws NamingException
 */
@Test
public void testRequestWithModificationWithEmptyValue() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_modification_with_empty_value.xml" )
            .openStream(), "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();

    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals( 1, modifications.size() );

    Modification modification = modifications.iterator().next();

    assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
    Attribute attribute = modification.getAttribute();

    assertEquals( "directreport", attribute.getId() );

    assertEquals( 1, attribute.size() );
    assertEquals( "", attribute.get().getString() );
}
 
Example 9
Source File: LdifEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a Modify changeType LdifEntry with no control
 */
@Test
public void testLdifEntryChangeTypeModifySimple() throws Exception
{
    String ldif =
        "changetype: modify\n" +
            "add: cn\n" +
            "cn: v1\n" +
            "cn: v2\n" +
            "-";

    LdifEntry ldifEntry = new LdifEntry( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldif );

    assertNotNull( ldifEntry );
    assertEquals( ChangeType.Modify, ldifEntry.getChangeType() );
    assertNull( ldifEntry.getEntry() );
    assertEquals( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldifEntry.getDn().getName() );
    assertFalse( ldifEntry.hasControls() );
    assertTrue( ldifEntry.isLdifChange() );

    // Check the modification
    assertNotNull( ldifEntry.getModifications() );

    for ( Modification modification : ldifEntry.getModifications() )
    {
        assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
        Attribute attribute = modification.getAttribute();

        assertNotNull( attribute );
        assertEquals( "cn", attribute.getId() );
        assertTrue( attribute.contains( "v1", "v2" ) );

    }
}
 
Example 10
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to extract a modification item from an array of modifications.
 * 
 * @param mods the array of ModificationItems to extract the Attribute from.
 * @param type the attributeType spec of the Attribute to extract
 * @return the modification item on the attributeType specified
 */
public static final Modification getModificationItem( List<Modification> mods, AttributeType type )
{
    for ( Modification modification : mods )
    {
        Attribute attribute = modification.getAttribute();

        if ( attribute.getAttributeType() == type )
        {
            return modification;
        }
    }

    return null;
}
 
Example 11
Source File: LdifEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a Modify changeType LdifEntry with increment operation
 */
@Test
public void testLdifEntryChangeTypeModifyIncrementNumber() throws Exception
{
    String ldif =
        "changetype: modify\n" +
            "increment: uidNumber\n" +
            "uidNumber: 3\n" +
            "-";

    LdifEntry ldifEntry = new LdifEntry( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldif );

    assertNotNull( ldifEntry );
    assertEquals( ChangeType.Modify, ldifEntry.getChangeType() );
    assertNull( ldifEntry.getEntry() );
    assertEquals( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldifEntry.getDn().getName() );
    assertFalse( ldifEntry.hasControls() );
    assertTrue( ldifEntry.isLdifChange() );

    // Check the modification
    assertNotNull( ldifEntry.getModifications() );

    for ( Modification modification : ldifEntry.getModifications() )
    {
        assertEquals( ModificationOperation.INCREMENT_ATTRIBUTE, modification.getOperation() );
        Attribute attribute = modification.getAttribute();

        assertNotNull( attribute );
        assertEquals( "uidnumber", attribute.getId() );
        assertEquals( "3", attribute.getString() );
    }
}
 
Example 12
Source File: LdifEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a Modify changeType LdifEntry with no attributes
 */
@Test
public void testLdifEntryChangeTypeModifyNoAttribute() throws Exception
{
    String ldif =
        "changetype: modify\n" +
            "add: cn\n" +
            "-";

    LdifEntry ldifEntry = new LdifEntry( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldif );

    assertNotNull( ldifEntry );
    assertEquals( ChangeType.Modify, ldifEntry.getChangeType() );
    assertNull( ldifEntry.getEntry() );
    assertEquals( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldifEntry.getDn().getName() );
    assertFalse( ldifEntry.hasControls() );
    assertTrue( ldifEntry.isLdifChange() );

    // Check the modification
    assertNotNull( ldifEntry.getModifications() );

    for ( Modification modification : ldifEntry.getModifications() )
    {
        assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
        Attribute attribute = modification.getAttribute();

        assertNotNull( attribute );
        assertEquals( "cn", attribute.getId() );
        assertNotNull( attribute.get() );
        assertTrue( attribute.get().isNull() );
    }
}
 
Example 13
Source File: LdifAnonymizerTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnonymizeModify() throws Exception
{
    String ldif = 
        "dn: [email protected],ou=Email,ou=Services,o=acme,dc=com\n" +
        "changetype: modify\n" +
        "replace: cn\n" +
        "cn::QUNNRSBJbmMuIExlZ2FsIFRlYW0=\n" +
        "-";
    LdifAnonymizer anonymizer = new LdifAnonymizer( schemaManager );
    anonymizer.addNamingContext( "o=acm,dc=com" );
    String result = anonymizer.anonymize( ldif );
    
    List<LdifEntry> entries = ldifReader.parseLdif( result );
    
    assertEquals( 1, entries.size() );
    
    LdifEntry entry = entries.get( 0 );
    assertTrue( entry.isChangeModify() );
    assertEquals( 1, entry.getModifications().size() );
    
    Modification modification = entry.getModifications().get( 0 );
    assertEquals( ModificationOperation.REPLACE_ATTRIBUTE, modification.getOperation() );

    Attribute attribute = modification.getAttribute();
    assertEquals( "cn", attribute.getUpId() );
    assertEquals( 1, attribute.size() );
    
    String value = attribute.getString();
    
    // We can only test the length and the fact the values are not equal (as the value has been anonymized)
    assertEquals( "AAAAAAAAAAAAAAAAAAAA".length(), value.length() );
    assertEquals( "AAAAAAAAAAAAAAAAAAAA", value );
}
 
Example 14
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a ModifyRequest with an add operation, and a
 * modification with a type and an empty vals
 */
@Test
public void testDecodeModifyRequestAddOperationModificationTypeEmptyVals()
    throws DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x37 );

    stream.put( new byte[]
        {
            0x30, 0x35,             // LdapMessage
              0x02, 0x01, 0x31,     // messageID MessageID
              0x66, 0x30,           // ModifyRequest
                0x04, 0x20,         // entry LDAPDN,
                  'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y',
                  ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',',
                  'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm',
                0x30, 0x0C,
                  0x30, 0x0A,
                    0x0A, 0x01, 0x00,
                    0x30, 0x05,
                       0x04, 0x01,
                        'l',
                       0x31, 0x00
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    // Decode a ModifyRequest PDU
    Asn1Decoder.decode( stream, ldapMessageContainer );

    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();

    assertEquals( 49, modifyRequest.getMessageId() );
    assertEquals( "cn=testModify,ou=users,ou=system", modifyRequest.getName().toString() );

    Object[] modifications = modifyRequest.getModifications().toArray();

    assertEquals( 1, modifications.length );

    Modification modification = ( Modification ) modifications[0];
    Attribute attributeValue = modification.getAttribute();

    assertEquals( "l", Strings.toLowerCaseAscii( attributeValue.getUpId() ) );
    assertEquals( 0, attributeValue.size() );

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, modifyRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 15
Source File: ModifyRequestFactory.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively encode the modifications, starting from the last one.
 * <pre>
 * 0x30 LL modification sequence
 *   0x0A 0x01 operation
 *   0x30 LL modification
 *     0x04 LL type
 *     0x31 LL vals
 *       0x04 LL attributeValue
 *       ...
 *       0x04 LL attributeValue
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @param modifications the modifications to encode
 */
private void encodeModifications( Asn1Buffer buffer, Iterator<Modification> modifications )
{
    if ( modifications.hasNext() )
    {
        Modification modification = modifications.next();

        // Recurse
        encodeModifications( buffer, modifications );

        int start = buffer.getPos();

        // The Attribute
        Attribute attribute = modification.getAttribute();

        // The values, if any
        if ( modification.getAttribute().size() != 0 )
        {
            encodeValues( buffer, modification.getAttribute().iterator() );

            // the value set
            BerValue.encodeSet( buffer, start );
        }
        else if ( modification.getOperation() != ModificationOperation.INCREMENT_ATTRIBUTE )
        {
            // the value set, if not a INCREMENT operation
            BerValue.encodeSet( buffer, start );
        }

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

        // The attribute sequence
        BerValue.encodeSequence( buffer, start );

        // The operation
        BerValue.encodeEnumerated( buffer, modification.getOperation().getValue() );

        // The modification sequence
        BerValue.encodeSequence( buffer, start );
    }
}
 
Example 16
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a ModifyRequest with an add operation, and a
 * modification with a type and an empty vals
 */
@Test
public void testDecodeModifyRequestAddOperationModificationIncrementWithValue()
    throws DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x42 );

    stream.put( new byte[]
        {
            0x30, 0x40,             // LdapMessage
              0x02, 0x01, 0x31,     // messageID MessageID
              0x66, 0x3B,           // ModifyRequest
                0x04, 0x20,         // entry LDAPDN,
                  'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y',
                  ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',',
                  'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm',
                0x30, 0x017,
                  0x30, 0x15,
                    0x0A, 0x01, 0x03,
                    0x30, 0x10,
                       0x04, 0x09,
                        'u', 'i', 'd', 'n', 'u', 'm', 'b', 'e', 'r',
                       0x31, 0x03,
                         0x04, 0x01,
                           '3'
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    // Decode a ModifyRequest PDU
    Asn1Decoder.decode( stream, ldapMessageContainer );

    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();

    assertEquals( 49, modifyRequest.getMessageId() );
    assertEquals( "cn=testModify,ou=users,ou=system", modifyRequest.getName().toString() );

    Object[] modifications = modifyRequest.getModifications().toArray();

    assertEquals( 1, modifications.length );

    Modification modification = ( Modification ) modifications[0];
    
    assertEquals( ModificationOperation.INCREMENT_ATTRIBUTE, modification.getOperation() );
    Attribute attributeValue = modification.getAttribute();

    assertEquals( "uidnumber", Strings.toLowerCaseAscii( attributeValue.getUpId() ) );
    assertEquals( 1, attributeValue.size() );
    assertEquals( "3", attributeValue.get().getString() );

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, modifyRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 17
Source File: LdifEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test a Modify changeType LdifEntry with no attributes and controls
 */
@Test
public void testLdifEntryChangeTypeModifyNoAttributeWithControls() throws Exception
{
    String ldif =
        "control: 1.2.840.113556.1.4.805 true\n" +
            "control: 1.2.840.113556.1.4.806 false: test\n" +
            "changetype: modify\n" +
            "add: cn\n" +
            "-";

    LdifEntry ldifEntry = new LdifEntry( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldif );

    assertNotNull( ldifEntry );
    assertEquals( ChangeType.Modify, ldifEntry.getChangeType() );
    assertNull( ldifEntry.getEntry() );
    assertEquals( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldifEntry.getDn().getName() );
    assertTrue( ldifEntry.isLdifChange() );

    // Check the modification
    assertNotNull( ldifEntry.getModifications() );

    for ( Modification modification : ldifEntry.getModifications() )
    {
        assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
        Attribute attribute = modification.getAttribute();

        assertNotNull( attribute );
        assertEquals( "cn", attribute.getId() );
        assertEquals( 1, attribute.size() );
        assertTrue( attribute.get().isNull() );
    }

    assertTrue( ldifEntry.hasControls() );

    LdifControl ldifControl = ldifEntry.getControl( "1.2.840.113556.1.4.805" );
    assertNotNull( ldifControl );
    assertEquals( "1.2.840.113556.1.4.805", ldifControl.getOid() );
    assertTrue( ldifControl.isCritical() );
    assertNull( ldifControl.getValue() );

    ldifControl = ldifEntry.getControl( "1.2.840.113556.1.4.806" );
    assertNotNull( ldifControl );
    assertEquals( "1.2.840.113556.1.4.806", ldifControl.getOid() );
    assertFalse( ldifControl.isCritical() );
    assertNotNull( ldifControl.getValue() );
    assertEquals( "test", Strings.utf8ToString( ldifControl.getValue() ) );
}
 
Example 18
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test parsing of a request with 2 Modification elements
 * @throws NamingException
 */
@Test
public void testRequestWith2Modifications() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_2_modifications.xml" ).openStream(),
            "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();

    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals( 2, modifications.size() );

    Iterator<Modification> iter = modifications.iterator();

    Modification modification = iter.next();

    assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
    Attribute attribute = modification.getAttribute();
    assertEquals( "directreport", attribute.getId() );

    assertEquals( "CN=John Smith, DC=microsoft, DC=com", attribute.get().getString() );

    modification = iter.next();

    attribute = modification.getAttribute();

    assertEquals( "sn", attribute.getId() );
    assertEquals( ModificationOperation.REPLACE_ATTRIBUTE, modification.getOperation() );

    assertEquals( "CN=Steve Jobs, DC=apple, DC=com", attribute.get().getString() );
}
 
Example 19
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a ModifyRequest with an add operation, and a
 * modification with a type and an empty vals
 */
@Test
public void testDecodeModifyRequestAddOperationModificationIncrement()
    throws DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x3D );

    stream.put( new byte[]
        {
            0x30, 0x3B,             // LdapMessage
              0x02, 0x01, 0x31,     // messageID MessageID
              0x66, 0x36,           // ModifyRequest
                0x04, 0x20,         // entry LDAPDN,
                  'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y',
                  ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',',
                  'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm',
                0x30, 0x012,
                  0x30, 0x10,
                    0x0A, 0x01, 0x03,
                    0x30, 0x0B,
                       0x04, 0x09,
                        'u', 'i', 'd', 'n', 'u', 'm', 'b', 'e', 'r',
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    // Decode a ModifyRequest PDU
    Asn1Decoder.decode( stream, ldapMessageContainer );

    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();

    assertEquals( 49, modifyRequest.getMessageId() );
    assertEquals( "cn=testModify,ou=users,ou=system", modifyRequest.getName().toString() );

    Object[] modifications = modifyRequest.getModifications().toArray();

    assertEquals( 1, modifications.length );

    Modification modification = ( Modification ) modifications[0];
    
    assertEquals( ModificationOperation.INCREMENT_ATTRIBUTE, modification.getOperation() );
    Attribute attributeValue = modification.getAttribute();

    assertEquals( "uidnumber", Strings.toLowerCaseAscii( attributeValue.getUpId() ) );
    assertEquals( 0, attributeValue.size() );

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, modifyRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 20
Source File: ModifyRequestDsml.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Element toDsml( Element root )
{
    Element element = super.toDsml( root );

    ModifyRequest request = getDecorated();

    // Dn
    if ( request.getName() != null )
    {
        element.addAttribute( "dn", request.getName().getName() );
    }

    // Modifications
    Collection<Modification> modifications = request.getModifications();

    for ( Modification modification : modifications )
    {
        Element modElement = element.addElement( "modification" );

        if ( modification.getAttribute() != null )
        {
            modElement.addAttribute( "name", modification.getAttribute().getId() );

            for ( Value value : modification.getAttribute() )
            {
                if ( value.getString() != null )
                {
                    if ( ParserUtils.needsBase64Encoding( value.getString() ) )
                    {
                        Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
                        Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
                        element.getDocument().getRootElement().add( xsdNamespace );
                        element.getDocument().getRootElement().add( xsiNamespace );

                        Element valueElement = modElement.addElement( "value" ).addText(
                            ParserUtils.base64Encode( value.getString() ) );
                        valueElement.addAttribute( new QName( "type", xsiNamespace ), "xsd:"
                            + ParserUtils.BASE64BINARY );
                    }
                    else
                    {
                        modElement.addElement( "value" ).setText( value.getString() );
                    }
                }
            }
        }

        ModificationOperation operation = modification.getOperation();

        if ( operation == ModificationOperation.ADD_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "add" );
        }
        else if ( operation == ModificationOperation.REPLACE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "replace" );
        }
        else if ( operation == ModificationOperation.REMOVE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "delete" );
        }
        else if ( operation == ModificationOperation.INCREMENT_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "increment" );
        }
    }

    return element;
}