You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2015/10/05 11:49:11 UTC

svn commit: r1706777 - in /directory/shared/trunk/ldap: client/api/src/main/java/org/apache/directory/ldap/client/api/ model/src/main/java/org/apache/directory/api/ldap/model/ldif/ model/src/main/java/org/apache/directory/api/ldap/model/schema/

Author: elecharny
Date: Mon Oct  5 09:49:11 2015
New Revision: 1706777

URL: http://svn.apache.org/viewvc?rev=1706777&view=rev
Log:
o Renamed the anonymize() method to anonymizeFile()
o Added get/set for the valueMap
o Created the missing AttributeType on the fly when not present
o Exposed the relaxed mode in the SchemaManager

Modified:
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdifAnonymizer.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaManager.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=1706777&r1=1706776&r2=1706777&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 Mon Oct  5 09:49:11 2015
@@ -383,7 +383,79 @@ public class LdifAnonymizer
         
         return anonymizedDn;
     }
+
+
+    /**
+     * Anonymize a LDIF 
+     * 
+     * @param ldif The ldif content to anonymize
+     * @return an anonymized version of the given ldif
+     * @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
+    {
+        LdifReader ldifReader = new LdifReader( schemaManager );
+
+        try
+        {
+            List<LdifEntry> entries = ldifReader.parseLdifFile( ldifFile );
+            StringBuilder result = new StringBuilder();
+
+            for ( LdifEntry ldifEntry : entries )
+            {
+                Entry entry = ldifEntry.getEntry();
+                Entry newEntry = new DefaultEntry( schemaManager );
+
+                // Process the DN first
+                Dn entryDn = entry.getDn();
+                
+                Dn anonymizedDn = anonymizeDn( entryDn );
+
+                // Now, process the entry
+                for ( Attribute attribute : entry )
+                {
+                    AttributeType attributeType = attribute.getAttributeType();
+                    
+                    if ( attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker )
+                    {
+                        for ( Value<?> dnValue : attribute )
+                        {
+                            Dn dn = new Dn( schemaManager, dnValue.getString() );
+                            Dn newdDn = anonymizeDn( dn );
+                            newEntry.add( attributeType, newdDn.toString() );
+                        }
+                    }
+                    else
+                    {
+                        Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType() );
+    
+                        if ( anonymizer == null )
+                        {
+                            newEntry.add( attribute );
+                        }
+                        else
+                        {
+                            Attribute anonymizedAttribute = anonymizer.anonymize( valueMap, attribute );
     
+                            newEntry.add( anonymizedAttribute );
+                        }
+                    }
+                }
+
+                newEntry.setDn( anonymizedDn );
+                result.append( LdifUtils.convertToLdif( newEntry ) );
+                result.append( "\n" );
+            }
+
+            return result.toString();
+        }
+        finally
+        {
+            ldifReader.close();
+        }
+    }
+
 
     /**
      * Anonymize a LDIF 
@@ -457,6 +529,24 @@ public class LdifAnonymizer
     }
 
 
+    /**
+     * @return the valueMap
+     */
+    public Map<Value<?>, Value<?>> getValueMap()
+    {
+        return valueMap;
+    }
+
+
+    /**
+     * @param valueMap the valueMap to set
+     */
+    public void setValueMap( Map<Value<?>, Value<?>> valueMap )
+    {
+        this.valueMap = valueMap;
+    }
+
+
     /**
      * The entry point, when used as a standalone application.
      *

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java?rev=1706777&r1=1706776&r2=1706777&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java Mon Oct  5 09:49:11 2015
@@ -43,6 +43,7 @@ import java.util.NoSuchElementException;
 
 import org.apache.directory.api.asn1.util.Oid;
 import org.apache.directory.api.i18n.I18n;
+import org.apache.directory.api.ldap.model.constants.SchemaConstants;
 import org.apache.directory.api.ldap.model.entry.Attribute;
 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
 import org.apache.directory.api.ldap.model.entry.ModificationOperation;
@@ -50,8 +51,11 @@ import org.apache.directory.api.ldap.mod
 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
 import org.apache.directory.api.ldap.model.message.Control;
+import org.apache.directory.api.ldap.model.name.Ava;
 import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.api.ldap.model.name.Rdn;
 import org.apache.directory.api.ldap.model.schema.AttributeType;
+import org.apache.directory.api.ldap.model.schema.MutableAttributeType;
 import org.apache.directory.api.ldap.model.schema.SchemaManager;
 import org.apache.directory.api.util.Base64;
 import org.apache.directory.api.util.Chars;
@@ -164,7 +168,8 @@ import org.slf4j.LoggerFactory;
  *  - The ValueSpec rule must accept multilines values. In this case, we have a LF followed by a
  *  single space before the continued value.
  * </pre>
- *
+ * The relaxed mode is used when a SchemaManager is injected.
+ * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 public class LdifReader implements Iterable<LdifEntry>, Closeable
@@ -243,6 +248,9 @@ public class LdifReader implements Itera
 
     /** flag to turn on/off of the DN validation. By default DNs are validated after parsing */
     protected boolean validateDn = true;
+    
+    /** A counter used to create facked OIDs */
+    private int oidCounter = 0;
 
 
     /**
@@ -1049,7 +1057,22 @@ public class LdifReader implements Itera
         }
 
         // Update the entry
-        entry.addAttribute( attributeType, attributeValue );
+        try
+        {
+            entry.addAttribute( attributeType, attributeValue );
+        }
+        catch ( Exception e )
+        {
+            // The attribute does not exist already, create a fake one 
+            if ( ( schemaManager != null ) && schemaManager.isRelaxed() )
+            {
+                MutableAttributeType newAttributeType = new MutableAttributeType( "1.3.6.1.4.1.18060.0.9999." + oidCounter++ );
+                newAttributeType.setNames( attributeType );
+                newAttributeType.setSyntax( schemaManager.getLdapSyntaxRegistry().get( SchemaConstants.DIRECTORY_STRING_SYNTAX ) );
+                schemaManager.add( newAttributeType );
+                entry.addAttribute( attributeType, attributeValue );
+            }
+        }
     }
 
 
@@ -1421,7 +1444,36 @@ public class LdifReader implements Itera
 
         String name = parseDn( line );
 
-        Dn dn = new Dn( schemaManager, name );
+        Dn dn = null;
+        
+        try
+        {
+            dn = new Dn( schemaManager, name );
+        }
+        catch ( LdapInvalidDnException lide )
+        {
+            // Deal with the RDN whihc is not in the schema
+            // First parse the DN without the schema
+            dn = new Dn( name );
+            
+            Rdn rdn = dn.getRdn();
+            
+            // Process each Ava
+            for ( Ava ava : rdn )
+            {
+                if ( ( schemaManager != null ) && ( schemaManager.getAttributeType( ava.getType() ) == null ) 
+                    && schemaManager.isRelaxed() )
+                {
+                    // Not found : create a new one
+                    MutableAttributeType newAttributeType = new MutableAttributeType( "1.3.6.1.4.1.18060.0.9999." + oidCounter++ );
+                    newAttributeType.setNames( ava.getType() );
+                    newAttributeType.setSyntax( schemaManager.getLdapSyntaxRegistry().get( SchemaConstants.DIRECTORY_STRING_SYNTAX ) );
+                    schemaManager.add( newAttributeType );
+                }
+            }
+            
+            dn = new Dn( schemaManager, name );
+        }
 
         // Ok, we have found a Dn
         LdifEntry entry = createLdifEntry( schemaManager );

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaManager.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaManager.java?rev=1706777&r1=1706776&r2=1706777&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaManager.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaManager.java Mon Oct  5 09:49:11 2015
@@ -419,12 +419,23 @@ public interface SchemaManager
      */
     boolean isRelaxed();
 
+
+    /**
+     * Set the SchemaManager to a RELAXED mode
+     */
+    void setRelaxed();
+
     /**
      * Tells if the SchemaManager is strict.
      *
      * @return True if SchemaObjects cannot be added if they break the consistency
      */
     boolean isStrict();
+
+    /**
+     * Set the SchemaManager to a STRICT mode
+     */
+    void setStrict();
     
     /**
      * Check that the Schemas are consistent regarding the current Registries.