Java Code Examples for org.apache.directory.api.ldap.model.message.ModifyRequest#getModifications()

The following examples show how to use org.apache.directory.api.ldap.model.message.ModifyRequest#getModifications() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element
 * @throws NamingException
 */
@Test
public void testRequestWith1Modification() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_1_modification.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( "CN=John Smith, DC=microsoft, DC=com", attribute.get().getString() );
}
 
Example 2
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element and Increment operation
 * @throws NamingException
 */
@Test
public void testRequestWith1ModificationIncrement() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_1_modification_increment.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.INCREMENT_ATTRIBUTE, modification.getOperation() );

    Attribute attribute = modification.getAttribute();

    assertEquals( "uidnumber", attribute.getId() );
    assertEquals( "CN=John Smith, DC=microsoft, DC=com", attribute.get().getString() );
}
 
Example 3
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element with Base64 Value
 * @throws NamingException
 */
@Test
public void testRequestWith1ModificationBase64Value() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_1_modification_base64_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();
    Attribute attribute = modification.getAttribute();

    assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );

    assertEquals( "directreport", attribute.getId() );

    String expected = new String( new byte[]
        { 'c', 'n', '=', 'E', 'm', 'm', 'a', 'n', 'u', 'e', 'l', ' ', 'L', ( byte ) 0xc3, ( byte ) 0xa9, 'c', 'h',
            'a', 'r', 'n', 'y', ',', ' ', 'o', 'u', '=', 'p', 'e', 'o', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=',
            'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=', 'c', 'o', 'm' }, StandardCharsets.UTF_8 );

    assertEquals( expected, attribute.get().getString() );
}
 
Example 4
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with operation attribute to Add value
 * @throws NamingException
 */
@Test
public void testRequestWithOperationAdd() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_operation_add.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() );
}
 
Example 5
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with operation attribute to Delete value
 * @throws NamingException
 */
@Test
public void testRequestWithOperationDelete() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_operation_delete.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.REMOVE_ATTRIBUTE, modification.getOperation() );
}
 
Example 6
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with operation attribute to Replace value
 * @throws NamingException
 */
@Test
public void testRequestWithOperationReplace() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_operation_replace.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.REPLACE_ATTRIBUTE, modification.getOperation() );
}
 
Example 7
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element without Value element
 * @throws NamingException
 */
@Test
public void testRequestWithModificationWithoutValue() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_modification_without_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( 0, attribute.size() );
}
 
Example 8
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a Modification element
 * @throws NamingException
 */
@Test
public void testRequestWithModificationWith2Values() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_modification_with_2_values.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( 2, attribute.size() );

    assertTrue( attribute.contains( "CN=John Smith, DC=microsoft, DC=com" ) );
    assertTrue( attribute.contains( "CN=Steve Jobs, DC=apple, DC=com" ) );
}
 
Example 9
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * 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().getString() );
}
 
Example 10
Source File: ModifyRequestDsml.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@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.getString() != null )
                {
                    if ( ParserUtils.needsBase64Encoding( value.getString() ) )
                    {
                        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.getString() ) );
                        valueElement.addAttribute( new QName( "type", xsiNamespace ), "xsd:"
                            + ParserUtils.BASE64BINARY );
                    }
                    else
                    {
                        modElement.addElement( "value" ).setText( value.getString() );
                    }
                }
            }
        }

        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" );
        }
        else if ( operation == ModificationOperation.INCREMENT_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "increment" );
        }
    }

    return element;
}
 
Example 11
Source File: ModifyRequestTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test parsing of a request with 2 Modification elements
 * @throws NamingException
 */
@Test
public void testRequestWith2Modifications() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( ModifyRequestTest.class.getResource( "request_with_2_modifications.xml" ).openStream(),
            "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    ModifyRequest modifyRequest = ( ModifyRequest ) parser.getBatchRequest().getCurrentRequest();

    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals( 2, modifications.size() );

    Iterator<Modification> iter = modifications.iterator();

    Modification modification = iter.next();

    assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
    Attribute attribute = modification.getAttribute();
    assertEquals( "directreport", attribute.getId() );

    assertEquals( "CN=John Smith, DC=microsoft, DC=com", attribute.get().getString() );

    modification = iter.next();

    attribute = modification.getAttribute();

    assertEquals( "sn", attribute.getId() );
    assertEquals( ModificationOperation.REPLACE_ATTRIBUTE, modification.getOperation() );

    assertEquals( "CN=Steve Jobs, DC=apple, DC=com", attribute.get().getString() );
}
 
Example 12
Source File: ModifyRequestFactory.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Encode the ModifyRequest message to a PDU.
 * <br>
 * ModifyRequest :
 * <pre>
 * 0x66 LL
 *   0x04 LL object
 *   0x30 LL modifications
 *     0x30 LL modification sequence
 *       0x0A 0x01 operation
 *       0x30 LL modification
 *         0x04 LL type
 *         0x31 LL vals
 *           0x04 LL attributeValue
 *           ...
 *           0x04 LL attributeValue
 *     ...
 *     0x30 LL modification sequence
 *       0x0A 0x01 operation
 *       0x30 LL modification
 *         0x04 LL type
 *         0x31 LL vals
 *           0x04 LL attributeValue
 *           ...
 *           0x04 LL attributeValue
 * </pre>
 *
 * @param codec The LdapApiService instance
 * @param buffer The buffer where to put the PDU
 * @param message the ModifyRequest to encode
 */
@Override
public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
{
    int start = buffer.getPos();
    ModifyRequest modifyRequest = ( ModifyRequest ) message;

    // The modifications, if any
    Collection<Modification> modifications = modifyRequest.getModifications();

    if ( ( modifications != null ) && ( !modifications.isEmpty() ) )
    {
        encodeModifications( buffer, modifications.iterator() );

        // The modifications sequence
        BerValue.encodeSequence( buffer, start );
    }

    // The entry DN
    BerValue.encodeOctetString( buffer, modifyRequest.getName().getName() );

    // The ModifyRequest tag
    BerValue.encodeSequence( buffer, LdapCodecConstants.MODIFY_REQUEST_TAG, start );
}