use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class Asn1Decoder method treatValueStartState.
/**
* Treat the Value part. We will distinguish two cases : - if the Tag is a
* Primitive one, we will get the value. - if the Tag is a Constructed one,
* nothing will be done.
*
* @param stream The ByteBuffer containing the PDU to decode
* @param container The container that stores the current state,
* the result and other informations.
* @return <code>true</code> if there are more bytes to read, <code>false
* </code> otherwise
*/
private boolean treatValueStartState(ByteBuffer stream, Asn1Container container) {
TLV currentTlv = container.getCurrentTLV();
if (TLV.isConstructed(currentTlv.getTag()) && !container.isGathering()) {
container.setState(TLVStateEnum.TLV_STATE_DONE);
return MORE;
} else {
int length = currentTlv.getLength();
int nbBytes = stream.remaining();
if (nbBytes < length) {
currentTlv.getValue().init(length);
currentTlv.getValue().setData(stream);
container.setState(TLVStateEnum.VALUE_STATE_PENDING);
return END;
} else {
currentTlv.getValue().init(length);
stream.get(currentTlv.getValue().getData(), 0, length);
container.setState(TLVStateEnum.TLV_STATE_DONE);
return MORE;
}
}
}
use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class SearchRequestDecorator method unstackFilters.
/**
* This method is used to clear the filter's stack for terminated elements. An element
* is considered as terminated either if :
* - it's a final element (ie an element which cannot contains a Filter)
* - its current length equals its expected length.
*
* @param container The container being decoded
*/
@SuppressWarnings("unchecked")
public void unstackFilters(Asn1Container container) {
LdapMessageContainer<MessageDecorator<Message>> ldapMessageContainer = (LdapMessageContainer<MessageDecorator<Message>>) container;
TLV tlv = ldapMessageContainer.getCurrentTLV();
TLV localParent = tlv.getParent();
Filter localFilter = terminalFilter;
// The parent has been completed, so fold it
while ((localParent != null) && (localParent.getExpectedLength() == 0)) {
int parentTlvId = localFilter.getParent() != null ? localFilter.getParent().getTlvId() : localFilter.getParentTlvId();
if (localParent.getId() != parentTlvId) {
localParent = localParent.getParent();
} else {
Filter filterParent = localFilter.getParent();
// pushed on the stack, so we need to get its parent's parent
if (localFilter instanceof PresentFilter) {
if (filterParent == null) {
// We don't have parent, get out
break;
}
filterParent = filterParent.getParent();
} else {
filterParent = filterParent.getParent();
}
if (filterParent != null) {
// The parent is a filter ; it will become the new currentFilter
// and we will loop again.
localFilter = currentFilter;
currentFilter = filterParent;
localParent = localParent.getParent();
} else {
// We can stop the recursion, we have reached the searchResult Object
break;
}
}
}
}
use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class CancelRequestTest method testDecodeCancelNoCancelId.
/**
* Test a Cancel message with no cancelId
*/
@Test
public void testDecodeCancelNoCancelId() {
Asn1Decoder cancelDecoder = new CancelDecoder();
ByteBuffer stream = ByteBuffer.allocate(0x02);
stream.put(new byte[] { 0x30, 0x00 }).flip();
// Allocate a Cancel Container
Asn1Container cancelContainer = new CancelContainer();
// Decode a Cancel message
try {
cancelDecoder.decode(stream, cancelContainer);
fail("CancelID expected");
} catch (DecoderException de) {
assertTrue(true);
}
}
use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class WhoAmIResponseTest method testDecodeWhoAmINoWhoAmIAuthzIdUserId.
/**
* Test a WhoAmI message with a UserId authzId
*/
@Test
public void testDecodeWhoAmINoWhoAmIAuthzIdUserId() {
Asn1Decoder whoAmIResponseDecoder = new WhoAmIResponseDecoder();
ByteBuffer stream = ByteBuffer.allocate(0x09);
stream.put(new byte[] { 0x04, 0x07, 'u', ':', 't', 'e', 's', 't', 0x00 }).flip();
String decodedPdu = Strings.dumpBytes(stream.array());
// Allocate a WhoAmI Container
Asn1Container whoAmIResponseContainer = new WhoAmIResponseContainer();
// Decode a WhoAmI message
try {
whoAmIResponseDecoder.decode(stream, whoAmIResponseContainer);
} catch (DecoderException de) {
fail();
}
WhoAmIResponseDecorator whoAmIResponse = ((WhoAmIResponseContainer) whoAmIResponseContainer).getWhoAmIResponse();
assertNotNull(whoAmIResponse.getAuthzId());
// Check the encoding
try {
ByteBuffer bb = whoAmIResponse.encodeInternal();
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class WhoAmIResponseTest method testDecodeWhoAmINoWhoAmIAuthzIdEmpty.
/**
* Test a WhoAmI message with no authzId
*/
@Test
public void testDecodeWhoAmINoWhoAmIAuthzIdEmpty() {
Asn1Decoder whoAmIResponseDecoder = new WhoAmIResponseDecoder();
ByteBuffer stream = ByteBuffer.allocate(0x02);
stream.put(new byte[] { 0x04, 0x00 }).flip();
String decodedPdu = Strings.dumpBytes(stream.array());
// Allocate a WhoAmI Container
Asn1Container whoAmIResponseContainer = new WhoAmIResponseContainer();
// Decode a WhoAmI message
try {
whoAmIResponseDecoder.decode(stream, whoAmIResponseContainer);
} catch (DecoderException de) {
fail();
}
WhoAmIResponseDecorator whoAmIResponse = ((WhoAmIResponseContainer) whoAmIResponseContainer).getWhoAmIResponse();
assertNull(whoAmIResponse.getAuthzId());
// Check the encoding
try {
ByteBuffer bb = whoAmIResponse.encodeInternal();
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations