You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2015/04/23 13:23:42 UTC

cxf git commit: [CXF-6366] Encoding forward slashes for matrix parameters

Repository: cxf
Updated Branches:
  refs/heads/master 982bdbc9d -> 78a46c1b0


[CXF-6366] Encoding forward slashes for matrix parameters


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/78a46c1b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/78a46c1b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/78a46c1b

Branch: refs/heads/master
Commit: 78a46c1b0284c2f074385c0932801a149fd0d591
Parents: 982bdbc
Author: Sergey Beryozkin <sb...@talend.com>
Authored: Thu Apr 23 12:23:20 2015 +0100
Committer: Sergey Beryozkin <sb...@talend.com>
Committed: Thu Apr 23 12:23:20 2015 +0100

----------------------------------------------------------------------
 .../apache/cxf/jaxrs/impl/UriBuilderImpl.java   | 23 +++++++++++++-------
 .../cxf/jaxrs/impl/UriBuilderImplTest.java      |  7 ++++++
 2 files changed, 22 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/78a46c1b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
index 84065b1..ae10a1b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
@@ -689,9 +689,9 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
         }
         List<String> list = matrix.get(name);
         if (list == null) {
-            matrix.put(name, toStringList(values));
+            matrix.put(name, toStringList(true, values));
         } else {
-            list.addAll(toStringList(values));
+            list.addAll(toStringList(true, values));
         }
         return this;
     }
@@ -703,9 +703,9 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
         }
         List<String> list = query.get(name);
         if (list == null) {
-            query.put(name, toStringList(values));
+            query.put(name, toStringList(false, values));
         } else {
-            list.addAll(toStringList(values));
+            list.addAll(toStringList(false, values));
         }
         return this;
     }
@@ -723,7 +723,7 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
             throw new IllegalArgumentException("name is null");
         }
         if (values != null && values.length >= 1 && values[0] != null) {
-            matrix.put(name, toStringList(values));
+            matrix.put(name, toStringList(true, values));
         } else {
             matrix.remove(name);
         }
@@ -779,7 +779,7 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
             throw new IllegalArgumentException("name is null");
         }
         if (values != null && values.length >= 1 && values[0] != null) {
-            query.put(name, toStringList(values));
+            query.put(name, toStringList(false, values));
         } else {
             query.remove(name);
         }
@@ -805,7 +805,7 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
      * @return list of strings
      * @throws IllegalArgumentException when one of values is null
      */
-    private List<String> toStringList(Object... values) throws IllegalArgumentException {
+    private List<String> toStringList(boolean encodeSlash, Object... values) throws IllegalArgumentException {
         List<String> list = new ArrayList<String>();
         if (values != null) {
             for (int i = 0; i < values.length; i++) {
@@ -813,7 +813,11 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
                 if (value == null) {
                     throw new IllegalArgumentException("Null value on " + i + " position");
                 }
-                list.add(value.toString());
+                String strValue = value.toString();
+                if (encodeSlash) {
+                    strValue = strValue.replaceAll("/", "%2F");
+                }
+                list.add(strValue);
             }
         }
         if (list.isEmpty()) {
@@ -840,6 +844,9 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
                 boolean templateValue = val.startsWith("{") && val.endsWith("}");
                 if (!templateValue) { 
                     val = HttpUtils.encodePartiallyEncoded(val, isQuery);
+                    if (!isQuery) {
+                        val = val.replaceAll("/", "%2F");
+                    }
                 } else {
                     val = new URITemplate(val).encodeLiteralCharacters(isQuery);
                 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/78a46c1b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
index 3abe884..a9ec843 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
@@ -480,6 +480,13 @@ public class UriBuilderImplTest extends Assert {
     }
     
     @Test
+    public void testMatrixWithSlash() throws Exception {
+        URI uri = new URI("http://bar/foo");
+        URI newUri = new UriBuilderImpl(uri).matrixParam("q", "1/2").build();   
+        assertEquals("URI is not built correctly", "http://bar/foo;q=1%2F2", newUri.toString());
+    }
+    
+    @Test
     public void replaceMatrixParamWithEmptyPathTest() throws Exception {
         String name = "name";
         String expected = "http://localhost:8080;name=x;name=y;name=y%20x;name=x%25y;name=%20";