directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r224597 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/LdapURL.java
Date Sun, 24 Jul 2005 10:17:27 GMT
Author: elecharny
Date: Sun Jul 24 03:17:24 2005
New Revision: 224597

URL: http://svn.apache.org/viewcvs?rev=224597&view=rev
Log:
- Added a private method to encode the LdapURL according to RFC 2255. This RFC is really nor
clear about
how an URL must be encoded, so this method is really dirty...

- fixed the toString() method to deal with the previous point

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/LdapURL.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/LdapURL.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/LdapURL.java?rev=224597&r1=224596&r2=224597&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/LdapURL.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/LdapURL.java
Sun Jul 24 03:17:24 2005
@@ -912,9 +912,62 @@
     }
 
     /**
+     * Encode a String to avoid special characters
+     * 
+     * *NOTE* : this is an ugly function, just needed because the
+     * RFC 2255 is VERY unclear about the way LDAP searches are
+     * to be encoded. Some references to RFC 1738 are made,
+     * but they are really useless and inadequat.
+     *   
+     * @param string The String to encode
+     * @param doubleEncode Set if we need to encode the comma
+     * @return An encoded string
+     */
+    private String urlEncode( String string, boolean doubleEncode)
+    {
+        StringBuffer sb = new StringBuffer();
+        
+        for (int i = 0; i < string.length(); i++)
+        {
+            char c = string.charAt(i);
+            
+            switch (c)
+            {
+	            case ' ' :
+	                sb.append("%20");
+	                break;
+	                
+	            case '?' :
+	                sb.append("%3f");
+	                break;
+	                
+	            case '\\' :
+	                sb.append("%5c");
+	                break;
+	                
+	            case ',' :
+	                if (doubleEncode)
+	                {
+	                    sb.append("%2c");
+	                }
+	                else
+	                {
+	                    sb.append( c );
+	                }
+	                break;
+	                
+	            default :
+	                sb.append( c );
+            }
+        }
+        
+        return sb.toString();
+    }
+    
+    /**
      * Get a string representation of a LdapURL.
      * @return A LdapURL string
-    */
+     */
     public String toString()
     {
 
@@ -929,7 +982,7 @@
 
         if ( dn != null )
         {
-            sb.append( '/' ).append( dn );
+            sb.append( '/' ).append( urlEncode(dn.toString(), false ) );
 
             if ( ( attributes.size() != 0 ) ||
                     ( ( scope != SearchControls.OBJECT_SCOPE ) ||
@@ -947,7 +1000,7 @@
                         sb.append( ',' );
                     }
 
-                    sb.append( attributes.get( i ) );
+                    sb.append( urlEncode((String)attributes.get( i ), false ) );
                 }
             }
 
@@ -983,7 +1036,7 @@
 
                     if ( filter != null )
                     {
-                        sb.append( filter );
+                        sb.append( urlEncode( filter, false ) );
                     }
 
                     if ( ( extensions.size() != 0 ) || ( criticalExtensions.size() != 0 )
)
@@ -1011,7 +1064,7 @@
 
                                 String key = ( String ) keys.next();
 
-                                sb.append( key ).append( '=' ).append( extensions.get( key
) );
+                                sb.append( urlEncode( key, false ) ).append( '=' ).append(
urlEncode( (String)extensions.get( key ), true ) );
                             }
                         }
 
@@ -1037,8 +1090,7 @@
 
                                 String key = ( String ) keys.next();
 
-                                sb.append( key ).append( '=' ).append( criticalExtensions.get(
-                                        key ) );
+                                sb.append( urlEncode( key, false ) ).append( '=' ).append(
urlEncode( (String)criticalExtensions.get( key ), true ) );
                             }
                         }
                     }



Mime
View raw message