Search in sources :

Example 11 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testRequestWithModificationWithEmptyValue.

/**
 * Test parsing of a request with a Modification element with an empty value
 * @throws NamingException
 */
@Test
public void testRequestWithModificationWithEmptyValue() throws LdapException {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_modification_with_empty_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals(1, modifications.size());
    Modification modification = modifications.iterator().next();
    assertEquals(ModificationOperation.ADD_ATTRIBUTE, modification.getOperation());
    Attribute attribute = modification.getAttribute();
    assertEquals("directreport", attribute.getId());
    assertEquals(1, attribute.size());
    assertEquals("", attribute.get().getValue());
}
Also used : Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 12 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testRequestWith2Controls.

/**
 * Test parsing of a request with 2 (optional) Control elements
 */
@Test
public void testRequestWith2Controls() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_2_controls.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    Map<String, Control> controls = modifyRequest.getControls();
    assertEquals(2, modifyRequest.getControls().size());
    Control control = controls.get("1.2.840.113556.1.4.789");
    assertNotNull(control);
    assertFalse(control.isCritical());
    assertEquals("1.2.840.113556.1.4.789", control.getOid());
    assertEquals("Some other text", Strings.utf8ToString(((DsmlControl<?>) control).getValue()));
}
Also used : DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Control(org.apache.directory.api.ldap.model.message.Control) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 13 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestTest method testRequestWithRequestId.

/**
 * Test parsing of a request with the (optional) requestID attribute
 */
@Test
public void testRequestWithRequestId() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_requestID_attribute.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    assertEquals(456, modifyRequest.getMessageId());
}
Also used : Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 14 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class BatchRequestTest method testResponseWith2ModifyRequest.

/**
 * Test parsing of a Request with 2 ModifyRequest
 */
@Test
public void testResponseWith2ModifyRequest() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(BatchRequestTest.class.getResource("request_with_2_ModifyRequest.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    BatchRequestDsml batchRequest = parser.getBatchRequest();
    assertEquals(2, batchRequest.getRequests().size());
    if (batchRequest.getCurrentRequest() instanceof ModifyRequest) {
        assertTrue(true);
    } else {
        fail();
    }
}
Also used : BatchRequestDsml(org.apache.directory.api.dsmlv2.request.BatchRequestDsml) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 15 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.

the class ModifyRequestDsml method toDsml.

/**
 * {@inheritDoc}
 */
@Override
public Element toDsml(Element root) {
    Element element = super.toDsml(root);
    ModifyRequest request = getDecorated();
    // Dn
    if (request.getName() != null) {
        element.addAttribute("dn", request.getName().getName());
    }
    // Modifications
    Collection<Modification> modifications = request.getModifications();
    for (Modification modification : modifications) {
        Element modElement = element.addElement("modification");
        if (modification.getAttribute() != null) {
            modElement.addAttribute("name", modification.getAttribute().getId());
            for (Value value : modification.getAttribute()) {
                if (value.getValue() != null) {
                    if (ParserUtils.needsBase64Encoding(value.getValue())) {
                        Namespace xsdNamespace = new Namespace("xsd", ParserUtils.XML_SCHEMA_URI);
                        Namespace xsiNamespace = new Namespace("xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI);
                        element.getDocument().getRootElement().add(xsdNamespace);
                        element.getDocument().getRootElement().add(xsiNamespace);
                        Element valueElement = modElement.addElement("value").addText(ParserUtils.base64Encode(value.getValue()));
                        valueElement.addAttribute(new QName("type", xsiNamespace), "xsd:" + ParserUtils.BASE64BINARY);
                    } else {
                        modElement.addElement("value").setText(value.getValue());
                    }
                }
            }
        }
        ModificationOperation operation = modification.getOperation();
        if (operation == ModificationOperation.ADD_ATTRIBUTE) {
            modElement.addAttribute("operation", "add");
        } else if (operation == ModificationOperation.REPLACE_ATTRIBUTE) {
            modElement.addAttribute("operation", "replace");
        } else if (operation == ModificationOperation.REMOVE_ATTRIBUTE) {
            modElement.addAttribute("operation", "delete");
        }
    }
    return element;
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) ModificationOperation(org.apache.directory.api.ldap.model.entry.ModificationOperation) QName(org.dom4j.QName) Element(org.dom4j.Element) Value(org.apache.directory.api.ldap.model.entry.Value) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) Namespace(org.dom4j.Namespace)

Aggregations

ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)40 Modification (org.apache.directory.api.ldap.model.entry.Modification)25 Test (org.junit.Test)24 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)22 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)17 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)17 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)14 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)11 ModifyRequestImpl (org.apache.directory.api.ldap.model.message.ModifyRequestImpl)11 ModifyResponse (org.apache.directory.api.ldap.model.message.ModifyResponse)10 Dn (org.apache.directory.api.ldap.model.name.Dn)10 ByteBuffer (java.nio.ByteBuffer)7 DecoderException (org.apache.directory.api.asn1.DecoderException)7 EncoderException (org.apache.directory.api.asn1.EncoderException)7 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)7 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)7 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)7 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)7 Control (org.apache.directory.api.ldap.model.message.Control)7