use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class AbandonRequestDecorator method encode.
// -------------------------------------------------------------------------
// The Decorator methods
// -------------------------------------------------------------------------
/**
* Encode the Abandon protocolOp part
*/
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
try {
// The tag
buffer.put(LdapCodecConstants.ABANDON_REQUEST_TAG);
// The length. It has to be evaluated depending on
// the abandoned messageId value.
buffer.put((byte) BerValue.getNbBytes(getAbandoned()));
// The abandoned messageId
buffer.put(BerValue.getBytes(getAbandoned()));
} catch (BufferOverflowException boe) {
String msg = I18n.err(I18n.ERR_04005);
throw new EncoderException(msg, boe);
}
return buffer;
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class ValueTest method testEncodeBitString.
@Test
public void testEncodeBitString() {
BitString bs = new BitString(10);
bs.setBit(9);
ByteBuffer buffer = ByteBuffer.allocate(5);
try {
BerValue.encode(buffer, bs);
} catch (EncoderException ee) {
fail();
}
assertEquals("0x03 0x03 0x06 0x00 0x40 ", Asn1StringUtils.dumpBytes(buffer.array()));
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class LdapEncoder method encodeMessage.
/**
* Generate the PDU which contains the encoded object.
*
* The generation is done in two phases :
* - first, we compute the length of each part and the
* global PDU length
* - second, we produce the PDU.
*
* <pre>
* 0x30 L1
* |
* +--> 0x02 L2 MessageId
* +--> ProtocolOp
* +--> Controls
*
* L2 = Length(MessageId)
* L1 = Length(0x02) + Length(L2) + L2 + Length(ProtocolOp) + Length(Controls)
* LdapMessageLength = Length(0x30) + Length(L1) + L1
* </pre>
*
* @param message The message to encode
* @return A ByteBuffer that contains the PDU
* @throws EncoderException If anything goes wrong.
*/
public ByteBuffer encodeMessage(Message message) throws EncoderException {
MessageDecorator<? extends Message> decorator = MessageDecorator.getDecorator(codec, message);
int length = computeMessageLength(decorator);
ByteBuffer buffer = ByteBuffer.allocate(length);
try {
try {
// The LdapMessage Sequence
buffer.put(UniversalTag.SEQUENCE.getValue());
// The length has been calculated by the computeLength method
buffer.put(TLV.getBytes(decorator.getMessageLength()));
} catch (BufferOverflowException boe) {
throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
}
// The message Id
BerValue.encode(buffer, message.getMessageId());
// Add the protocolOp part
decorator.encode(buffer);
// Do the same thing for Controls, if any.
Map<String, Control> controls = decorator.getControls();
if ((controls != null) && (controls.size() > 0)) {
// Encode the controls
buffer.put((byte) LdapCodecConstants.CONTROLS_TAG);
buffer.put(TLV.getBytes(decorator.getControlsLength()));
// Encode each control
for (Control control : controls.values()) {
encodeControl(buffer, control);
// The OctetString tag if the value is not null
int controlValueLength = ((CodecControl<?>) control).computeLength();
if (controlValueLength > 0) {
buffer.put(UniversalTag.OCTET_STRING.getValue());
buffer.put(TLV.getBytes(controlValueLength));
// And now, the value
((org.apache.directory.api.ldap.codec.api.CodecControl<?>) control).encode(buffer);
}
}
}
} catch (EncoderException ee) {
throw new MessageEncoderException(message.getMessageId(), ee.getMessage(), ee);
}
buffer.flip();
return buffer;
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class SortRequestDecorator method encode.
/**
* {@inheritDoc}
*/
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
if (buffer == null) {
throw new EncoderException(I18n.err(I18n.ERR_04023));
}
buffer.put(UniversalTag.SEQUENCE.getValue());
buffer.put(TLV.getBytes(sortReqLen));
List<SortKey> lst = getSortKeys();
for (int i = 0; i < lst.size(); i++) {
SortKey sk = lst.get(i);
int skLen = sortKeyLenList.get(i);
buffer.put(UniversalTag.SEQUENCE.getValue());
buffer.put(TLV.getBytes(skLen));
BerValue.encode(buffer, sk.getAttributeTypeDesc());
String mrId = sk.getMatchingRuleId();
if (mrId != null) {
buffer.put((byte) ORDERING_RULE_TAG);
byte[] value = Asn1StringUtils.getBytesUtf8(mrId);
buffer.put(TLV.getBytes(value.length));
buffer.put(value);
}
if (sk.isReverseOrder()) {
buffer.put((byte) REVERSE_ORDER_TAG);
buffer.put((byte) 0x01);
buffer.put(BerValue.TRUE_VALUE);
}
}
return buffer;
}
use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.
the class CancelRequestTest method testDecodeCancel.
/**
* Test the normal Cancel message
*/
@Test
public void testDecodeCancel() {
Asn1Decoder cancelDecoder = new CancelDecoder();
ByteBuffer stream = ByteBuffer.allocate(0x05);
stream.put(new byte[] { 0x30, 0x03, 0x02, 0x01, 0x01 }).flip();
String decodedPdu = Strings.dumpBytes(stream.array());
// Allocate a Cancel Container
Asn1Container cancelContainer = new CancelContainer();
// Decode a Cancel message
try {
cancelDecoder.decode(stream, cancelContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
CancelRequestDecorator cancel = ((CancelContainer) cancelContainer).getCancel();
assertEquals(1, cancel.getCancelId());
// Check the encoding
try {
ByteBuffer bb = cancel.encodeInternal();
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations