@Test public void testReverseMultipleModifications() throws Exception { String initialEntryLdif = "dn: cn=test, ou=system\n" + "objectclass: top\n" + "objectclass: person\n" + "cn: test\n" + "sn: joe doe\n" + "l: USA\n" + "ou: apache\n" + "ou: acme corp\n"; LdifReader reader = new LdifReader(); List<LdifEntry> entries = reader.parseLdif( initialEntryLdif ); reader.close(); LdifEntry initialEntry = entries.get( 0 ); Dn dn = new Dn( "cn=test, ou=system" ); List<Modification> modifications = new ArrayList<Modification>(); Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, new DefaultAttribute( "ou", "BigCompany inc." ) ); modifications.add( mod ); mod = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, new DefaultAttribute( "l" ) ); modifications.add( mod ); mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, new DefaultAttribute( "l", "FR" ) ); modifications.add( mod ); mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultAttribute( "l", "USA" ) ); modifications.add( mod ); mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultAttribute( "ou", "apache" ) ); modifications.add( mod ); LdifEntry reversedEntry = LdifRevertor.reverseModify( dn, modifications, initialEntry.getEntry() ); String expectedEntryLdif = "dn: cn=test, ou=system\n" + "changetype: modify\n" + "replace: ou\n" + "ou: apache\n" + "ou: acme corp\n" + "ou: BigCompany inc.\n" + "-\n" + "replace: l\n" + "l: FR\n" + "-\n" + "delete: l\n" + "l: FR\n" + "-\n" + "add: l\n" + "l: USA\n" + "-\n" + "delete: ou\n" + "ou: BigCompany inc.\n" + "-\n\n"; reader = new LdifReader(); entries = reader.parseLdif( expectedEntryLdif ); reader.close(); LdifEntry expectedEntry = entries.get( 0 ); assertEquals( expectedEntry, reversedEntry ); }
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; }