directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1655504 - in /directory/shared/trunk/ldap: client/api/src/test/java/org/apache/directory/ldap/client/api/search/ model/src/main/java/org/apache/directory/api/ldap/model/filter/ model/src/test/java/org/apache/directory/api/ldap/model/filter/
Date Thu, 29 Jan 2015 00:39:00 GMT
Author: elecharny
Date: Thu Jan 29 00:39:00 2015
New Revision: 1655504

URL: http://svn.apache.org/r1655504
Log:
Fixed the FIlterEncoder class (DIRPI-220)

Modified:
    directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/AttributeValueAssertionTest.java
    directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/FilterBuilderTest.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/filter/FilterEncoder.java
    directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/filter/FilterEncoderTest.java

Modified: directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/AttributeValueAssertionTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/AttributeValueAssertionTest.java?rev=1655504&r1=1655503&r2=1655504&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/AttributeValueAssertionTest.java
(original)
+++ directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/AttributeValueAssertionTest.java
Thu Jan 29 00:39:00 2015
@@ -54,7 +54,7 @@ public class AttributeValueAssertionTest
         assertEquals( "(uid=admin)",
             AttributeValueAssertionFilter.equal( "uid", "admin" )
                 .build().toString() );
-        assertEquals( "(cn=lu*)",
+        assertEquals( "(cn=lu\\2A)",
             AttributeValueAssertionFilter.equal( "cn", "lu*" )
                 .build().toString() );
     }

Modified: directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/FilterBuilderTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/FilterBuilderTest.java?rev=1655504&r1=1655503&r2=1655504&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/FilterBuilderTest.java
(original)
+++ directory/shared/trunk/ldap/client/api/src/test/java/org/apache/directory/ldap/client/api/search/FilterBuilderTest.java
Thu Jan 29 00:39:00 2015
@@ -47,11 +47,11 @@ public class FilterBuilderTest
     {
         assertEquals( "(cn=Babs Jensen)", equal( "cn", "Babs Jensen" ).toString() );
         assertEquals( "(!(cn=Tim Howes))", not( equal( "cn", "Tim Howes" ) ).toString() );
-        assertEquals( "(&(objectClass=Person)(|(sn=Jensen)(cn=Babs J*)))",
+        assertEquals( "(&(objectClass=Person)(|(sn=Jensen)(cn=Babs J\\2A)))",
             and( equal( "objectClass", "Person" ),
                 or( equal( "sn", "Jensen" ),
                     equal( "cn", "Babs J*" ) ) ).toString() );
-        assertEquals( "(o=univ*of*mich*)", equal( "o", "univ*of*mich*" ).toString() );
+        assertEquals( "(o=univ\\2Aof\\2Amich\\2A)", equal( "o", "univ*of*mich*" ).toString()
);
     }
 
 
@@ -88,6 +88,8 @@ public class FilterBuilderTest
         assertEquals( "(o=*)", contains( "o" ).toString() );
         assertEquals( "(o=*of*)", contains( "o", "of" ).toString() );
         assertEquals( "(o=*sit*of*chi*)", contains( "o", "sit", "of", "chi" ).toString()
);
+        assertEquals( "(cn=*\u00e9*)", contains( "cn", "\u00e9" ).toString() );
+        assertEquals( "(cn=*\\C3\\E9*)", contains( "cn", "\\C3\\E9" ).toString() );
     }
 
 

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/filter/FilterEncoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/filter/FilterEncoder.java?rev=1655504&r1=1655503&r2=1655504&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/filter/FilterEncoder.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/filter/FilterEncoder.java
Thu Jan 29 00:39:00 2015
@@ -85,51 +85,182 @@ public class FilterEncoder
      */
     public static String encodeFilterValue( String value )
     {
-        StringBuilder sb = null;
+        StringBuilder sb = new StringBuilder( value.length() );
+        boolean escaped = false;
+        boolean hexPair = false;
+        char hex = '\0';
 
         for ( int i = 0; i < value.length(); i++ )
         {
             char ch = value.charAt( i );
-            String replace = null;
 
             switch ( ch )
             {
                 case '*':
-                    replace = "\\2A";
+                    if ( escaped )
+                    {
+                        sb.append( "\\5C" );
+
+                        if ( hexPair )
+                        {
+                            sb.append( hex );
+                            hexPair = false;
+                        }
+
+                        escaped = false;
+                    }
+
+                    sb.append( "\\2A" );
                     break;
 
                 case '(':
-                    replace = "\\28";
+                    if ( escaped )
+                    {
+                        sb.append( "\\5C" );
+
+                        if ( hexPair )
+                        {
+                            sb.append( hex );
+                            hexPair = false;
+                        }
+
+                        escaped = false;
+                    }
+
+                    sb.append( "\\28" );
                     break;
 
                 case ')':
-                    replace = "\\29";
+                    if ( escaped )
+                    {
+                        sb.append( "\\5C" );
+
+                        if ( hexPair )
+                        {
+                            sb.append( hex );
+                            hexPair = false;
+                        }
+
+                        escaped = false;
+                    }
+
+                    sb.append( "\\29" );
+                    break;
+
+                case '\0':
+                    if ( escaped )
+                    {
+                        sb.append( "\\5C" );
+
+                        if ( hexPair )
+                        {
+                            sb.append( hex );
+                            hexPair = false;
+                        }
+
+                        escaped = false;
+                    }
+
+                    sb.append( "\\00" );
                     break;
 
                 case '\\':
-                    replace = "\\5C";
+                    if ( escaped )
+                    {
+                        sb.append( "\\5C" );
+                        escaped = false;
+                    }
+                    else
+                    {
+                        escaped = true;
+                        hexPair = false;
+                    }
+
                     break;
 
-                case '\0':
-                    replace = "\\00";
+                case '0':
+                case '1':
+                case '2':
+                case '3':
+                case '4':
+                case '5':
+                case '6':
+                case '7':
+                case '8':
+                case '9':
+                case 'a':
+                case 'b':
+                case 'c':
+                case 'd':
+                case 'e':
+                case 'f':
+                case 'A':
+                case 'B':
+                case 'C':
+                case 'D':
+                case 'E':
+                case 'F':
+                    if ( escaped )
+                    {
+                        if ( hexPair )
+                        {
+                            sb.append( '\\' ).append( hex ).append( ch );
+                            escaped = false;
+                            hexPair = false;
+                        }
+                        else
+                        {
+                            hexPair = true;
+                            hex = ch;
+                        }
+                    }
+                    else
+                    {
+                        sb.append( ch );
+                    }
+
                     break;
-            }
 
-            if ( replace != null )
-            {
-                if ( sb == null )
-                {
-                    sb = new StringBuilder( value.length() * 2 );
-                    sb.append( value.substring( 0, i ) );
-                }
-                sb.append( replace );
-            }
-            else if ( sb != null )
-            {
-                sb.append( ch );
+                default:
+                    if ( escaped )
+                    {
+                        sb.append( "\\5C" );
+
+                        if ( hexPair )
+                        {
+                            sb.append( hex );
+                            hexPair = false;
+                        }
+
+                        escaped = false;
+                    }
+
+                    sb.append( ch );
             }
         }
 
+        if ( escaped )
+        {
+            sb.append( "\\5C" );
+        }
+
         return ( sb == null ? value : sb.toString() );
     }
+
+
+    private static void handleEscaped( boolean escaped, boolean hexPair, char hex, StringBuilder
sb )
+    {
+        if ( escaped )
+        {
+            sb.append( "\\5C" );
+
+            if ( hexPair )
+            {
+                sb.append( hex );
+                hexPair = false;
+            }
+
+            escaped = false;
+        }
+    }
 }

Modified: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/filter/FilterEncoderTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/filter/FilterEncoderTest.java?rev=1655504&r1=1655503&r2=1655504&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/filter/FilterEncoderTest.java
(original)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/filter/FilterEncoderTest.java
Thu Jan 29 00:39:00 2015
@@ -24,7 +24,6 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import org.apache.directory.api.ldap.model.filter.FilterEncoder;
 import org.junit.Test;
 
 
@@ -117,6 +116,7 @@ public class FilterEncoderTest
         assertEquals( "\\5C", FilterEncoder.encodeFilterValue( "\\" ) );
         assertEquals( "\\00", FilterEncoder.encodeFilterValue( "\0" ) );
         assertEquals( "\\28\\2A\\29", FilterEncoder.encodeFilterValue( "(*)" ) );
+        assertEquals( "a test \\2A \\5Cend", FilterEncoder.encodeFilterValue( "a test \\2A
\\end" ) );
     }
 
 }



Mime
View raw message