use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.
the class SchemaElementImpl method descToLdif.
/**
* @return The description as a ldif line
* @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong
*/
private String descToLdif() throws LdapException {
if (Strings.isEmpty(description)) {
return "";
} else {
Entry entry = new DefaultEntry();
Attribute attribute = new DefaultAttribute("m-description", description);
entry.put(attribute);
return LdifUtils.convertAttributesToLdif(entry);
}
}
use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.
the class SchemaElementImpl method extensionsToLdif.
/**
* Return the extensions formated as Ldif lines
*
* @param id The attributeId : can be m-objectClassExtension or
* m-attributeTypeExtension
* @return The extensions formated as ldif lines
* @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong
*/
protected String extensionsToLdif(String id) throws LdapException {
StringBuilder sb = new StringBuilder();
Entry entry = new DefaultEntry();
Attribute attribute = new DefaultAttribute(id);
for (String extension : extensions.keySet()) {
attribute.add(extension);
}
sb.append(LdifUtils.convertAttributesToLdif(entry));
return sb.toString();
}
use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.
the class AddRequestTest method testDecodeAddRequestEmptyAttributeValueWithControl.
/**
* Test the decoding of a AddRequest with a empty attributeList and a
* control
*/
@Test
public void testDecodeAddRequestEmptyAttributeValueWithControl() throws NamingException {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x51);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x4F, 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, (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 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 Control
Map<String, Control> controls = addRequest.getControls();
assertEquals(1, controls.size());
assertTrue(addRequest.hasControl("2.16.840.1.113730.3.4.2"));
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("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()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(addRequest);
// Check the length
assertEquals(0x51, 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.ldap.model.entry.Attribute in project directory-ldap-api by apache.
the class ModifyRequestTest method testDecodeModifyRequestManyOperations2.
/**
* Test the decoding of a ModifyRequest, with different operations, take 2
*/
@Test
public void testDecodeModifyRequestManyOperations2() throws LdapException {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x18C);
stream.put(new byte[] { 0x30, (byte) 0x81, // LdapMessage
(byte) 0xB6, 0x02, 0x01, // Message ID : 49
0x31, 0x66, (byte) 0x81, // ModifyRequest
(byte) 0x93, 0x04, // object : cn=Tori Amos,ou=playground,dc=apache,dc=org
0x2B, 'c', 'n', '=', 'T', 'o', 'r', 'i', ' ', 'A', 'm', 'o', 's', ',', 'o', 'u', '=', 'p', 'l', 'a', 'y', 'g', 'r', 'o', 'u', 'n', 'd', ',', 'd', 'c', '=', 'a', 'p', 'a', 'c', 'h', 'e', ',', 'd', 'c', '=', 'o', 'r', 'g', 0x30, // Modifications
0x64, 0x30, // Modification
0x14, 0x0A, 0x01, // Operation : Delete
0x01, 0x30, // type : description
0x0F, 0x04, 0x0B, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x31, // Vals = null
0x00, 0x30, // Modification
0x25, 0x0A, 0x01, // Operation : Add
0x00, 0x30, // type : telephoneNumber
0x20, 0x04, 0x0F, 't', 'e', 'l', 'e', 'p', 'h', 'o', 'n', 'e', 'N', 'u', 'm', 'b', 'e', 'r', 0x31, // Vals : 01234567890
0x0D, 0x04, 0x0B, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0x30, // Modification
0x25, 0x0A, 0x01, // Operation : Add
0x00, 0x30, // type : telephoneNumber
0x20, 0x04, 0x0F, 't', 'e', 'l', 'e', 'p', 'h', 'o', 'n', 'e', 'N', 'u', 'm', 'b', 'e', 'r', 0x31, // Vals : 01234567890
0x0D, 0x04, 0x0B, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', (byte) 0xA0, // Controls : 2.16.840.1.113730.3.4.2
0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
// Decode a ModifyRequest PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded PDU
ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
assertEquals(49, modifyRequest.getMessageId());
assertEquals("cn=Tori Amos,ou=playground,dc=apache,dc=org", modifyRequest.getName().toString());
Object[] modifications = modifyRequest.getModifications().toArray();
assertEquals(3, modifications.length);
Modification modification = (Modification) modifications[0];
Attribute attributeValue = modification.getAttribute();
assertEquals("description", Strings.toLowerCaseAscii(attributeValue.getUpId()));
assertEquals(0, attributeValue.size());
modification = (Modification) modifications[1];
attributeValue = modification.getAttribute();
String attrValue = attributeValue.getString();
assertEquals("telephonenumber", Strings.toLowerCaseAscii(attributeValue.getUpId()));
assertEquals("01234567890", attrValue);
modification = (Modification) modifications[2];
attributeValue = modification.getAttribute();
attrValue = attributeValue.getString();
assertEquals("telephonenumber", Strings.toLowerCaseAscii(attributeValue.getUpId()));
attrValue = attributeValue.getString();
assertEquals("01234567890", attrValue);
// Check the encoding, by decoding and re-encoding the result
try {
ByteBuffer bb = encoder.encodeMessage(modifyRequest);
// Check the length
assertEquals(0xB9, bb.limit());
String decodedPdu1 = Strings.dumpBytes(bb.array());
try {
ldapDecoder.decode(bb, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
ModifyRequest modifyRequest2 = ldapMessageContainer.getMessage();
bb = encoder.encodeMessage(modifyRequest2);
String decodedPdu2 = Strings.dumpBytes(bb.array());
assertEquals(decodedPdu1, decodedPdu2);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.
the class ModifyRequestTest method testDecodeModifyRequest2AttrsSuccess.
/**
* Test the decoding of a ModifyRequest
*/
@Test
public void testDecodeModifyRequest2AttrsSuccess() throws LdapException {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x54);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x52, 0x02, 0x01, // messageID MessageID
0x01, 0x66, // CHOICE { ..., modifyRequest ModifyRequest, ...
0x4d, // object 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', 0x30, 0x29, // modification SEQUENCE OF SEQUENCE {
0x30, 0x11, 0x0A, 0x01, // operation ENUMERATED {
0x02, // modification AttributeTypeAndValues } }
0x30, // AttributeTypeAndValues ::= SEQUENCE {
0x0c, 0x04, 0x01, // type AttributeDescription,
'l', 0x31, // vals SET OF AttributeValue }
0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // modification SEQUENCE OF *SEQUENCE* {
0x14, 0x0A, 0x01, // operation ENUMERATED {
0x00, // modification AttributeTypeAndValues } }
0x30, // AttributeTypeAndValues ::= SEQUENCE {
0x0f, // type AttributeDescription,
0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET OF AttributeValue }
0x06, 0x04, 0x04, 't', 'e', 's', 't' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
// Decode a ModifyRequest PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded PDU
ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
assertEquals(1, modifyRequest.getMessageId());
assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
Collection<Modification> modifications = modifyRequest.getModifications();
assertEquals(2, modifications.size());
for (Modification modification : modifications) {
Attribute attribute = modification.getAttribute();
if ("l".equalsIgnoreCase(attribute.getUpId())) {
String attrValue = attribute.getString();
assertEquals("Paris", attrValue);
} else if ("attrs".equalsIgnoreCase(attribute.getUpId())) {
String attrValue = attribute.getString();
assertEquals("test", attrValue);
}
}
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(modifyRequest);
// Check the length
assertEquals(0x54, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations