Author: dkulp
Date: Thu Oct 20 15:52:52 2011
New Revision: 1186866
URL: http://svn.apache.org/viewvc?rev=1186866&view=rev
Log:
[CXF-3855] Fix issue with uri escaping/encoding
Modified:
cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/URIParserUtil.java
cxf/trunk/tools/common/src/test/java/org/apache/cxf/tools/util/URIParserUtilTest.java
Modified:
cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/URIParserUtil.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/URIParserUtil.java?rev=1186866&r1=1186865&r2=1186866&view=diff
==============================================================================
---
cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/URIParserUtil.java
(original)
+++
cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/URIParserUtil.java
Thu Oct 20 15:52:52 2011
@@ -227,24 +227,25 @@ public final class URIParserUtil {
}
public static String escapeChars(String s) {
- StringBuilder b = new StringBuilder(s);
- int x = 0;
- do {
- char ch = b.charAt(x);
+ StringBuilder b = new StringBuilder(s.length());
+
+ for (int x = 0; x < s.length(); x++) {
+ char ch = s.charAt(x);
if (isExcluded(ch)) {
try {
byte[] bytes = Character.toString(ch).getBytes("UTF-8");
- b.setCharAt(x++, '%');
for (int y = 0; y < bytes.length; y++) {
- b.insert(x++, HEX_DIGITS.charAt((bytes[y] & 0xFF) >>
4));
- b.insert(x, HEX_DIGITS.charAt(bytes[y] & 0x0F));
+ b.append("%");
+ b.append(HEX_DIGITS.charAt((bytes[y] & 0xFF) >> 4));
+ b.append(HEX_DIGITS.charAt(bytes[y] & 0x0F));
}
} catch (UnsupportedEncodingException e) {
//should not happen
}
+ } else {
+ b.append(ch);
}
- x++;
- } while (x < b.length());
+ }
return b.toString();
}
public static String normalize(final String uri) {
Modified:
cxf/trunk/tools/common/src/test/java/org/apache/cxf/tools/util/URIParserUtilTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/tools/common/src/test/java/org/apache/cxf/tools/util/URIParserUtilTest.java?rev=1186866&r1=1186865&r2=1186866&view=diff
==============================================================================
---
cxf/trunk/tools/common/src/test/java/org/apache/cxf/tools/util/URIParserUtilTest.java
(original)
+++
cxf/trunk/tools/common/src/test/java/org/apache/cxf/tools/util/URIParserUtilTest.java
Thu Oct 20 15:52:52 2011
@@ -19,6 +19,9 @@
package org.apache.cxf.tools.util;
+import java.io.File;
+import java.net.URI;
+
import org.junit.Assert;
import org.junit.Test;
@@ -93,4 +96,11 @@ public class URIParserUtilTest extends A
uri = "file:/home/john/test/all/../../alltest";
assertEquals("file:/home/john/alltest",
URIParserUtil.getAbsoluteURI(uri));
}
+ @Test
+ public void testCXF3855() throws Exception {
+ String orig = new String(new byte[] {-47, -122}, "UTF-8");
+ orig = "/foo" + orig + ".txt";
+ String s = URIParserUtil.escapeChars(orig);
+ assertEquals(orig, new File(new URI("file:" + s)).getAbsolutePath());
+ }
}