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

The following examples show how to use org.apache.directory.api.ldap.model.entry.Entry#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: SearchResultEntryFactory.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Encode the SearchResultEntry message to a PDU.
 * <br>
 * SearchResultEntry :
 * <pre>
 * 0x64 LL
 *   0x04 LL objectName
 *   0x30 LL attributes
 *     0x30 LL partialAttributeList
 *       0x04 LL type
 *       0x31 LL vals
 *         0x04 LL attributeValue
 *         ...
 *         0x04 LL attributeValue
 *     ...
 *     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 message the SearchResultEntry to encode
 */
@Override
public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
{
    int start = buffer.getPos();

    SearchResultEntry searchResultEntry = ( SearchResultEntry ) message;

    // The partial attribute list
    Entry entry = searchResultEntry.getEntry();

    // The attributes, recursively, if we have any
    if ( ( entry != null ) && ( entry.size() != 0 ) )
    {
        encodeAttributes( buffer, entry.iterator() );
    }

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

    // The objectName
    BerValue.encodeOctetString( buffer, searchResultEntry.getObjectName().getName() );

    // The SearchResultEntry tag
    BerValue.encodeSequence( buffer, LdapCodecConstants.SEARCH_RESULT_ENTRY_TAG, start );
}
 
Example 2
Source File: AddRequestFactory.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Encode the AddRequest message to a PDU.
 * <br>
 * AddRequest :
 * <pre>
 * 0x68 LL
 *   0x04 LL entry
 *   0x30 LL attributesList
 *     0x30 LL attribute
 *       0x04 LL attributeDescription
 *       0x31 LL attributeValues
 *         0x04 LL attributeValue
 *         ...
 *         0x04 LL attributeValue
 *     ...
 *     0x30 LL attribute
 *       0x04 LL attributeDescription
 *       0x31 LL attributeValue
 *         0x04 LL attributeValue
 *         ...
 *         0x04 LL attributeValue
 * </pre>
 *
 * @param codec The LdapApiService instance
 * @param buffer The buffer where to put the PDU
 * @param message the AbandonRequest to encode
 */
@Override
public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
{
    int start = buffer.getPos();
    AddRequest addRequest = ( AddRequest ) message;

    // The partial attribute list
    Entry entry = addRequest.getEntry();

    if ( entry.size() != 0 )
    {
        // Encode the attributes
        encodeAttributeReverse( buffer, entry.iterator() );
    }

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

    // The entry DN
    BerValue.encodeOctetString( buffer, entry.getDn().getName() );

    // The AddRequest Tag
    BerValue.encodeSequence( buffer, LdapCodecConstants.ADD_REQUEST_TAG, start );
}
 
Example 3
Source File: SearchResultEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchResultEntry
 */
@Test
public void testDecodeSearchResultEntrySuccess() throws DecoderException, EncoderException, LdapException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x50 );

    stream.put( new byte[]
        {
            0x30, 0x4e,                     // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,             // messageID MessageID
              0x64, 0x49,                   // CHOICE { ..., searchResEntry SearchResultEntry,
                                            // ...
                                            // SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
                                            // objectName LDAPDN,
                0x04, 0x1b,
                  'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',',
                  'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm',
                                            // attributes PartialAttributeList }
                                            // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
                0x30, 0x2a,
                  0x30, 0x28,
                    0x04, 0x0b,             // type AttributeDescription,
                      'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                    0x31, 0x19,             // vals SET OF AttributeValue }
                      0x04, 0x03,           // AttributeValue ::= OCTET STRING
                        't', 'o', 'p',
                      0x04, 0x12,           // AttributeValue ::= OCTET STRING
                        'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'a', 'l', 'U', 'n', 'i', 't',
         } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntry> ldapMessageContainer =
        new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();

    assertEquals( 1, searchResultEntry.getMessageId() );
    assertEquals( "ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString() );

    Entry entry = searchResultEntry.getEntry();

    assertEquals( 1, entry.size() );

    for ( int i = 0; i < entry.size(); i++ )
    {
        Attribute attribute = entry.get( "objectclass" );

        assertEquals( Strings.toLowerCaseAscii( "objectClass" ), Strings.toLowerCaseAscii( attribute.getUpId() ) );

        assertTrue( attribute.contains( "top" ) );
        assertTrue( attribute.contains( "organizationalUnit" ) );
    }

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

    LdapEncoder.encodeMessage( buffer, codec, searchResultEntry );

    assertTrue( Arrays.equals( stream.array(), buffer.getBytes().array() ) );
}
 
Example 4
Source File: SearchResultEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchResultEntry with more bytes to be decoded at
 * the end
 */
@Test
public void testDecodeSearchResultEntrySuccessWithFollowingMessage() throws DecoderException, EncoderException, LdapException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x66 );

    stream.put( new byte[]
        {
            0x30, 0x5F,                     // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x02,             // messageID MessageID
              0x64, 0x5A,                   // CHOICE { ..., searchResEntry SearchResultEntry,
                                            // ...
                                            // SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
                                            // objectName LDAPDN,
                0x04, 0x13,
                  'u', 'i', 'd', '=', 'a', 'd', 'm', 'i', 'n', ',',
                  'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm',
                                            // attributes PartialAttributeList }
                0x30, 0x43,                 // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
                  0x30, 0x41,
                    0x04, 0x0B,             // type AttributeDescription,
                      'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                    0x31, 0x32,             // vals SET OF AttributeValue }
                      0x04, 0x0D,           // AttributeValue ::= OCTET STRING
                        'i', 'n', 'e', 't', 'O', 'r', 'g', 'P', 'e', 'r', 's', 'o', 'n',
                      0x04, 0x14,           // AttributeValue ::= OCTET STRING
                        'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a',
                        't', 'i', 'o', 'n', 'a', 'l', 'P', 'e',
                        'r', 's', 'o', 'n',
                      0x04, 0x06,           // AttributeValue ::= OCTET STRING
                        'p', 'e', 'r', 's', 'o', 'n',
                      0x04, 0x03,           // AttributeValue ::= OCTET STRING
                        't', 'o', 'p',
            0x30, 0x45, // Start of the next message
              0x02, 0x01, 0x03 // messageID MessageID ...
        } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntry> ldapMessageContainer =
        new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();

    assertEquals( 2, searchResultEntry.getMessageId() );
    assertEquals( "uid=admin,ou=system", searchResultEntry.getObjectName().toString() );

    Entry entry = searchResultEntry.getEntry();

    assertEquals( 1, entry.size() );

    for ( int i = 0; i < entry.size(); i++ )
    {
        Attribute attribute = entry.get( "objectclass" );

        assertEquals( Strings.toLowerCaseAscii( "objectClass" ), Strings.toLowerCaseAscii( attribute.getUpId() ) );

        assertTrue( attribute.contains( "top" ) );
        assertTrue( attribute.contains( "person" ) );
        assertTrue( attribute.contains( "organizationalPerson" ) );
        assertTrue( attribute.contains( "inetOrgPerson" ) );
    }

    // Check that the next bytes is the first of the next PDU
    assertEquals( 0x30, stream.get( stream.position() ) );
    assertEquals( 0x45, stream.get( stream.position() + 1 ) );
    assertEquals( 0x02, stream.get( stream.position() + 2 ) );
    assertEquals( 0x01, stream.get( stream.position() + 3 ) );
    assertEquals( 0x03, stream.get( stream.position() + 4 ) );

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

    LdapEncoder.encodeMessage( buffer, codec, searchResultEntry );

    // We can't compare the encodings, the order of the attributes has
    // changed
    LdapMessageContainer<SearchResultEntry> ldapMessageContainer2 =
        new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( buffer.getBytes(), ldapMessageContainer2 );

    assertEquals( searchResultEntry.getEntry(), ldapMessageContainer2.getMessage().getEntry() );
    assertEquals( searchResultEntry.getObjectName(), ldapMessageContainer2.getMessage().getObjectName() );
}
 
Example 5
Source File: SearchResultEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchResultEntry with an empty vals
 */
@Test
public void testDecodeSearchResultEntryEmptyVals() throws DecoderException, EncoderException, LdapException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x37 );

    stream.put( new byte[]
        {
            0x30, 0x35,                     // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,             // messageID MessageID
              0x64, 0x30,                   // CHOICE { ..., searchResEntry SearchResultEntry,
                                            // ...
                                            // SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
                                            // objectName LDAPDN,
                0x04, 0x1b,
                  'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',',
                  'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm',
                                            // attributes PartialAttributeList }
                                            // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
                0x30, 0x11,
                  0x30, 0x0F,
                    0x04, 0x0b,             // type AttributeDescription,
                      'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                    0x31, 0x00
        } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntry> ldapMessageContainer =
        new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();

    assertEquals( 1, searchResultEntry.getMessageId() );
    assertEquals( "ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString() );

    Entry entry = searchResultEntry.getEntry();

    assertEquals( 1, entry.size() );

    for ( int i = 0; i < entry.size(); i++ )
    {
        Attribute attribute = entry.get( "objectclass" );

        assertEquals( Strings.toLowerCaseAscii( "objectClass" ), Strings.toLowerCaseAscii( attribute.getUpId() ) );
        assertEquals( 0, attribute.size() );
    }

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

    ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, searchResultEntry );

    assertArrayEquals( stream.array(), result.array() );
}
 
Example 6
Source File: SearchResultEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchResultEntry with an empty vals with controls
 */
@Test
public void testDecodeSearchResultEntryEmptyValsWithControls() throws DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x5B );

    stream.put( new byte[]
        {
            0x30, 0x59,                     // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,             // messageID MessageID
              0x64, 0x30,                   // CHOICE { ..., searchResEntry SearchResultEntry,
                                            // ...
                                            // SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
                                            // objectName LDAPDN,
                0x04, 0x1b,
                  'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',',
                  'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm',
                                            // attributes PartialAttributeList }
                                            // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
                0x30, 0x11,
                  0x30, 0x0F,
                    0x04, 0x0b,             // type AttributeDescription,
                      'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                    0x31, 0x00,
              ( byte ) 0xA0, 0x22,          // A control
                0x30, 0x20,
                  0x04, 0x17,               // EntryChange response control
                    '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', 
                    '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '7',
                  0x04, 0x05,               // Control value
                    0x30, 0x03,             // EntryChangeNotification ::= SEQUENCE {
                      0x0A, 0x01, 0x01      //     changeType ENUMERATED {
                                            //         add             (1),
        } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntry> ldapMessageContainer =
        new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();

    assertEquals( 1, searchResultEntry.getMessageId() );
    assertEquals( "ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString() );

    Entry entry = searchResultEntry.getEntry();

    assertEquals( 1, entry.size() );

    for ( int i = 0; i < entry.size(); i++ )
    {
        Attribute attribute = entry.get( "objectclass" );

        assertEquals( Strings.toLowerCaseAscii( "objectClass" ), Strings.toLowerCaseAscii( attribute.getUpId() ) );

        assertEquals( 0, attribute.size() );
    }

    // Check the Control
    Map<String, Control> controls = searchResultEntry.getControls();

    assertEquals( 1, controls.size() );

    Control control = controls.get( "2.16.840.1.113730.3.4.7" );
    assertEquals( "2.16.840.1.113730.3.4.7", control.getOid() );
    assertTrue( control instanceof EntryChange );

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

    ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, searchResultEntry );

    assertArrayEquals( stream.array(), result.array() );
}
 
Example 7
Source File: SearchResultEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchResultEntry with an empty attribute value
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributeValue()
    throws DecoderException, EncoderException, LdapException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x39 );

    stream.put( new byte[]
        {
            0x30, 0x37,                     // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,             // messageID MessageID
              0x64, 0x32,                   // CHOICE { ..., searchResEntry SearchResultEntry,
                                            // ...
                                            // SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
                                            // objectName LDAPDN,
                0x04, 0x1b,
                  'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',',
                  'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm',
                                            // attributes PartialAttributeList }
                                            // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
                0x30, 0x13,
                  0x30, 0x11,
                    0x04, 0x0b,             // type AttributeDescription,
                      'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x31, 0x02,               // vals SET OF AttributeValue }
                    0x04, 0x00              // AttributeValue ::= OCTET STRING
        } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntry> ldapMessageContainer =
        new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();

    assertEquals( 1, searchResultEntry.getMessageId() );
    assertEquals( "ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString() );

    Entry entry = searchResultEntry.getEntry();

    assertEquals( 1, entry.size() );

    for ( int i = 0; i < entry.size(); i++ )
    {
        Attribute attribute = entry.get( "objectclass" );

        assertEquals( Strings.toLowerCaseAscii( "objectClass" ), Strings.toLowerCaseAscii( attribute.getUpId() ) );

        assertTrue( attribute.contains( "" ) );
    }

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

    ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, searchResultEntry );

    assertArrayEquals( stream.array(), result.array() );
}
 
Example 8
Source File: SearchResultEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchResultEntry with an empty attribute value
 * with controls
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributeValueWithControls() throws DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x5D );

    stream.put( new byte[]
        {
            0x30, 0x5B,                     // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,             // messageID MessageID
              0x64, 0x32,                   // CHOICE { ..., searchResEntry SearchResultEntry,
                                            // ...
                                            // SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
                                            // objectName LDAPDN,
                0x04, 0x1b,
                  'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',',
                  'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm',
                                            // attributes PartialAttributeList }
                                            // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
                0x30, 0x13,
                  0x30, 0x11,
                    0x04, 0x0b,             // type AttributeDescription,
                      'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                    0x31, 0x02,             // vals SET OF AttributeValue }
                      0x04, 0x00,           // AttributeValue ::= OCTET STRING
              ( byte ) 0xA0, 0x22,          // A control
                0x30, 0x20,
                  0x04, 0x17,               // EntryChange response control
                    '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', 
                    '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '7',
                  0x04, 0x05,               // Control value
                    0x30, 0x03,             // EntryChangeNotification ::= SEQUENCE {
                      0x0A, 0x01, 0x01      //     changeType ENUMERATED {
                                            //         add             (1),
        } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntry> ldapMessageContainer =
        new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();

    assertEquals( 1, searchResultEntry.getMessageId() );
    assertEquals( "ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString() );

    Entry entry = searchResultEntry.getEntry();

    assertEquals( 1, entry.size() );

    for ( int i = 0; i < entry.size(); i++ )
    {
        Attribute attribute = entry.get( "objectclass" );

        assertEquals( Strings.toLowerCaseAscii( "objectClass" ), Strings.toLowerCaseAscii( attribute.getUpId() ) );

        assertTrue( attribute.contains( "" ) );
    }

    // Check the Control
    Map<String, Control> controls = searchResultEntry.getControls();

    assertEquals( 1, controls.size() );

    Control control = controls.get( "2.16.840.1.113730.3.4.7" );
    assertEquals( "2.16.840.1.113730.3.4.7", control.getOid() );
    assertTrue( control instanceof EntryChange );

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

    ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, searchResultEntry );

    assertArrayEquals( stream.array(), result.array() );
}