use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class LdapControlTest method testDecodeRequestWithEmptyControls.
/**
* Test the decoding of a Request with an empty list of controls
*/
@Test
public void testDecodeRequestWithEmptyControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x0A);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x08, // messageID MessageID
0x02, // messageID MessageID
0x01, // messageID MessageID
0x03, // CHOICE { ..., abandonRequest
0x50, // CHOICE { ..., abandonRequest
0x01, // CHOICE { ..., abandonRequest
0x02, // controls [0] Controls OPTIONAL }
(byte) 0xA0, // controls [0] Controls OPTIONAL }
0x00 });
stream.flip();
// Allocate a LdapMessageContainer Container
LdapMessageContainer<AbandonRequestDecorator> ldapMessageContainer = new LdapMessageContainer<AbandonRequestDecorator>(codec);
// Decode the PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check that everything is OK
AbandonRequestDecorator abandonRequest = ldapMessageContainer.getMessage();
// Copy the message
AbandonRequest internalAbandonRequest = new AbandonRequestImpl(abandonRequest.getAbandoned());
internalAbandonRequest.setMessageId(abandonRequest.getMessageId());
assertEquals(3, abandonRequest.getMessageId());
assertEquals(2, abandonRequest.getAbandoned());
// Check the Controls
Map<String, Control> controls = abandonRequest.getControls();
assertEquals(0, controls.size());
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(internalAbandonRequest);
// Check the length, which should be 2 bytes shorter, as we don't encode teh empty control
assertEquals(0x08, bb.limit());
// So we decode the generated PDU, and we compare it with the initial message
try {
ldapDecoder.decode(bb, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
AbandonRequest abandonRequest2 = ldapMessageContainer.getMessage();
assertEquals(abandonRequest, abandonRequest2);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class LdapMessageTest method testDecodeUnBindRequestNoControls.
/**
* Test the decoding of a LdapMessage with a large MessageId
*/
@Test
public void testDecodeUnBindRequestNoControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x08);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x06, 0x02, 0x02, 0x01, // messageID MessageID (500)
(byte) 0xF4, 0x42, // CHOICE { ..., unbindRequest UnbindRequest,...
0x00 // UnbindRequest ::= [APPLICATION 2] NULL
});
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a BindRequest Container
LdapMessageContainer<UnbindRequestDecorator> container = new LdapMessageContainer<UnbindRequestDecorator>(codec);
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
Message message = container.getMessage();
assertEquals(500, message.getMessageId());
// Check the length
UnbindRequest internalUnbindRequest = new UnbindRequestImpl();
internalUnbindRequest.setMessageId(message.getMessageId());
try {
ByteBuffer bb = encoder.encodeMessage(internalUnbindRequest);
// Check the length
assertEquals(0x08, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class IntermediateResponseDecorator method encode.
/**
* Encode the IntermediateResponse message to a PDU.
* <br>
* IntermediateResponse :
* <pre>
* 0x79 LL
* [0x80 LL response name]
* [0x81 LL responseValue]
* </pre>
*
* @param buffer The buffer where to put the PDU
*/
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
try {
// The ExtendedResponse Tag
buffer.put(LdapCodecConstants.INTERMEDIATE_RESPONSE_TAG);
buffer.put(TLV.getBytes(intermediateResponseLength));
// The responseName, if any
if ((responseNameBytes != null) && (responseNameBytes.length != 0)) {
buffer.put((byte) LdapCodecConstants.INTERMEDIATE_RESPONSE_NAME_TAG);
buffer.put(TLV.getBytes(responseNameBytes.length));
buffer.put(responseNameBytes);
}
// The encodedValue, if any
if (encodedValueBytes != null) {
buffer.put((byte) LdapCodecConstants.INTERMEDIATE_RESPONSE_VALUE_TAG);
buffer.put(TLV.getBytes(encodedValueBytes.length));
if (encodedValueBytes.length != 0) {
buffer.put(encodedValueBytes);
}
}
} catch (BufferOverflowException boe) {
throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
}
return buffer;
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class LdapResultDecorator method encode.
/**
* Encode the LdapResult message to a PDU.
*
* @param buffer The buffer where to put the PDU
* @return The PDU.
*/
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
if (buffer == null) {
throw new EncoderException(I18n.err(I18n.ERR_04023));
}
if (decoratedLdapResult.isDefaultSuccess()) {
// The length of a default success PDU : 0xA0 0x01 0x00 0x04 0x00 0x04 0x00
buffer.put(DEFAULT_SUCCESS);
return buffer;
}
try {
// The result code
BerValue.encodeEnumerated(buffer, getResultCode().getValue());
} catch (BufferOverflowException boe) {
throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
}
// The matchedDN
BerValue.encode(buffer, matchedDnBytes);
// The error message
BerValue.encode(buffer, errorMessageBytes);
// The referrals, if any
Referral referral = getReferral();
if (referral != null) {
LdapEncoder.encodeReferral(buffer, referral);
}
return buffer;
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class ModifyDnRequestDecorator method encode.
/**
* Encode the ModifyDNRequest message to a PDU.
* <br>
* ModifyDNRequest :
* <pre>
* 0x6C LL
* 0x04 LL entry
* 0x04 LL newRDN
* 0x01 0x01 deleteOldRDN
* [0x80 LL newSuperior]
* </pre>
*
* @param buffer The buffer where to put the PDU
* @return The PDU.
*/
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
try {
// The ModifyDNRequest Tag
buffer.put(LdapCodecConstants.MODIFY_DN_REQUEST_TAG);
buffer.put(TLV.getBytes(modifyDnRequestLength));
// The entry
BerValue.encode(buffer, dnBytes);
// The newRDN
BerValue.encode(buffer, getNewRdn().getName());
// The flag deleteOldRdn
BerValue.encode(buffer, getDeleteOldRdn());
// The new superior, if any
if (getNewSuperior() != null) {
// Encode the reference
buffer.put((byte) LdapCodecConstants.MODIFY_DN_REQUEST_NEW_SUPERIOR_TAG);
int newSuperiorLength = newSuperiorBytes.length;
buffer.put(TLV.getBytes(newSuperiorLength));
if (newSuperiorLength != 0) {
buffer.put(newSuperiorBytes);
}
}
} catch (BufferOverflowException boe) {
throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
}
return buffer;
}
Aggregations