directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1709634 - /directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdifAnonymizer.java
Date Tue, 20 Oct 2015 16:16:03 GMT
Author: elecharny
Date: Tue Oct 20 16:16:03 2015
New Revision: 1709634

URL: http://svn.apache.org/viewvc?rev=1709634&view=rev
Log:
o Injected the SchemaManager into the LdifAnonymizer
o Allow the addition of some dedicated anonymizers
o Check for missing NamingContext
o Added a Writer so that the result is written on the flow, instead of accumulating the result
in a String, to avoid OOM
o Added some output to expose the progression
o Handle a few errors

Modified:
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdifAnonymizer.java

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdifAnonymizer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdifAnonymizer.java?rev=1709634&r1=1709633&r2=1709634&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdifAnonymizer.java
(original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdifAnonymizer.java
Tue Oct 20 16:16:03 2015
@@ -22,8 +22,11 @@ package org.apache.directory.ldap.client
 
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -235,7 +238,7 @@ public class LdifAnonymizer
             {
                 attributeAnonymizers.put( attributeType.getOid(), new IntegerAnonymizer()
);
             }
-            else
+            if ( syntax.getOid().equals( SchemaConstants.DIRECTORY_STRING_SYNTAX ) )
             {
                 attributeAnonymizers.put( attributeType.getOid(), new StringAnonymizer()
);
             }
@@ -248,6 +251,20 @@ public class LdifAnonymizer
     
     
     /**
+     * Add an attributeType that has to be anonymized, with its associated anonymizer.
+     *
+     * @param attributeType the AttributeType that has to be anonymized
+     * @param anonymizer the instance of anonymizer to use with this AttributeType
+     * @throws LdapException If the attributeType cannot be added
+     */
+    public void addAnonAttributeType( AttributeType attributeType, Anonymizer<?> anonymizer
) throws LdapException
+    {
+        schemaManager.add( attributeType );
+        attributeAnonymizers.put( attributeType.getOid(), anonymizer );
+    }
+    
+    
+    /**
      * Remove an attributeType that has to be anonymized
      *
      * @param attributeType the AttributeType that we don't want to be anonymized
@@ -286,7 +303,7 @@ public class LdifAnonymizer
         {
             Attribute attribute = new DefaultAttribute( attributeType );
             attribute.add( value );
-            Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType()
);
+            Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType().getOid()
);
 
             if ( value.isHumanReadable() )
             {
@@ -349,6 +366,11 @@ public class LdifAnonymizer
                 break;
             }
         }
+        
+        if ( namingContext == null )
+        {
+            throw new LdapException( "No naming context attached with this entry : " + entryDn
);
+        }
 
         Rdn[] anonymizedRdns = new Rdn[entryDn.size()];
         int rdnPos = entryDn.size() - 1;
@@ -393,66 +415,121 @@ public class LdifAnonymizer
      * @throws LdapException If we got some LDAP related exception
      * @throws IOException If we had some issue during some IO operations
      */
-    public String anonymizeFile( String ldifFile ) throws LdapException, IOException
+    public void anonymizeFile( String ldifFile, Writer writer ) throws LdapException, IOException
     {
-        LdifReader ldifReader = new LdifReader( schemaManager );
+        File inputFile = new File( ldifFile );
+        
+        if ( !inputFile.exists() )
+        {
+            System.out.println( "Cannot open file " + ldifFile );
+            return;
+        }
+        
+        LdifReader ldifReader = new LdifReader( inputFile, schemaManager );
+        int count = 0;
+        List<LdifEntry> errors = new ArrayList<LdifEntry>();
 
         try
         {
-            List<LdifEntry> entries = ldifReader.parseLdifFile( ldifFile );
-            StringBuilder result = new StringBuilder();
-
-            for ( LdifEntry ldifEntry : entries )
+            for ( LdifEntry ldifEntry : ldifReader )
             {
-                Entry entry = ldifEntry.getEntry();
-                Entry newEntry = new DefaultEntry( schemaManager );
-
-                // Process the DN first
-                Dn entryDn = entry.getDn();
+                count++;
                 
-                Dn anonymizedDn = anonymizeDn( entryDn );
-
-                // Now, process the entry
-                for ( Attribute attribute : entry )
+                try
                 {
-                    AttributeType attributeType = attribute.getAttributeType();
+                    Entry entry = ldifEntry.getEntry();
+                    Entry newEntry = new DefaultEntry( schemaManager );
+    
+                    // Process the DN first
+                    Dn entryDn = entry.getDn();
                     
-                    if ( attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker
)
+                    Dn anonymizedDn = anonymizeDn( entryDn );
+                    
+                    if ( anonymizedDn == null )
                     {
-                        for ( Value<?> dnValue : attribute )
-                        {
-                            Dn dn = new Dn( schemaManager, dnValue.getString() );
-                            Dn newdDn = anonymizeDn( dn );
-                            newEntry.add( attributeType, newdDn.toString() );
-                        }
+                        // Wrong entry base DN
+                        continue;
                     }
-                    else
-                    {
-                        int h = attribute.getAttributeType().hashCode();
-                        Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType().getOid()
);
     
-                        if ( anonymizer == null )
+                    // Now, process the entry
+                    for ( Attribute attribute : entry )
+                    {
+                        AttributeType attributeType = attribute.getAttributeType();
+                        
+                        if ( attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker
)
                         {
-                            newEntry.add( attribute );
+                            for ( Value<?> dnValue : attribute )
+                            {
+                                String dnStr = dnValue.getString();
+                                Dn dn = new Dn( schemaManager, dnStr );
+                                Dn newdDn = anonymizeDn( dn );
+                                newEntry.add( attributeType, newdDn.toString() );
+                            }
                         }
                         else
                         {
-                            Attribute anonymizedAttribute = anonymizer.anonymize( valueMap,
attribute );
-    
-                            newEntry.add( anonymizedAttribute );
+                            Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType().getOid()
);
+        
+                            if ( anonymizer == null )
+                            {
+                                newEntry.add( attribute );
+                            }
+                            else
+                            {
+                                Attribute anonymizedAttribute = anonymizer.anonymize( valueMap,
attribute );
+        
+                                newEntry.add( anonymizedAttribute );
+                            }
                         }
                     }
+
+                    newEntry.setDn( anonymizedDn );
+                    writer.write( LdifUtils.convertToLdif( newEntry ) );
+                    writer.write( "\n" );
+
+                    System.out.print( '.' );
+                    
+                    if ( count % 100  == 0 )
+                    {
+                        System.out.println();
+                    }
                 }
+                catch ( Exception e )
+                {
+                    System.out.print( '*' );
 
-                newEntry.setDn( anonymizedDn );
-                result.append( LdifUtils.convertToLdif( newEntry ) );
-                result.append( "\n" );
+                    if ( count % 100  == 0 )
+                    {
+                        System.out.println();
+                    }
+                    
+                    errors.add( ldifEntry );
+                }
             }
 
-            return result.toString();
+            System.out.println();
+            
+            if ( errors.size() != 0 )
+            {
+                System.out.println( "There are " + errors.size() + " bad entries" );
+                
+                for ( LdifEntry ldifEntry : errors )
+                {
+                    System.out.println( "---------------------------------------------------"
);
+                    System.out.println( ldifEntry.getDn() );
+                }
+            }
         }
         finally
         {
+            System.out.println();
+
+            if ( errors.size() != 0 )
+            {
+                System.out.println( "There are " + errors.size() + " bad entries" );
+            }
+                
+            System.out.println( "Nb entries : " + count ); 
             ldifReader.close();
         }
     }
@@ -484,12 +561,19 @@ public class LdifAnonymizer
                 Dn entryDn = entry.getDn();
                 
                 Dn anonymizedDn = anonymizeDn( entryDn );
+                
+                if ( anonymizedDn == null )
+                {
+                    continue;
+                }
 
                 // Now, process the entry
                 for ( Attribute attribute : entry )
                 {
                     AttributeType attributeType = attribute.getAttributeType();
                     
+                    // Deal with the special case of 
+                    
                     if ( attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker
)
                     {
                         for ( Value<?> dnValue : attribute )
@@ -501,7 +585,7 @@ public class LdifAnonymizer
                     }
                     else
                     {
-                        Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType()
);
+                        Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType().getOid()
);
     
                         if ( anonymizer == null )
                         {
@@ -510,8 +594,11 @@ public class LdifAnonymizer
                         else
                         {
                             Attribute anonymizedAttribute = anonymizer.anonymize( valueMap,
attribute );
-    
-                            newEntry.add( anonymizedAttribute );
+                            
+                            if ( anonymizedAttribute != null )
+                            {
+                                newEntry.add( anonymizedAttribute );
+                            }
                         }
                     }
                 }
@@ -523,6 +610,11 @@ public class LdifAnonymizer
 
             return result.toString();
         }
+        catch ( Exception e )
+        {
+            System.out.println( "Error :"  + e.getMessage() );
+            return null;
+        }
         finally
         {
             ldifReader.close();



Mime
View raw message