use of org.apache.directory.api.ldap.model.message.AddRequest in project ldapchai by ldapchai.
the class ApacheLdapProviderImpl method createEntry.
public void createEntry(final String entryDN, final Set<String> baseObjectClasses, final Map<String, String> stringAttributes) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
activityPreCheck();
getInputValidator().createEntry(entryDN, baseObjectClasses, stringAttributes);
try {
final AddRequest addRequest = new AddRequestImpl();
final Entry entry = new DefaultEntry();
entry.setDn(entryDN);
for (final String baseObjectClass : baseObjectClasses) {
entry.add(ChaiConstant.ATTR_LDAP_OBJECTCLASS, baseObjectClass);
}
for (final Map.Entry<String, String> entryIter : stringAttributes.entrySet()) {
final String name = entryIter.getKey();
final String value = entryIter.getValue();
entry.add(name, value);
}
final AddResponse response = connection.add(addRequest);
processResponse(response);
} catch (LdapException e) {
throw ChaiOperationException.forErrorMessage(e.getMessage());
}
}
use of org.apache.directory.api.ldap.model.message.AddRequest 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.message.AddRequest in project directory-ldap-api by apache.
the class AddRequestTest method testRequestWith1AttrWithBase64Value.
/**
* Test parsing of a request with an Attr elements with value
*/
@Test
public void testRequestWith1AttrWithBase64Value() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(AddRequestTest.class.getResource("request_with_1_attr_with_base64_value.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
Entry entry = addRequest.getEntry();
assertEquals(1, entry.size());
// Getting the Attribute
Iterator<Attribute> attributeIterator = entry.iterator();
Attribute attribute = attributeIterator.next();
assertEquals("objectclass", attribute.getUpId());
// Getting the Value
Iterator<Value> valueIterator = attribute.iterator();
assertTrue(valueIterator.hasNext());
Value value = valueIterator.next();
assertFalse(value.isHumanReadable());
assertEquals("DSMLv2.0 rocks!!", value.getValue());
}
use of org.apache.directory.api.ldap.model.message.AddRequest in project directory-ldap-api by apache.
the class AddRequestTest method testRequestWith3ControlsWithoutValue.
/**
* Test parsing of a request with 3 (optional) Control elements without value
*/
@Test
public void testRequestWith3ControlsWithoutValue() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(AddRequestTest.class.getResource("request_with_3_controls_without_value.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
Map<String, Control> controls = addRequest.getControls();
assertEquals(3, addRequest.getControls().size());
Control control = controls.get("1.2.840.113556.1.4.456");
assertNotNull(control);
assertTrue(control.isCritical());
assertEquals("1.2.840.113556.1.4.456", control.getOid());
assertFalse(((DsmlControl<?>) control).hasValue());
}
use of org.apache.directory.api.ldap.model.message.AddRequest in project directory-ldap-api by apache.
the class AddRequestTest method testRequestWith1AttrWithValue.
/**
* Test parsing of a request with an Attr elements with value
*/
@Test
public void testRequestWith1AttrWithValue() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(AddRequestTest.class.getResource("request_with_1_attr_with_value.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
Entry entry = addRequest.getEntry();
assertEquals(1, entry.size());
// Getting the Attribute
Iterator<Attribute> attributeIterator = entry.iterator();
Attribute attribute = attributeIterator.next();
assertEquals("objectclass", attribute.getUpId());
// Getting the Value
Iterator<Value> valueIterator = attribute.iterator();
assertTrue(valueIterator.hasNext());
Value value = valueIterator.next();
assertEquals("top", value.getValue());
}
Aggregations