You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/03/17 04:05:27 UTC

svn commit: r924119 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java test/java/org/apache/camel/util/UnsafeCharactersEncoderTest.java

Author: ningjiang
Date: Wed Mar 17 03:05:27 2010
New Revision: 924119

URL: http://svn.apache.org/viewvc?rev=924119&view=rev
Log:
CAMEL-2554 Fixed the IndexOutOfBoundsException issue of  UnsafeUriCharactersEncoder

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/UnsafeCharactersEncoderTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java?rev=924119&r1=924118&r2=924119&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java Wed Mar 17 03:05:27 2010
@@ -60,32 +60,31 @@ public final class UnsafeUriCharactersEn
             return s;
         }
 
-        try {
-            // First check whether we actually need to encode
-            byte[] bytes = s.getBytes("UTF8");
-            for (int i = 0;;) {
-                if (unsafeCharacters.get(bytes[i])) {
+        // First check whether we actually need to encode
+        char chars[] = s.toCharArray();
+        for (int i = 0;;) {
+            // just deal with the ascii character
+            if (chars[i] > 0 && chars[i] < 128) {
+                if (unsafeCharacters.get(chars[i])) {
                     break;
                 }
-                if (++i >= bytes.length) {
-                    return s;
-                }
             }
+            if (++i >= chars.length) {
+                return s;
+            }
+        }
 
-            // okay there are some unsafe characters so we do need to encode
-            StringBuffer sb = new StringBuffer();
-            for (byte b : bytes) {
-                if (unsafeCharacters.get(b)) {
-                    appendEscape(sb, b);
-                } else {
-                    sb.append((char)b);
-                }
+        // okay there are some unsafe characters so we do need to encode
+        StringBuffer sb = new StringBuffer();
+        for (char ch : chars) {
+            if (ch > 0 && ch < 128 && unsafeCharacters.get(ch)) {
+                appendEscape(sb, (byte)ch);
+            } else {
+                sb.append(ch);
             }
-            return sb.toString();
-        } catch (UnsupportedEncodingException e) {
-            LOG.error("Can't encoding the uri: ", e);
-            return null;
         }
+        return sb.toString();
+
     }
 
     private static void appendEscape(StringBuffer sb, byte b) {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/UnsafeCharactersEncoderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/UnsafeCharactersEncoderTest.java?rev=924119&r1=924118&r2=924119&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/UnsafeCharactersEncoderTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/UnsafeCharactersEncoderTest.java Wed Mar 17 03:05:27 2010
@@ -19,17 +19,24 @@ package org.apache.camel.util;
 import junit.framework.TestCase;
 
 public class UnsafeCharactersEncoderTest extends TestCase {
+    private void testEncoding(String before, String after) {
+        String result = UnsafeUriCharactersEncoder.encode(before);
+        assertEquals("Get the wrong encoding result", result, after);
+    }
+    
     public void testQnameEncoder() {
         String afterEncoding = "%7Bhttp://www.example.com/test%7DServiceName";
         String beforeEncoding = "{http://www.example.com/test}ServiceName";
-
-        String result = UnsafeUriCharactersEncoder.encode(beforeEncoding);
-        assertEquals("Get the wrong encoding result", result, afterEncoding);
+        testEncoding(beforeEncoding, afterEncoding);
     }
 
     public void testNoEncoding() {
         String noEncoding = "http://www.example.com";
-        String result = UnsafeUriCharactersEncoder.encode(noEncoding);
-        assertEquals("Get the wrong encoding result", result, noEncoding);
+        testEncoding(noEncoding, noEncoding);
+    }
+    
+    public void testUnicodes() {
+        String noEncoding = "http://test.com/\uFD04";
+        testEncoding(noEncoding, noEncoding);
     }
 }