Search in sources :

Example 6 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyAttributes.

/**
 * Test the decoding of an SearchResultEntry with an empty attributes
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributes() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x26);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x24, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x1F, // 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', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultEntry.getMessageId());
    assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
    Entry entry = searchResultEntry.getEntry();
    assertEquals(0, entry.size());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x26, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 7 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyAttributeList.

/**
 * Test the decoding of an SearchResultEntry with an empty attributes list
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributeList() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x28);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x26, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x21, // 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', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x02, 0x30, 0x00 });
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 8 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntrySuccess.

/**
 * Test the decoding of a SearchResultEntry
 */
@Test
public void testDecodeSearchResultEntrySuccess() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x50);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x4e, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x49, // 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', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x2a, 0x30, 0x28, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // vals SET OF AttributeValue }
    0x31, 0x19, // AttributeValue ::= OCTET STRING
    0x04, 0x03, 't', 'o', 'p', // AttributeValue ::= OCTET STRING
    0x04, 0x12, 'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'a', 'l', 'U', 'n', 'i', 't' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    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 the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x50, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 9 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyType.

/**
 * Test the decoding of a SearchResultEntry with an empty type
 */
@Test
public void testDecodeSearchResultEntryEmptyType() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2A);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x28, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x23, // 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', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x04, 0x30, 0x02, // type AttributeDescription,
    0x04, 0x00 });
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 10 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class AddAttributeType method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<SearchResultEntryDecorator> container) throws DecoderException {
    SearchResultEntryDecorator searchResultEntry = container.getMessage();
    TLV tlv = container.getCurrentTLV();
    // Store the type
    if (tlv.getLength() == 0) {
        // The type can't be null
        String msg = I18n.err(I18n.ERR_04081);
        LOG.error(msg);
        throw new DecoderException(msg);
    } else {
        try {
            searchResultEntry.addAttribute(tlv.getValue().getData());
        } catch (LdapException ine) {
            String type = Strings.utf8ToString(tlv.getValue().getData());
            // This is for the client side. We will never decode LdapResult on the server
            String msg = "The Attribute type " + type + "is invalid : " + ine.getMessage();
            LOG.error("{} : {}", msg, ine.getMessage());
            throw new DecoderException(msg, ine);
        }
    }
    if (IS_DEBUG) {
        String type = Strings.utf8ToString(tlv.getValue().getData());
        LOG.debug("Attribute type : {}", type);
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Aggregations

SearchResultEntryDecorator (org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator)21 DecoderException (org.apache.directory.api.asn1.DecoderException)18 ByteBuffer (java.nio.ByteBuffer)16 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)16 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)16 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)16 Test (org.junit.Test)16 SearchResultEntry (org.apache.directory.api.ldap.model.message.SearchResultEntry)10 EncoderException (org.apache.directory.api.asn1.EncoderException)9 Entry (org.apache.directory.api.ldap.model.entry.Entry)9 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)8 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)3 Control (org.apache.directory.api.ldap.model.message.Control)3 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)2 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)2 AbandonRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator)1 AddRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator)1 AddResponseDecorator (org.apache.directory.api.ldap.codec.decorators.AddResponseDecorator)1 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)1 BindResponseDecorator (org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator)1