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);
}
}
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");
}
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");
}
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");
}
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()));
}
Aggregations