org.apache.directory.api.ldap.model.entry.ModificationOperation Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.entry.ModificationOperation. 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: ModificationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "test1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example #2
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * @param user
 * @throws org.apache.directory.fortress.core.UpdateException
 */
void lock( User user ) throws UpdateException
{
    LdapConnection ld = null;
    String userDn = getDn( user.getUserId(), user.getContextId() );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, OPENLDAP_PW_LOCKED_TIME,
            LOCK_VALUE ) );
        ld = getAdminConnection();
        modify( ld, userDn, mods, user );
    }
    catch ( LdapException e )
    {
        String error = "lock user [" + user.getUserId() + "] caught LDAPException=" + e;
        throw new UpdateException( GlobalErrIds.USER_PW_LOCK_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
}
 
Example #3
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of RBAC roles, {@link UserRole}, convert to raw data format and load into ldap modification
 * set in preparation for ldap modify.
 *
 * @param list contains List of type {@link UserRole} targeted for updating into ldap.
 * @param mods contains ldap modification set containing RBAC role assignments in raw ldap format to be updated.
 * @throws LdapInvalidAttributeValueException
 */
private void loadUserRoles( List<UserRole> list, List<Modification> mods )
    throws LdapInvalidAttributeValueException
{
    Attribute userRoleData = new DefaultAttribute( GlobalIds.USER_ROLE_DATA );
    Attribute userRoleAssign = new DefaultAttribute( USER_ROLE_ASSIGN );

    if ( list != null )
    {
        for ( UserRole userRole : list )
        {
            userRoleData.add( userRole.getRawData() );
            userRoleAssign.add( userRole.getName() );
        }

        if ( userRoleData.size() != 0 )
        {
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userRoleData ) );
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userRoleAssign ) );
        }
    }
}
 
Example #4
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of {@link java.util.Properties}, convert to raw data name-value format and load into ldap
 * modification set in preparation for ldap modify.
 *
 * @param props    contains {@link java.util.Properties} targeted for removal from ldap.
 * @param mods     ldap modification set containing name-value pairs in raw ldap format to be removed.
 * @param attrName contains the name of the ldap attribute to be removed.
 */
protected void removeProperties( Properties props, List<Modification> mods, String attrName )
{
    if ( props != null && props.size() > 0 )
    {
        for ( Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); )
        {
            String key = ( String ) e.nextElement();
            String val = props.getProperty( key );

            // This LDAP attr is stored as a name-value pair separated by a ':'.
            mods.add( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attrName,
                key + GlobalIds.PROP_SEP + val ) );
        }
    }
}
 
Example #5
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void writeStringAttribute( final String entryDN, final String attributeName, final Set<String> values, final boolean overwrite )
        throws ChaiOperationException, ChaiUnavailableException, IllegalStateException
{
    activityPreCheck();
    getInputValidator().writeStringAttribute( entryDN, attributeName, values, overwrite );

    try
    {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName( new Dn( entryDN ) );
        {
            final Modification modification = new DefaultModification();
            modification.setOperation( overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE );
            modification.setAttribute( new DefaultAttribute( attributeName, values.toArray( new String[values.size()] ) ) );
            modifyRequest.addModification( modification );
        }
        final ModifyResponse response = connection.modify( modifyRequest );
        processResponse( response );
    }
    catch ( LdapException e )
    {
        throw ChaiOperationException.forErrorMessage( e.getMessage() );
    }
}
 
Example #6
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void writeBinaryAttribute( final String entryDN, final String attributeName, final byte[][] values, final boolean overwrite )
        throws ChaiUnavailableException, ChaiOperationException
{
    activityPreCheck();
    getInputValidator().writeBinaryAttribute( entryDN, attributeName, values, overwrite );

    try
    {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName( new Dn( entryDN ) );
        {
            final Modification modification = new DefaultModification();
            modification.setOperation( overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE );
            modification.setAttribute( new DefaultAttribute( attributeName, values ) );
            modifyRequest.addModification( modification );
        }
        final ModifyResponse response = connection.modify( modifyRequest );
        processResponse( response );
    }
    catch ( LdapException e )
    {
        throw ChaiOperationException.forErrorMessage( e.getMessage() );
    }
}
 
Example #7
Source File: GroupDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
Group add( Group group, String key, String value ) throws FinderException, CreateException
{
    LdapConnection ld = null;
    String nodeDn = getDn( group.getName(), group.getContextId() );

    try
    {
        LOG.debug( "add group property dn [{}], key [{}], value [{}]", nodeDn, key, value );
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.ADD_ATTRIBUTE, GROUP_PROPERTY_ATTR_IMPL, key + "=" + value ) );
        ld = getAdminConnection();
        modify( ld, nodeDn, mods, group );
    }
    catch ( LdapException e )
    {
        String error = "update group property node dn [" + nodeDn + "] caught LDAPException=" + e;
        throw new CreateException( GlobalErrIds.GROUP_ADD_PROPERTY_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return get( group );
}
 
Example #8
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * @param user
 * @return
 * @throws UpdateException
 * @throws Exception
 */
String deletePwPolicy( User user ) throws UpdateException
{
    LdapConnection ld = null;
    String userDn = getDn( user.getUserId(), user.getContextId() );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();

        mods.add( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, OPENLDAP_POLICY_SUBENTRY ) );
        ld = getAdminConnection();
        modify( ld, userDn, mods, user );
    }
    catch ( LdapException e )
    {
        String warning = "deletePwPolicy userId [" + user.getUserId() + "] caught LDAPException=" + e + " msg=" + e;
        throw new UpdateException( GlobalErrIds.USER_PW_PLCY_DEL_FAILED, warning, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return userDn;
}
 
Example #9
Source File: ModifyRequestImplTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test for inequality when only the number of mods are different.
 */
@Test
public void testNotEqualDiffModCount() throws LdapException
{
    ModifyRequestImpl req0 = getRequest();
    Attribute attr = new DefaultAttribute( "attr3" );
    attr.add( "val0" );
    attr.add( "val1" );
    attr.add( "val2" );
    Modification item = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    req0.addModification( item );

    ModifyRequestImpl req1 = getRequest();

    assertFalse( req0.equals( req1 ) );
    assertFalse( req1.equals( req0 ) );
}
 
Example #10
Source File: AdminRoleDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * This method will remove the supplied DN as a role occupant to the target record.
 * This data will be stored in the {@link GlobalIds#ADMIN_ROLE_ROOT} container.
 *
 * @param entity record contains {@link AdminRole#name}.  Null attributes will be ignored.
 * @param userDn contains the DN for userId who is being deassigned.
 * @return input record back to client.
 * @throws UpdateException in the event LDAP errors occur.
 */
AdminRole deassign( AdminRole entity, String userDn ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.REMOVE_ATTRIBUTE, ROLE_OCCUPANT, userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "deassign role name [" + entity.getName() + "] user dn [" + userDn
            + "] caught LdapException=" + e;
        throw new UpdateException( GlobalErrIds.ARLE_USER_DEASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
    return entity;
}
 
Example #11
Source File: AdminRoleDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * This method will add the supplied DN as a role occupant to the target record.
 * This data will be stored in the {@link GlobalIds#ADMIN_ROLE_ROOT} container.
 *
 * @param entity record contains {@link AdminRole#name}.  Null attributes will be ignored.
 * @param userDn contains the DN for userId who is being assigned.
 * @return input record back to client.
 * @throws UpdateException in the event LDAP errors occur.
 */
AdminRole assign( AdminRole entity, String userDn ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, ROLE_OCCUPANT, userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "assign role name [" + entity.getName() + "] user dn [" + userDn + "] caught LdapException="
            + e;
        throw new UpdateException( GlobalErrIds.ARLE_USER_ASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return entity;
}
 
Example #12
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the deletion of an attribute into an entry which contains the attribute.
 * 
 * The entry should not contain the attribute after the operation
 */
@Test
public void testApplyRemoveModificationFromEntrySameAttributeSameValue() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test" );

    Attribute attr = new DefaultAttribute( "cn", "test" );

    Modification modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attr );

    AttributeUtils.applyModification( entry, modification );

    assertNull( entry.get( "cn" ) );
    assertEquals( 0, entry.size() );
}
 
Example #13
Source File: GroupDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * @param entity
 * @param userDn
 * @return
 * @throws org.apache.directory.fortress.core.UpdateException
 *
 */
Group assign( Group entity, String userDn ) throws FinderException, UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );
    LOG.debug( "assign group property dn [{}], member dn [{}]", dn, userDn );
    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.ADD_ATTRIBUTE, SchemaConstants.MEMBER_AT, userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "assign group name [" + entity.getName() + "] user dn [" + userDn + "] caught " +
            "LDAPException=" + e;
        throw new UpdateException( GlobalErrIds.GROUP_USER_ASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
    return get( entity );
}
 
Example #14
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the deletion of an attribute into an entry which contains the attribute
 * but without the value to be deleted
 */
@Test
public void testApplyRemoveModificationFromEntryAttributeNotSameValue() throws LdapException
{
    Entry entry = new DefaultEntry();

    Attribute cn = new DefaultAttribute( "cn", "apache" );
    entry.put( cn );

    Attribute attr = new DefaultAttribute( "cn", "test" );

    Modification modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attr );

    AttributeUtils.applyModification( entry, modification );

    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );
    assertEquals( cn, entry.get( "cn" ) );
}
 
Example #15
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the deletion of an attribute into an entry which does not contain the attribute
 */
@Test
public void testApplyRemoveModificationFromEntryAttributeNotPresent() throws LdapException
{
    Entry entry = new DefaultEntry();

    Attribute dc = new DefaultAttribute( "dc", "apache" );
    entry.put( dc );

    Attribute cn = new DefaultAttribute( "cn", "test" );

    Modification modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, cn );

    AttributeUtils.applyModification( entry, modification );

    assertNull( entry.get( "cn" ) );
    assertNotNull( entry.get( "dc" ) );
    assertEquals( 1, entry.size() );
    assertEquals( dc, entry.get( "dc" ) );
}
 
Example #16
Source File: GroupDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
Group delete( Group group, String key, String value ) throws FinderException, RemoveException
{
    LdapConnection ld = null;
    String nodeDn = getDn( group.getName(), group.getContextId() );

    try
    {
        LOG.debug( "delete group property dn [{}], key [{}], value [{}]", nodeDn, key, value );
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.REMOVE_ATTRIBUTE, GROUP_PROPERTY_ATTR_IMPL, key + "=" + value ) );
        ld = getAdminConnection();
        modify( ld, nodeDn, mods, group );
    }
    catch ( LdapException e )
    {
        String error = "delete group property node dn [" + nodeDn + "] caught LDAPException=" + e;
        throw new RemoveException( GlobalErrIds.GROUP_DELETE_PROPERTY_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
    return get( group );
}
 
Example #17
Source File: SchemaAwareModificationSerializationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn", cnAT );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "TEST1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example #18
Source File: RoleDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * @param entity
 * @param userDn
 * @return
 * @throws org.apache.directory.fortress.core.UpdateException
 *
 */
Role deassign( Role entity, String userDn ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );
    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE,
            SchemaConstants.ROLE_OCCUPANT_AT, userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "deassign role name [" + entity.getName() + "] user dn [" + userDn
            + "] caught LdapException=" + e;
        throw new UpdateException( GlobalErrIds.ROLE_USER_DEASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return entity;
}
 
Example #19
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationREPLACE() throws ClassNotFoundException, IOException, LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    DefaultModification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, attribute );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example #20
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void modify( Entry entry, ModificationOperation modOp ) throws LdapException
{
    if ( entry == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.msg( I18n.MSG_04140_NULL_ENTRY_MODIFY ) );
        }
        
        throw new IllegalArgumentException( I18n.err( I18n.ERR_04133_NULL_MODIFIED_ENTRY ) );
    }

    ModifyRequest modReq = new ModifyRequestImpl();
    modReq.setName( entry.getDn() );

    Iterator<Attribute> itr = entry.iterator();

    while ( itr.hasNext() )
    {
        modReq.addModification( itr.next(), modOp );
    }

    ModifyResponse modifyResponse = modify( modReq );

    processResponse( modifyResponse );
}
 
Example #21
Source File: RoleDAO.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * @param entity
 * @param userDn
 * @return
 * @throws org.apache.directory.fortress.core.UpdateException
 *
 */
Role assign( Role entity, String userDn ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, SchemaConstants.ROLE_OCCUPANT_AT,
            userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "assign role name [" + entity.getName() + "] user dn [" + userDn + "] caught LdapException="
            + e;
        throw new UpdateException( GlobalErrIds.ROLE_USER_ASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return entity;
}
 
Example #22
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the deletion of an attribute into an empty entry
 */
@Test
public void testApplyRemoveModificationFromEmptyEntry() throws LdapException
{
    Entry entry = new DefaultEntry();

    Attribute attr = new DefaultAttribute( "cn", "test" );

    Modification modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNull( entry.get( "cn" ) );
    assertEquals( 0, entry.size() );
}
 
Example #23
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an entry with the same attribute
 * and the same value 
 */
@Test
public void testApplyAddModificationToEntryWithSameValue() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );

    Attribute cnAttr = entry.get( "cn" );

    assertTrue( cnAttr.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "apache" );
    expectedValues.add( "test" );

    for ( Value value : cnAttr )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example #24
Source File: LdapPosixTestEnvironment.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void init() throws Exception {
  initializeDirectory();

  // Enable POSIX groups in ApacheDS
  Dn nis = new Dn("cn=nis,ou=schema");
  if (service.getAdminSession().exists(nis)) {
    Entry entry = service.getAdminSession().lookup(nis);
    Attribute nisDisabled = entry.get("m-disabled");
    if (null != nisDisabled && "TRUE".equalsIgnoreCase(nisDisabled.getString())) {
      nisDisabled.remove("TRUE");
      nisDisabled.add("FALSE");
      List<Modification> modifications = new ArrayList<Modification>();
      modifications.add(new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, nisDisabled));
      service.getAdminSession().modify(nis, modifications);
      service.shutdown();
      initializeDirectory(); // Note: This instantiates service again for schema modifications to take effect.
    }
  }

  startServer();

  createGroup("office-berlin");
  createUserUid("daniel", "office-berlin", "Daniel", "Meyer", "[email protected]");

  createGroup("people");
  createUserUid("ruecker", "people", "Bernd", "Ruecker", "[email protected]");
  createUserUid("monster", "people", "Cookie", "Monster", "[email protected]");
  createUserUid("fozzie", "people", "Bear", "Fozzie", "[email protected]");

  createGroup("groups");
  createPosixGroup("1", "posix-group-without-members");
  createPosixGroup("2", "posix-group-with-members", "fozzie", "monster", "ruecker");
}
 
Example #25
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an empty entry
 */
@Test
public void testApplyAddModificationToEmptyEntry() throws LdapException
{
    Entry entry = new DefaultEntry();
    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );
    assertEquals( attr, entry.get( "cn" ) );
}
 
Example #26
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationNoAttribute() throws ClassNotFoundException, IOException
{
    DefaultModification mod = new DefaultModification();

    mod.setOperation( ModificationOperation.ADD_ATTRIBUTE );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example #27
Source File: GroupDAO.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * @param entity
 * @param userDn
 * @return
 * @throws org.apache.directory.fortress.core.UpdateException
 *
 */
Group deassign( Group entity, String userDn ) throws FinderException, UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );
    LOG.debug( "deassign group property dn [{}], member dn [{}]", dn, userDn );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.REMOVE_ATTRIBUTE, SchemaConstants.MEMBER_AT, userDn ) );

        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "deassign group name [" + entity.getName() + "] user dn [" + userDn + "] caught " +
            "LDAPException=" + e;
        throw new UpdateException( GlobalErrIds.GROUP_USER_DEASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return get( entity );
}
 
Example #28
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationADD() throws ClassNotFoundException, IOException, LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    DefaultModification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example #29
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ModificationItem to an instance of a ServerModification object
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( ModificationItem modificationImpl, AttributeType attributeType )
    throws LdapException
{
    ModificationOperation operation;

    switch ( modificationImpl.getModificationOp() )
    {
        case DirContext.REMOVE_ATTRIBUTE:
            operation = ModificationOperation.REMOVE_ATTRIBUTE;
            break;

        case DirContext.REPLACE_ATTRIBUTE:
            operation = ModificationOperation.REPLACE_ATTRIBUTE;
            break;

        case DirContext.ADD_ATTRIBUTE:
        default:
            operation = ModificationOperation.ADD_ATTRIBUTE;
            break;

    }

    Modification modification = new DefaultModification(
        operation,
        ServerEntryUtils.toServerAttribute( modificationImpl.getAttribute(), attributeType ) );

    return modification;

}
 
Example #30
Source File: AddModifyRequestAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<ModifyRequest> container ) throws DecoderException
{
    ModifyRequest modifyRequest = container.getMessage();

    TLV tlv = container.getCurrentTLV();

    // Store the value. It can't be null
    String type;

    if ( tlv.getLength() == 0 )
    {
        String msg = I18n.err( I18n.ERR_05123_TYPE_CANT_BE_NULL );
        LOG.error( msg );

        ModifyResponseImpl response = new ModifyResponseImpl( modifyRequest.getMessageId() );
        throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX,
            modifyRequest.getName(), null );
    }
    else
    {
        type = Strings.utf8ToString( tlv.getValue().getData() );
        Attribute currentAttribute = new DefaultAttribute( type );
        container.setCurrentAttribute( currentAttribute );
        container.getCurrentModification().setAttribute( currentAttribute );
    }

    // We can have an END transition if the operation was INCREMENT
    if ( container.getCurrentModification().getOperation() == ModificationOperation.INCREMENT_ATTRIBUTE )
    {
        container.setGrammarEndAllowed( true );
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05128_MODIFYING_TYPE, type ) );
    }
}