directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r432492 - in /directory/trunks: apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/ shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/ shared/ldap/src/test/java/org/apache/directory/shared/l...
Date Fri, 18 Aug 2006 05:34:27 GMT
Author: akarasulu
Date: Thu Aug 17 22:34:26 2006
New Revision: 432492

URL: http://svn.apache.org/viewvc?rev=432492&view=rev
Log:
Applying pams patches for DIRSERVER-612

Added:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
    directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
Modified:
    directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java

Modified: directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java?rev=432492&r1=432491&r2=432492&view=diff
==============================================================================
--- directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java
(original)
+++ directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java
Thu Aug 17 22:34:26 2006
@@ -51,10 +51,12 @@
 import org.apache.directory.server.tools.util.ListenerParameter;
 import org.apache.directory.server.tools.util.Parameter;
 import org.apache.directory.server.tools.util.ToolCommandException;
+import org.apache.directory.shared.ldap.ldif.LdifUtils;
 import org.apache.directory.shared.ldap.message.LockableAttributeImpl;
 import org.apache.directory.shared.ldap.message.LockableAttributesImpl;
 import org.apache.directory.shared.ldap.schema.AttributeType;
 import org.apache.directory.shared.ldap.schema.UsageEnum;
+import org.apache.directory.shared.ldap.util.Base64;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 
@@ -331,7 +333,31 @@
                     entry.put( myattr );
                     for ( int ii = 0; ii < attr.size(); ii++ )
                     {
-                        myattr.add( attr.get( ii ) );
+                        Object value = attr.get(ii);
+                        
+                        // Checking if the value is binary
+                        if ( value instanceof byte[] )
+                        {
+                        	// It is binary, so we have to encode it using Base64 before adding
it
+                        	char[] encoded = Base64.encode( ( byte[] ) value );
+                        	
+                        	myattr.add( new String( encoded ) );                        	
+                        }
+                        else if ( value instanceof String )
+                        {
+                        	// It's a String but, we have to check if encoding isn't required
+                        	String str = (String) value;
+                        	if ( !LdifUtils.isLDIFSafe( str ) )
+                        	{
+                        		char[] encoded = Base64.encode( ( ( String ) value ).getBytes()
);
+                        		
+                        		myattr.add( new String( encoded ) );
+                        	}
+                        	else
+                        	{
+                        		myattr.add( value );
+                        	}
+                        }
                     }
                 }
             }
@@ -347,7 +373,6 @@
             buf.setLength( 0 );
         }
     }
-
 
     private void filterAttributes( String dn, Attributes entry ) throws NamingException
     {

Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java?rev=432492&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
(added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
Thu Aug 17 22:34:26 2006
@@ -0,0 +1,105 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.directory.shared.ldap.ldif;
+
+
+/**
+ * Some LDIF useful methods
+ *
+ */
+public class LdifUtils
+{
+
+	/** The array that will be used to match the first char.*/
+    private static boolean[] LDIF_SAFE_STARTING_CHAR_ALPHABET = new boolean[128];
+    
+    /** The array that will be used to match the other chars.*/
+    private static boolean[] LDIF_SAFE_OTHER_CHARS_ALPHABET = new boolean[128];
+    
+    static
+    {
+    	// Initialization of the array that will be used to match the first char.
+    	for (int i = 0; i < 128; i++) {
+    		LDIF_SAFE_STARTING_CHAR_ALPHABET[i] = true;
+		}
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[0] = false; // 0 (NUL)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[10] = false; // 10 (LF)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[13] = false; // 13 (CR)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[32] = false; // 32 (SPACE)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[58] = false; // 58 (:)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[60] = false; // 60 (>)
+    	
+    	// Initialization of the array that will be used to match the other chars.
+    	for (int i = 0; i < 128; i++) {
+    		LDIF_SAFE_OTHER_CHARS_ALPHABET[i] = true;
+		}
+    	LDIF_SAFE_OTHER_CHARS_ALPHABET[0] = false; // 0 (NUL)
+    	LDIF_SAFE_OTHER_CHARS_ALPHABET[10] = false; // 10 (LF)
+    	LDIF_SAFE_OTHER_CHARS_ALPHABET[13] = false; // 13 (CR)
+    }
+
+    /**
+     * Checks if the input String contains only safe values, that is, the data
+     * does not need to be encoded for use with LDIF. The rules for checking safety
+     * are based on the rules for LDIF (LDAP Data Interchange Format) per RFC 2849.
+     * The data does not need to be encoded if all the following are true:
+     * 
+     * The data cannot start with the following char values:
+     * 		00 (NUL)
+     * 		10 (LF)
+     * 		13 (CR)
+     * 		32 (SPACE)
+     * 		58 (:)
+     * 		60 (<)
+     * 		Any character with value greater than 127
+     * 
+     * The data cannot contain any of the following char values:
+     * 		00 (NUL)
+     * 		10 (LF)
+     * 		13 (CR)
+     * 		Any character with value greater than 127
+     * 
+     * The data cannot end with a space.
+     * 
+     * @param str the String to be checked
+     * @return true if encoding not required for LDIF
+     */
+    public static boolean isLDIFSafe( String str )
+    {
+    	// Checking the first char
+    	char currentChar = str.charAt(0);
+    	if ( currentChar > 127 || !LDIF_SAFE_STARTING_CHAR_ALPHABET[currentChar] )
+    	{
+    		return false;
+    	}
+    	
+    	// Checking the other chars
+    	for (int i = 1; i < str.length(); i++)
+    	{
+        	currentChar = str.charAt(i);
+        	
+        	if ( currentChar > 127 || !LDIF_SAFE_OTHER_CHARS_ALPHABET[currentChar] )
+        	{
+        		return false;
+        	}
+		}
+    	
+    	// The String cannot end with a space
+    	return ( currentChar != ' ' );
+    }
+}
+

Added: directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java?rev=432492&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
(added)
+++ directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
Thu Aug 17 22:34:26 2006
@@ -0,0 +1,186 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.directory.shared.ldap.ldif;
+
+import junit.framework.TestCase;
+
+public class LdifUtilsTest extends TestCase
+{
+	private String testString = "this is a test";
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char NUL (ASCII code 0)
+	 */
+	public void testIsLdifSafeStartingWithNUL()
+    {
+		char c = ( char ) 0;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char LF (ASCII code 10)
+	 */
+	public void testIsLdifSafeStartingWithLF()
+    {
+		char c = ( char ) 10;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char CR (ASCII code 13)
+	 */
+	public void testIsLdifSafeStartingWithCR()
+    {
+		char c = ( char ) 13;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char SPACE (ASCII code 32)
+	 */
+	public void testIsLdifSafeStartingWithSpace()
+    {
+		char c = ( char ) 32;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char COLON (:) (ASCII code 58)
+	 */
+	public void testIsLdifSafeStartingWithColon()
+    {
+		char c = ( char ) 58;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char LESS_THAN (<) (ASCII code 60)
+	 */
+	public void testIsLdifSafeStartingWithLessThan()
+    {
+		char c = ( char ) 60;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char with ASCII code 127
+	 */
+	public void testIsLdifSafeStartingWithCharGreaterThan127()
+    {
+		char c = ( char ) 127;
+		
+		assertTrue( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char with ASCII code greater than 127
+	 */
+	public void testIsLdifSafeStartingWithCharGreaterThan127Bis()
+    {
+		char c = ( char ) 222;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char NUL (ASCII code 0)
+	 */
+	public void testIsLdifSafeContainsNUL()
+    {
+		char c = ( char ) 0;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char LF (ASCII code 10)
+	 */
+	public void testIsLdifSafeContainsLF()
+    {
+		char c = ( char ) 10;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char CR (ASCII code 13)
+	 */
+	public void testIsLdifSafeContainsCR()
+    {
+		char c = ( char ) 13;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char with ASCII code 127
+	 */
+	public void testIsLdifSafeContainsCharGreaterThan127()
+    {
+		char c = ( char ) 127;
+		
+		assertTrue( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing a
+	 * char with ASCII code greater than 127
+	 */
+	public void testIsLdifSafeContainsCharGreaterThan127Bis()
+    {
+		char c = ( char ) 328;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String ending with the
+	 * char SPACE (ASCII code 32)
+	 */
+	public void testIsLdifSafeEndingWithSpace()
+    {
+		char c = ( char ) 32;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString  + c) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a correct String
+	 */
+	public void testIsLdifSafeCorrectString()
+    {		
+		assertTrue( LdifUtils.isLDIFSafe( testString ) );
+    }
+}



Mime
View raw message