use of org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator in project directory-ldap-api by apache.
the class AddAttributeValue method action.
/**
* {@inheritDoc}
*/
public void action(LdapMessageContainer<AddRequestDecorator> container) {
AddRequestDecorator addRequest = container.getMessage();
TLV tlv = container.getCurrentTLV();
// Store the value. It can't be null
Object value = null;
try {
if (tlv.getLength() == 0) {
addRequest.addAttributeValue("");
} else {
if (container.isBinary(addRequest.getCurrentAttributeType())) {
value = tlv.getValue().getData();
if (IS_DEBUG) {
LOG.debug("Adding value {}", Strings.dumpBytes((byte[]) value));
}
addRequest.addAttributeValue((byte[]) value);
} else {
value = Strings.utf8ToString(tlv.getValue().getData());
if (IS_DEBUG) {
LOG.debug("Adding value {}" + value);
}
addRequest.addAttributeValue((String) value);
}
}
} catch (LdapException le) {
// Just swallow the exception, it can't occur here
}
// We can have an END transition
container.setGrammarEndAllowed(true);
}
use of org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator in project directory-ldap-api by apache.
the class AddRequestTest method testDecodeAddRequestSuccess.
/**
* Test the decoding of a AddRequest
*/
@Test
public void testDecodeAddRequestSuccess() throws NamingException {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x59);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x57, 0x02, 0x01, // messageID MessageID
0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
0x52, // entry LDAPDN,
0x04, 0x20, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', // attributes AttributeList }
0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
0x2E, 0x30, // attribute 1
0x0c, 0x04, 0x01, // type AttributeDescription,
'l', 0x31, // vals SET OF AttributeValue }
0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // attribute 2
0x1E, // type AttributeDescription,
0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET
0x15, // }
0x04, 0x05, 't', 'e', 's', 't', '1', 0x04, 0x05, 't', 'e', 's', 't', '2', 0x04, 0x05, 't', 'e', 's', 't', '3' });
Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<AddRequestDecorator> container = new LdapMessageContainer<AddRequestDecorator>(codec);
// Decode a AddRequest message
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
AddRequest addRequest = container.getMessage();
// Check the decoded message
assertEquals(1, addRequest.getMessageId());
assertEquals("cn=testModify,ou=users,ou=system", addRequest.getEntryDn().toString());
Entry entry = addRequest.getEntry();
assertEquals(2, entry.size());
Set<String> expectedTypes = new HashSet<String>();
expectedTypes.add("l");
expectedTypes.add("attrs");
Map<String, Set<String>> typesVals = new HashMap<String, Set<String>>();
Set<String> lVal1 = new HashSet<String>();
lVal1.add("Paris");
typesVals.put("l", lVal1);
Set<String> lVal2 = new HashSet<String>();
lVal2.add("test1");
lVal2.add("test2");
lVal2.add("test3");
typesVals.put("attrs", lVal2);
Attribute attribute = entry.get("l");
assertTrue(expectedTypes.contains(Strings.toLowerCaseAscii(attribute.getId())));
Set<String> vals = (Set<String>) typesVals.get(Strings.toLowerCaseAscii(attribute.getId()));
for (Value value : attribute) {
assertTrue(vals.contains(value.getValue()));
vals.remove(value.getValue());
}
attribute = entry.get("attrs");
assertTrue(expectedTypes.contains(Strings.toLowerCaseAscii(attribute.getId())));
vals = (Set<String>) typesVals.get(Strings.toLowerCaseAscii(attribute.getId()));
for (Value value : attribute) {
assertTrue(vals.contains(value.getValue()));
vals.remove(value.getValue());
}
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(addRequest);
// Check the length
assertEquals(0x59, bb.limit());
// kept. Let's decode again and compare the resulting AddRequest
try {
ldapDecoder.decode(bb, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
AddRequest addRequest2 = container.getMessage();
assertEquals(addRequest, addRequest2);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator in project directory-ldap-api by apache.
the class AddRequestTest method testDecodeAddRequestEmptyAttributeValue.
/**
* Test the decoding of a AddRequest with a empty attributeList
*/
@Test
public void testDecodeAddRequestEmptyAttributeValue() throws NamingException {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x34);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x32, 0x02, 0x01, // messageID MessageID
0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
0x2D, // entry LDAPDN,
0x04, 0x20, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', // attributes AttributeList }
0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
0x09, 0x30, // attribute 1
0x07, 0x04, 0x01, // type AttributeDescription,
'l', 0x31, 0x02, 0x04, 0x00 });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<AddRequestDecorator> container = new LdapMessageContainer<AddRequestDecorator>(codec);
// Decode a AddRequest message
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
AddRequest addRequest = container.getMessage();
// Check the decoded message
assertEquals(1, addRequest.getMessageId());
assertEquals("cn=testModify,ou=users,ou=system", addRequest.getEntryDn().toString());
Entry entry = addRequest.getEntry();
assertEquals(1, entry.size());
Attribute attribute = entry.get("l");
assertEquals("l", Strings.toLowerCaseAscii(attribute.getId()));
for (Value value : attribute) {
assertEquals("", value.getValue());
}
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(addRequest);
// Check the length
assertEquals(0x34, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations