@Test public void testReverseModifyDelExistingOuValue() throws LdapException { Entry modifiedEntry = buildEntry(); modifiedEntry.put( "ou", "apache", "acme corp" ); Dn dn = new Dn( "cn=test, ou=system" ); Modification mod = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, new DefaultAttribute( "ou", "acme corp" ) ); LdifEntry reversed = LdifRevertor.reverseModify( dn, Collections.<Modification> singletonList( mod ), modifiedEntry ); assertNotNull( reversed ); assertEquals( dn.getName(), reversed.getDn().getName() ); assertEquals( ChangeType.Modify, reversed.getChangeType() ); assertNull( reversed.getEntry() ); List<Modification> mods = reversed.getModifications(); assertNotNull( mods ); assertEquals( 1, mods.size() ); Modification modif = mods.get( 0 ); assertEquals( ModificationOperation.ADD_ATTRIBUTE, modif.getOperation() ); Attribute attr = modif.getAttribute(); assertNotNull( attr ); assertEquals( "ou", attr.getId() ); assertEquals( "acme corp", attr.getString() ); }
public static LdifEntry reverseModify( Dn dn, List<Modification> forwardModifications, Entry modifiedEntry ) throws LdapException { Entry clonedEntry = modifiedEntry.clone(); LdifEntry entry = new LdifEntry(); entry.setChangeType( ChangeType.Modify ); entry.setDn( dn ); List<Modification> reverseModifications = new ArrayList<>(); for ( Modification modification : forwardModifications ) { switch ( modification.getOperation() ) { case ADD_ATTRIBUTE: Attribute mod = modification.getAttribute(); Attribute previous = clonedEntry.get( mod.getId() ); if ( mod.equals( previous ) ) { continue; } Modification reverseModification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REMOVE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( previous == null ) { continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; } reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REPLACE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( ( mod.get() == null ) && ( previous == null ) ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultAttribute( mod.getId() ) ); reverseModifications.add( 0, reverseModification ); continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); continue; } if ( previous == null ) { Attribute emptyAttribute = new DefaultAttribute( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, emptyAttribute ); reverseModifications.add( 0, reverseModification ); continue; } reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; case INCREMENT_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; default: break; } AttributeUtils.applyModification( clonedEntry, modification ); } if ( reverseModifications.isEmpty() ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13465_CANT_DEDUCE_REVERSE_FOR_MOD, forwardModifications ) ); } for ( Modification modification : reverseModifications ) { entry.addModification( modification ); } return entry; }
LdifRevertor { public static LdifEntry reverseModify( Dn dn, List<Modification> forwardModifications, Entry modifiedEntry ) throws LdapException { Entry clonedEntry = modifiedEntry.clone(); LdifEntry entry = new LdifEntry(); entry.setChangeType( ChangeType.Modify ); entry.setDn( dn ); List<Modification> reverseModifications = new ArrayList<>(); for ( Modification modification : forwardModifications ) { switch ( modification.getOperation() ) { case ADD_ATTRIBUTE: Attribute mod = modification.getAttribute(); Attribute previous = clonedEntry.get( mod.getId() ); if ( mod.equals( previous ) ) { continue; } Modification reverseModification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REMOVE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( previous == null ) { continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; } reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REPLACE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( ( mod.get() == null ) && ( previous == null ) ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultAttribute( mod.getId() ) ); reverseModifications.add( 0, reverseModification ); continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); continue; } if ( previous == null ) { Attribute emptyAttribute = new DefaultAttribute( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, emptyAttribute ); reverseModifications.add( 0, reverseModification ); continue; } reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; case INCREMENT_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; default: break; } AttributeUtils.applyModification( clonedEntry, modification ); } if ( reverseModifications.isEmpty() ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13465_CANT_DEDUCE_REVERSE_FOR_MOD, forwardModifications ) ); } for ( Modification modification : reverseModifications ) { entry.addModification( modification ); } return entry; } }
LdifRevertor { public static LdifEntry reverseModify( Dn dn, List<Modification> forwardModifications, Entry modifiedEntry ) throws LdapException { Entry clonedEntry = modifiedEntry.clone(); LdifEntry entry = new LdifEntry(); entry.setChangeType( ChangeType.Modify ); entry.setDn( dn ); List<Modification> reverseModifications = new ArrayList<>(); for ( Modification modification : forwardModifications ) { switch ( modification.getOperation() ) { case ADD_ATTRIBUTE: Attribute mod = modification.getAttribute(); Attribute previous = clonedEntry.get( mod.getId() ); if ( mod.equals( previous ) ) { continue; } Modification reverseModification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REMOVE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( previous == null ) { continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; } reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REPLACE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( ( mod.get() == null ) && ( previous == null ) ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultAttribute( mod.getId() ) ); reverseModifications.add( 0, reverseModification ); continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); continue; } if ( previous == null ) { Attribute emptyAttribute = new DefaultAttribute( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, emptyAttribute ); reverseModifications.add( 0, reverseModification ); continue; } reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; case INCREMENT_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; default: break; } AttributeUtils.applyModification( clonedEntry, modification ); } if ( reverseModifications.isEmpty() ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13465_CANT_DEDUCE_REVERSE_FOR_MOD, forwardModifications ) ); } for ( Modification modification : reverseModifications ) { entry.addModification( modification ); } return entry; } private  LdifRevertor(); }
LdifRevertor { public static LdifEntry reverseModify( Dn dn, List<Modification> forwardModifications, Entry modifiedEntry ) throws LdapException { Entry clonedEntry = modifiedEntry.clone(); LdifEntry entry = new LdifEntry(); entry.setChangeType( ChangeType.Modify ); entry.setDn( dn ); List<Modification> reverseModifications = new ArrayList<>(); for ( Modification modification : forwardModifications ) { switch ( modification.getOperation() ) { case ADD_ATTRIBUTE: Attribute mod = modification.getAttribute(); Attribute previous = clonedEntry.get( mod.getId() ); if ( mod.equals( previous ) ) { continue; } Modification reverseModification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REMOVE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( previous == null ) { continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; } reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REPLACE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( ( mod.get() == null ) && ( previous == null ) ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultAttribute( mod.getId() ) ); reverseModifications.add( 0, reverseModification ); continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); continue; } if ( previous == null ) { Attribute emptyAttribute = new DefaultAttribute( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, emptyAttribute ); reverseModifications.add( 0, reverseModification ); continue; } reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; case INCREMENT_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; default: break; } AttributeUtils.applyModification( clonedEntry, modification ); } if ( reverseModifications.isEmpty() ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13465_CANT_DEDUCE_REVERSE_FOR_MOD, forwardModifications ) ); } for ( Modification modification : reverseModifications ) { entry.addModification( modification ); } return entry; } private  LdifRevertor(); static LdifEntry reverseAdd( Dn dn ); static LdifEntry reverseDel( Dn dn, Entry deletedEntry ); static LdifEntry reverseModify( Dn dn, List<Modification> forwardModifications, Entry modifiedEntry ); static LdifEntry reverseMove( Dn newSuperiorDn, Dn modifiedDn ); static List<LdifEntry> reverseRename( Entry entry, Rdn newRdn, boolean deleteOldRdn ); static List<LdifEntry> reverseMoveAndRename( Entry entry, Dn newSuperior, Rdn newRdn, boolean deleteOldRdn ); }
LdifRevertor { public static LdifEntry reverseModify( Dn dn, List<Modification> forwardModifications, Entry modifiedEntry ) throws LdapException { Entry clonedEntry = modifiedEntry.clone(); LdifEntry entry = new LdifEntry(); entry.setChangeType( ChangeType.Modify ); entry.setDn( dn ); List<Modification> reverseModifications = new ArrayList<>(); for ( Modification modification : forwardModifications ) { switch ( modification.getOperation() ) { case ADD_ATTRIBUTE: Attribute mod = modification.getAttribute(); Attribute previous = clonedEntry.get( mod.getId() ); if ( mod.equals( previous ) ) { continue; } Modification reverseModification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REMOVE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( previous == null ) { continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; } reverseModification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, mod ); reverseModifications.add( 0, reverseModification ); break; case REPLACE_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); if ( ( mod.get() == null ) && ( previous == null ) ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultAttribute( mod.getId() ) ); reverseModifications.add( 0, reverseModification ); continue; } if ( mod.get() == null ) { reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); continue; } if ( previous == null ) { Attribute emptyAttribute = new DefaultAttribute( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, emptyAttribute ); reverseModifications.add( 0, reverseModification ); continue; } reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; case INCREMENT_ATTRIBUTE: mod = modification.getAttribute(); previous = clonedEntry.get( mod.getId() ); reverseModification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, previous ); reverseModifications.add( 0, reverseModification ); break; default: break; } AttributeUtils.applyModification( clonedEntry, modification ); } if ( reverseModifications.isEmpty() ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13465_CANT_DEDUCE_REVERSE_FOR_MOD, forwardModifications ) ); } for ( Modification modification : reverseModifications ) { entry.addModification( modification ); } return entry; } private  LdifRevertor(); static LdifEntry reverseAdd( Dn dn ); static LdifEntry reverseDel( Dn dn, Entry deletedEntry ); static LdifEntry reverseModify( Dn dn, List<Modification> forwardModifications, Entry modifiedEntry ); static LdifEntry reverseMove( Dn newSuperiorDn, Dn modifiedDn ); static List<LdifEntry> reverseRename( Entry entry, Rdn newRdn, boolean deleteOldRdn ); static List<LdifEntry> reverseMoveAndRename( Entry entry, Dn newSuperior, Rdn newRdn, boolean deleteOldRdn ); static final boolean DELETE_OLD_RDN; static final boolean KEEP_OLD_RDN; }