use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class LdapControlTest method testDecodeRequestWithControls.
/**
* Test the decoding of a Request with controls
*/
@SuppressWarnings("unchecked")
@Test
public void testDecodeRequestWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x64);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x62, // 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 }
0x5A, // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x1A, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1', // criticality BOOLEAN DEFAULT FALSE,
0x01, // criticality BOOLEAN DEFAULT FALSE,
0x01, // criticality BOOLEAN DEFAULT FALSE,
(byte) 0xFF, // controlValue OCTET STRING OPTIONAL }
0x04, // controlValue OCTET STRING OPTIONAL }
0x06, 'a', 'b', 'c', 'd', 'e', 'f', // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x17, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // controlValue OCTET STRING OPTIONAL }
0x04, // controlValue OCTET STRING OPTIONAL }
0x06, 'g', 'h', 'i', 'j', 'k', 'l', // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x12, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3', // criticality BOOLEAN DEFAULT FALSE}
0x01, // criticality BOOLEAN DEFAULT FALSE}
0x01, // criticality BOOLEAN DEFAULT FALSE}
(byte) 0xFF, // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x0F, // controlType LDAPOID}
0x04, // controlType LDAPOID}
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '4' });
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(4, controls.size());
CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.1");
assertEquals("1.3.6.1.5.5.1", control.getOid());
assertEquals("0x61 0x62 0x63 0x64 0x65 0x66 ", Strings.dumpBytes((byte[]) control.getValue()));
assertTrue(control.isCritical());
internalAbandonRequest.addControl(control);
control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.2");
assertEquals("1.3.6.1.5.5.2", control.getOid());
assertEquals("0x67 0x68 0x69 0x6A 0x6B 0x6C ", Strings.dumpBytes((byte[]) control.getValue()));
assertFalse(control.isCritical());
internalAbandonRequest.addControl(control);
control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.3");
assertEquals("1.3.6.1.5.5.3", control.getOid());
assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
assertTrue(control.isCritical());
internalAbandonRequest.addControl(control);
control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.4");
assertEquals("1.3.6.1.5.5.4", control.getOid());
assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
assertFalse(control.isCritical());
internalAbandonRequest.addControl(control);
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(internalAbandonRequest);
// Check the length
assertEquals(0x64, 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.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class EndTransactionResponseTest method testEndTransactionResponseUpdateControls.
/**
* Test the decoding of a EndTransactionResponse with updateControls
*/
@Test
public void testEndTransactionResponseUpdateControls() throws DecoderException, EncoderException {
Asn1Decoder decoder = new Asn1Decoder();
ByteBuffer bb = ByteBuffer.allocate(0xAC);
bb.put(new byte[] { // EndTransactionResponse ::= SEQUENCE {
0x30, // EndTransactionResponse ::= SEQUENCE {
(byte) 0x81, // EndTransactionResponse ::= SEQUENCE {
(byte) 0xA9, // UpdateControls
0x30, // UpdateControls
(byte) 0x81, // UpdateControls
(byte) 0xA6, // updateControl
0x30, // updateControl
0x5F, // messageID
0x02, // messageID
0x01, // messageID
0x01, // controls
0x30, // controls
0x5A, // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x1A, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1', // criticality BOOLEAN DEFAULT FALSE,
0x01, // criticality BOOLEAN DEFAULT FALSE,
0x01, // criticality BOOLEAN DEFAULT FALSE,
(byte) 0xFF, // controlValue OCTET STRING OPTIONAL }
0x04, // controlValue OCTET STRING OPTIONAL }
0x06, 'a', 'b', 'c', 'd', 'e', 'f', // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x17, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // controlValue OCTET STRING OPTIONAL }
0x04, // controlValue OCTET STRING OPTIONAL }
0x06, 'g', 'h', 'i', 'j', 'k', 'l', // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x12, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3', // criticality BOOLEAN DEFAULT FALSE}
0x01, // criticality BOOLEAN DEFAULT FALSE}
0x01, // criticality BOOLEAN DEFAULT FALSE}
(byte) 0xFF, // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x0F, // controlType LDAPOID}
0x04, // controlType LDAPOID}
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '4', // updateControl
0x30, // updateControl
0x43, // messageID
0x02, // messageID
0x01, // messageID
0x02, // controls
0x30, // controls
0x3E, // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x17, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // controlValue OCTET STRING OPTIONAL }
0x04, // controlValue OCTET STRING OPTIONAL }
0x06, 'g', 'h', 'i', 'j', 'k', 'l', // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x12, // controlType LDAPOID,
0x04, // controlType LDAPOID,
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3', // criticality BOOLEAN DEFAULT FALSE}
0x01, // criticality BOOLEAN DEFAULT FALSE}
0x01, // criticality BOOLEAN DEFAULT FALSE}
(byte) 0xFF, // Control ::= SEQUENCE {
0x30, // Control ::= SEQUENCE {
0x0F, // controlType LDAPOID}
0x04, // controlType LDAPOID}
0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '4' });
String decodedPdu = Strings.dumpBytes(bb.array());
bb.flip();
EndTransactionResponseContainer container = new EndTransactionResponseContainer();
try {
decoder.decode(bb, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
EndTransactionResponse endTransactionResponse = container.getEndTransactionResponse();
assertEquals(-1, endTransactionResponse.getFailedMessageId());
assertEquals(2, endTransactionResponse.getUpdateControls().size());
UpdateControls updateControls1 = endTransactionResponse.getUpdateControls().get(0);
assertEquals(1, updateControls1.getMessageId());
assertNotNull(updateControls1.getControls());
assertEquals(4, updateControls1.getControls().size());
for (Control control : updateControls1.getControls()) {
switch(control.getOid()) {
case "1.3.6.1.5.5.1":
assertTrue(control.isCritical());
assertEquals("abcdef", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
break;
case "1.3.6.1.5.5.2":
assertFalse(control.isCritical());
assertEquals("ghijkl", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
break;
case "1.3.6.1.5.5.3":
assertTrue(control.isCritical());
assertNull(((CodecControl<?>) control).getValue());
break;
case "1.3.6.1.5.5.4":
assertFalse(control.isCritical());
assertNull(((CodecControl<?>) control).getValue());
break;
default:
fail();
break;
}
}
UpdateControls updateControls2 = endTransactionResponse.getUpdateControls().get(1);
assertEquals(2, updateControls2.getMessageId());
assertNotNull(updateControls2.getControls());
assertEquals(3, updateControls2.getControls().size());
for (Control control : updateControls2.getControls()) {
switch(control.getOid()) {
case "1.3.6.1.5.5.2":
assertFalse(control.isCritical());
assertEquals("ghijkl", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
break;
case "1.3.6.1.5.5.3":
assertTrue(control.isCritical());
assertNull(((CodecControl<?>) control).getValue());
break;
case "1.3.6.1.5.5.4":
assertFalse(control.isCritical());
assertNull(((CodecControl<?>) control).getValue());
break;
default:
fail();
break;
}
}
// Check the length
assertEquals(0xAC, ((EndTransactionResponseDecorator) endTransactionResponse).computeLengthInternal());
// Check the encoding
ByteBuffer bb1 = ((EndTransactionResponseDecorator) endTransactionResponse).encodeInternal();
String encodedPdu = Strings.dumpBytes(bb1.array());
assertEquals(encodedPdu, decodedPdu);
}
use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class EndTransactionResponseDecorator method encodeInternal.
/**
* Encodes the EndTransactionResponse extended operation.
*
* @return A ByteBuffer that contains the encoded PDU
* @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
*/
/* No qualifier */
ByteBuffer encodeInternal() throws EncoderException {
ByteBuffer bb = ByteBuffer.allocate(computeLengthInternal());
bb.put(UniversalTag.SEQUENCE.getValue());
bb.put(TLV.getBytes(globalSequenceLength));
// The failed message id, if any
if (getFailedMessageId() >= 0) {
// We have had an error, just encode the messageId
BerValue.encode(bb, getFailedMessageId());
} else {
// No error, just updateControls
bb.put(UniversalTag.SEQUENCE.getValue());
bb.put(TLV.getBytes(updateSequenceLength));
int updateControlsNb = 0;
for (UpdateControls updateControls : getUpdateControls()) {
// The updateControls length
bb.put(UniversalTag.SEQUENCE.getValue());
bb.put(TLV.getBytes(updateControlsLength[updateControlsNb]));
// The message ID
BerValue.encode(bb, updateControls.getMessageId());
// The controls sequence
bb.put(UniversalTag.SEQUENCE.getValue());
bb.put(TLV.getBytes(controlsLengths[updateControlsNb]));
// The controls
int controlNb = 0;
for (Control control : updateControls.getControls()) {
// The control SEQUENCE
bb.put(UniversalTag.SEQUENCE.getValue());
bb.put(TLV.getBytes(controlLengths[updateControlsNb][controlNb]));
// The control OID
BerValue.encode(bb, control.getOid());
// The criticality, if true
if (control.isCritical()) {
BerValue.encode(bb, true);
}
// compute the value length
int valueLength = ((CodecControl<?>) control).computeLength();
if (valueLength > 0) {
bb.put(UniversalTag.OCTET_STRING.getValue());
bb.put(TLV.getBytes(valueLength));
((CodecControl<?>) control).encode(bb);
}
controlNb++;
}
updateControlsNb++;
}
}
return bb;
}
use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class ParserUtils method addControls.
/**
* Adds Controls to the given Element.
*
* @param codec The LDAP Service to use
* @param element the element to add the Controls to
* @param controls a List of Controls
*/
public static void addControls(LdapApiService codec, Element element, Collection<Control> controls) {
if (controls != null) {
for (Control control : controls) {
Element controlElement = element.addElement("control");
if (control.getOid() != null) {
controlElement.addAttribute("type", control.getOid());
}
if (control.isCritical()) {
controlElement.addAttribute("criticality", "true");
}
byte[] value;
if (control instanceof CodecControl<?>) {
value = ((org.apache.directory.api.ldap.codec.api.CodecControl<?>) control).getValue();
} else {
value = codec.newControl(control).getValue();
}
if (value != null) {
if (ParserUtils.needsBase64Encoding(value)) {
element.getDocument().getRootElement().add(XSD_NAMESPACE);
element.getDocument().getRootElement().add(XSI_NAMESPACE);
Element valueElement = controlElement.addElement("controlValue").addText(ParserUtils.base64Encode(value));
valueElement.addAttribute(new QName("type", XSI_NAMESPACE), ParserUtils.XSD + ":" + ParserUtils.BASE64BINARY);
} else {
controlElement.addElement("controlValue").setText(Arrays.toString(value));
}
}
}
}
}
use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class SearchRequestSubstringTest method testDecodeSearchRequestSubstringInitialAnyWithControls.
/**
* Test the decoding of a SearchRequest with a substring filter. Test the
* initial filter : (objectclass=t*) With controls
*/
@Test
public void testDecodeSearchRequestSubstringInitialAnyWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x0081);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x7F, 0x02, 0x01, // messageID MessageID
0x01, 0x63, // CHOICE { ..., searchRequest SearchRequest, ...
0x5D, // SearchRequest ::= APPLICATION[3] SEQUENCE {
0x04, // baseObject 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', 0x0A, 0x01, // scope
0x01, // wholeSubtree (2) },
0x0A, 0x01, // derefAliases ENUMERATED {
0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
0x02, 0x02, 0x03, (byte) 0xE8, // timeLimit INTEGER (0 .. maxInt), (1000)
0x02, 0x02, 0x03, (byte) 0xE8, 0x01, 0x01, // typesOnly
(byte) 0xFF, // filter Filter,
(byte) 0xA4, // Filter ::= CHOICE {
0x12, // SubstringFilter ::= SEQUENCE {
0x04, // type AttributeDescription,
0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x30, 0x03, (byte) 0x80, 0x01, //
't', 0x30, // AttributeDescriptionList ::= SEQUENCE OF
0x15, // AttributeDescription
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
'0', // ::= LDAPString
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
'1', // ::= LDAPString
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
'2', // ::= LDAPString
(byte) 0xA0, // A control
0x1B, 0x30, 0x19, 0x04, 0x17, 0x32, 0x2E, 0x31, 0x36, 0x2E, 0x38, 0x34, 0x30, 0x2E, 0x31, 0x2E, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2E, 0x33, 0x2E, 0x34, 0x2E, 0x32 });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a BindRequest Container
LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
SearchRequest searchRequest = ldapMessageContainer.getMessage();
assertEquals(1, searchRequest.getMessageId());
assertEquals("uid=akarasulu,dc=example,dc=com", searchRequest.getBase().toString());
assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
assertEquals(1000, searchRequest.getSizeLimit());
assertEquals(1000, searchRequest.getTimeLimit());
assertEquals(true, searchRequest.getTypesOnly());
// (objectclass=t*)
ExprNode node = searchRequest.getFilter();
SubstringNode substringNode = (SubstringNode) node;
assertNotNull(substringNode);
assertEquals("objectclass", substringNode.getAttribute());
assertEquals("t", substringNode.getInitial());
// The attributes
List<String> attributes = searchRequest.getAttributes();
for (String attribute : attributes) {
assertNotNull(attribute);
}
// Check the Control
Map<String, Control> controls = searchRequest.getControls();
assertEquals(1, controls.size());
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) searchRequest.getControl("2.16.840.1.113730.3.4.2");
assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
// attributes may have been reordered
try {
ByteBuffer bb = encoder.encodeMessage(searchRequest);
// Check the length
assertEquals(0x0081, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x53), decodedPdu.substring(0, 0x53));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations