Search in sources :

Example 1 with MessageDecorator

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

the class LdapProtocolDecoder method decode.

/**
 * {@inheritDoc}
 */
@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    @SuppressWarnings("unchecked") LdapMessageContainer<MessageDecorator<? extends Message>> messageContainer = (LdapMessageContainer<MessageDecorator<? extends Message>>) session.getAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR);
    if (session.containsAttribute(LdapDecoder.MAX_PDU_SIZE_ATTR)) {
        int maxPDUSize = (Integer) session.getAttribute(LdapDecoder.MAX_PDU_SIZE_ATTR);
        messageContainer.setMaxPDUSize(maxPDUSize);
    }
    List<Message> decodedMessages = new ArrayList<>();
    ByteBuffer buf = in.buf();
    decode(buf, messageContainer, decodedMessages);
    for (Message message : decodedMessages) {
        out.write(message);
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 2 with MessageDecorator

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

the class LdapResultTest method testDecodeAddResponseEmptyResultCodeEmptyReferrals.

/**
 * Test the decoding of a AddResponse with a valid LdapResult and an invalid
 * transition after the referral sequence
 */
@Test
public void testDecodeAddResponseEmptyResultCodeEmptyReferrals() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x10);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x0E, 0x02, 0x01, // messageID MessageID
    0x01, 0x69, // CHOICE { ..., addResponse AddResponse, ...
    0x09, 0x0A, 0x01, // resultCode success (Referral)
    0x0A, 0x04, // Empty matched Dn
    0x00, 0x04, // Empty errorMessage
    0x00, (byte) 0xA3, 0x00 });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<MessageDecorator<? extends Message>> container = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode the AddResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } 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) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with MessageDecorator

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

the class AbandonRequestTest method testDecodeAbandonRequestNoMessageId.

/**
 * Test the decoding of a AbandonRequest with a null messageId
 */
@Test
public void testDecodeAbandonRequestNoMessageId() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0A);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x08, 0x02, 0x01, // messageID MessageID
    0x01, 0x50, // CHOICE { ..., abandonRequest AbandonRequest,...
    0x00 // AbandonRequest ::= [APPLICATION 16] MessageID
    });
    stream.flip();
    // Allocate a LdapMessageContainer Container
    LdapMessageContainer<MessageDecorator<? extends Message>> ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode the PDU
    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) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 4 with MessageDecorator

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

the class AbandonRequestTest method testDecodeAbandonRequestBadMessageId.

/**
 * Test the decoding of a AbandonRequest with a bad Message Id
 */
@Test
public void testDecodeAbandonRequestBadMessageId() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0B);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x09, 0x02, 0x01, // messageID MessageID
    0x01, 0x50, 0x01, // CHOICE { ..., abandonRequest AbandonRequest,...
    (byte) 0xFF // AbandonRequest ::= [APPLICATION 16] MessageID
    });
    stream.flip();
    // Allocate a LdapMessageContainer Container
    LdapMessageContainer<MessageDecorator<? extends Message>> ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode the PDU
    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) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 5 with MessageDecorator

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

the class LdapDecoderTest method testDecode2Messages.

/**
 * Test the decoding of two messages in a PDU
 */
@Test
public void testDecode2Messages() throws Exception {
    LdapMessageContainer<MessageDecorator<? extends Message>> container = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    IoSession dummySession = new DummySession();
    dummySession.setAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR, container);
    ByteBuffer stream = ByteBuffer.allocate(0x6A);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x33, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', (byte) 0x80, // authentication
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd', 0x30, // LDAPMessage ::=SEQUENCE {
    0x33, 0x02, 0x01, // messageID MessageID
    0x02, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', (byte) 0x80, // authentication
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
    stream.flip();
    List<Message> result = new ArrayList<Message>();
    // Decode a BindRequest PDU
    try {
        decode(stream, container, result);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded PDU
    BindRequest bindRequest = (BindRequest) (result.get(0));
    assertEquals(1, bindRequest.getMessageId());
    assertTrue(bindRequest.isVersion3());
    assertEquals("uid=akarasulu,dc=example,dc=com", bindRequest.getName().toString());
    assertTrue(bindRequest.isSimple());
    assertEquals("password", Strings.utf8ToString(bindRequest.getCredentials()));
    // The second message
    bindRequest = (BindRequest) (result.get(1));
    assertEquals(2, bindRequest.getMessageId());
    assertTrue(bindRequest.isVersion3());
    assertEquals("uid=akarasulu,dc=example,dc=com", bindRequest.getName().toString());
    assertTrue(bindRequest.isSimple());
    assertEquals("password", Strings.utf8ToString(bindRequest.getCredentials()));
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) ArrayList(java.util.ArrayList) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) DummySession(org.apache.mina.core.session.DummySession) ByteBuffer(java.nio.ByteBuffer) IoSession(org.apache.mina.core.session.IoSession) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)11 Message (org.apache.directory.api.ldap.model.message.Message)11 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)10 ByteBuffer (java.nio.ByteBuffer)8 DecoderException (org.apache.directory.api.asn1.DecoderException)8 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)7 Test (org.junit.Test)7 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)6 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)3 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)2 ArrayList (java.util.ArrayList)2 SchemaBinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector)2 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)2 InvalidConnectionException (org.apache.directory.ldap.client.api.exception.InvalidConnectionException)2 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)1 BinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector)1