cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-6015] Optionally URL encoding all or specific client parameter characters
Date Wed, 24 Sep 2014 13:53:23 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 22a6eba59 -> 2194c9367


[CXF-6015] Optionally URL encoding all or specific client parameter characters


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

Branch: refs/heads/master
Commit: 2194c9367be5de03dbf92ffe2bf0d46d0346314f
Parents: 22a6eba
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed Sep 24 14:53:04 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed Sep 24 14:53:04 2014 +0100

----------------------------------------------------------------------
 .../org/apache/cxf/common/util/UrlUtils.java    | 17 +++++
 .../org/apache/cxf/jaxrs/utils/HttpUtils.java   |  9 +--
 .../jaxrs/client/JAXRSClientFactoryBean.java    | 25 +++++++
 .../jaxrs/client/UrlEncodingParamConverter.java | 70 ++++++++++++++++++++
 .../org/apache/cxf/systest/jaxrs/BookStore.java | 15 +++++
 .../jaxrs/JAXRSClientServerBookTest.java        | 42 +++++++++++-
 6 files changed, 167 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/2194c936/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java b/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java
index 1e16677..e33fe8f 100644
--- a/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java
@@ -21,6 +21,7 @@ package org.apache.cxf.common.util;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.StringTokenizer;
@@ -43,6 +44,22 @@ public final class UrlUtils {
         
     }
 
+    public static String urlEncode(String value) {
+        
+        return urlEncode(value, "UTF-8");
+    }
+    
+    public static String urlEncode(String value, String enc) {
+        
+        try {
+            value = URLEncoder.encode(value, enc);
+        } catch (UnsupportedEncodingException ex) {
+            throw new RuntimeException(ex);
+        }
+        
+        return value;
+    }
+    
     /**
      * Decodes using URLDecoder - use when queries or form post values are decoded
      * @param value value to decode

http://git-wip-us.apache.org/repos/asf/cxf/blob/2194c936/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
index f76cbf2..6f9028a 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
@@ -22,7 +22,6 @@ package org.apache.cxf.jaxrs.utils;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.net.URLEncoder;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -134,13 +133,7 @@ public final class HttpUtils {
     
     public static String urlEncode(String value, String enc) {
         
-        try {
-            value = URLEncoder.encode(value, enc);
-        } catch (UnsupportedEncodingException ex) {
-            throw new RuntimeException(ex);
-        }
-        
-        return value;
+        return UrlUtils.urlEncode(value, enc);
     }
     
     public static String pathEncode(String value) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/2194c936/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
index 95b4676..5fdd141 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
@@ -18,14 +18,19 @@
  */
 package org.apache.cxf.jaxrs.client;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
 
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.PropertyUtils;
 import org.apache.cxf.common.util.ProxyHelper;
 import org.apache.cxf.configuration.security.AuthorizationPolicy;
 import org.apache.cxf.endpoint.ClientLifeCycleManager;
@@ -368,6 +373,26 @@ public class JAXRSClientFactoryBean extends AbstractJAXRSFactoryBean
{
         }
         ClientProviderFactory factory = ClientProviderFactory.createInstance(getBus()); 
         setupFactory(factory, ep);
+        
+        final Map<String, Object> theProperties = super.getProperties();
+        final boolean encodeClientParameters = PropertyUtils.isTrue(theProperties, "url.encode.client.parameters");
+        if (encodeClientParameters) {
+            final String encodeClientParametersList = theProperties == null ? null 
+                : (String)getProperties().get("url.encode.client.parameters.list");
+            factory.registerUserProvider(new ParamConverterProvider() {
+
+                @SuppressWarnings("unchecked")
+                @Override
+                public <T> ParamConverter<T> getConverter(Class<T> cls,
Type t, Annotation[] anns) {
+                    if (cls == String.class) {
+                        return (ParamConverter<T>)new UrlEncodingParamConverter(encodeClientParametersList);
+                    } else {
+                        return null;
+                    }
+                }
+            
+            });
+        }
         getBus().setProperty(ClientProviderFactory.class.getName(), factory);
         
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/2194c936/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/UrlEncodingParamConverter.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/UrlEncodingParamConverter.java
b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/UrlEncodingParamConverter.java
new file mode 100644
index 0000000..5b5bbd5
--- /dev/null
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/UrlEncodingParamConverter.java
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.client;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.ws.rs.ext.ParamConverter;
+
+import org.apache.cxf.jaxrs.utils.HttpUtils;
+
+public class UrlEncodingParamConverter implements ParamConverter<String> {
+
+    private Set<Character> encodeClientParametersList;
+    
+    public UrlEncodingParamConverter() {
+        this(null);    
+    }
+    public UrlEncodingParamConverter(String encodeClientParametersListStr) {
+        if (encodeClientParametersListStr != null) {
+            String[] chars = encodeClientParametersListStr.trim().split(" ");
+            encodeClientParametersList = new HashSet<Character>();
+            for (String ch : chars) {
+                // this may need to be tuned though this should cover URI reserved chars
+                encodeClientParametersList.add(Character.valueOf(ch.charAt(0)));
+            }
+        }
+    }
+    
+    @Override
+    public String fromString(String s) {
+        return HttpUtils.urlDecode(s);
+    }
+
+    @Override
+    public String toString(String s) {
+        if (encodeClientParametersList == null || encodeClientParametersList.isEmpty()) {
+            return HttpUtils.urlEncode(s);    
+        } else {
+            StringBuilder sb = new StringBuilder();
+            for (int i = 0; i < s.length(); i++) {
+                Character ch = s.charAt(i);
+                if (encodeClientParametersList.contains(ch)) {
+                    sb.append(HttpUtils.urlEncode(ch.toString()));
+                } else {
+                    sb.append(ch);
+                }
+            }
+            return sb.toString();
+        }
+        
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/2194c936/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index 91d93e6..af45433 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -46,6 +46,7 @@ import javax.ws.rs.Consumes;
 import javax.ws.rs.CookieParam;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.DefaultValue;
+import javax.ws.rs.Encoded;
 import javax.ws.rs.GET;
 import javax.ws.rs.HEAD;
 import javax.ws.rs.HeaderParam;
@@ -928,6 +929,20 @@ public class BookStore {
     }
     
     @GET
+    @Path("/thebooks/{bookId}/")
+    @Produces("application/xml")
+    public Book getBookWithSemicolon(@Encoded @PathParam("bookId") String id) {
+        Book b = new Book();
+        b.setId(Long.valueOf(id.substring(0, 3)));
+        b.setName("CXF in Action" + id.substring(3));
+        String absPath = ui.getAbsolutePath().toString();
+        if (absPath.contains("123;")) {
+            b.setName(b.getName() + ";");
+        }
+        return b;
+    }
+    
+    @GET
     @Path("/books/search")
     @Produces("application/xml")
     public Book getBook(@Context SearchContext searchContext) 

http://git-wip-us.apache.org/repos/asf/cxf/blob/2194c936/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 1cdc92f..e51d515 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -685,6 +685,41 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
         Book book = store.getBookWithSpace("123");
         assertEquals(123L, book.getId());
+        assertEquals("CXF in Action", book.getName());
+    }
+    
+    @Test
+    public void testBookWithSpaceProxyPathUrlEncoded() throws Exception {
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+        bean.setServiceClass(BookStore.class);
+        bean.setAddress("http://localhost:" + PORT);
+        bean.setProperties(Collections.<String, Object>singletonMap("url.encode.client.parameters",
Boolean.TRUE));
+        BookStore store = bean.create(BookStore.class);
+        Book book = store.getBookWithSemicolon("123;:");
+        assertEquals(123L, book.getId());
+        assertEquals("CXF in Action%3B%3A", book.getName());
+    }
+    
+    @Test
+    public void testBookWithSpaceProxyPathUrlEncodedSemicolonOnly() throws Exception {
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+        bean.setServiceClass(BookStore.class);
+        bean.setAddress("http://localhost:" + PORT);
+        bean.getProperties(true).put("url.encode.client.parameters", "true");
+        bean.getProperties(true).put("url.encode.client.parameters.list", ";");
+        BookStore store = bean.create(BookStore.class);
+        Book book = store.getBookWithSemicolon("123;:");
+        assertEquals(123L, book.getId());
+        assertEquals("CXF in Action%3B:", book.getName());
+    }
+    
+    @Test
+    public void testBookWithSpaceProxyNonEncodedSemicolon() throws Exception {
+        BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT,
+                                                    BookStore.class);
+        Book book = store.getBookWithSemicolon("123;");
+        assertEquals(123L, book.getId());
+        assertEquals("CXF in Action;", book.getName());
     }
     
     @Test
@@ -2139,14 +2174,15 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     
     @Test
     public void testAddBookEmptyContent() throws Exception {
-        Response r = WebClient.create("http://localhost:" + PORT + "/bookstore/books").post(null);
+        Response r = WebClient.create("http://localhost:" + PORT + "/bookstore/books")
+            .type("*/*").post(null);
         assertEquals(400, r.getStatus());
     }
     
     @Test
     public void testAddBookEmptyContentWithNullable() throws Exception {
-        Book defaultBook = 
-            WebClient.create("http://localhost:" + PORT + "/bookstore/books/null").post(null,
Book.class);
+        Book defaultBook = WebClient.create("http://localhost:" + PORT + "/bookstore/books/null")
+            .type("*/*").post(null, Book.class);
         assertEquals("Default Book", defaultBook.getName());
     }
     


Mime
View raw message