cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1163607 - in /cxf/branches/2.4.x-fixes: ./ common/common/src/main/java/org/apache/cxf/common/util/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs...
Date Wed, 31 Aug 2011 12:41:50 GMT
Author: sergeyb
Date: Wed Aug 31 12:41:50 2011
New Revision: 1163607

URL: http://svn.apache.org/viewvc?rev=1163607&view=rev
Log:
Merged revisions 1163595 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1163595 | sergeyb | 2011-08-31 13:10:31 +0100 (Wed, 31 Aug 2011) | 1 line
  
  [CXF-3764] Proper support for form encodings, thanks to Marc Giger
........

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Aug 31 12:41:50 2011
@@ -1 +1 @@
-/cxf/trunk:1163593,1163603
+/cxf/trunk:1163593,1163595,1163603

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java?rev=1163607&r1=1163606&r2=1163607&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java
(original)
+++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java
Wed Aug 31 12:41:50 2011
@@ -43,17 +43,22 @@ public final class UrlUtils {
     /**
      * Decodes using URLDecoder - use when queries or form post values are decoded
      * @param value value to decode
+     * @param enc encoding
      * @return
      */
-    public static String urlDecode(String value) {
+    public static String urlDecode(String value, String enc) {
         try {
-            value = URLDecoder.decode(value, "UTF-8");
+            value = URLDecoder.decode(value, enc);
         } catch (UnsupportedEncodingException e) {
             LOG.warning("UTF-8 encoding can not be used to decode " + value);          
         }
         return value;
     }
     
+    public static String urlDecode(String value) {
+        return urlDecode(value, "UTF-8");
+    }
+    
     /**
      * URL path segments may contain '+' symbols which should not be decoded into ' '
      * This method replaces '+' with %2B and delegates to URLDecoder

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java?rev=1163607&r1=1163606&r2=1163607&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
Wed Aug 31 12:41:50 2011
@@ -141,8 +141,10 @@ public class FormEncodingProvider implem
                 AttachmentUtils.getMultipartBody(mc, attachmentDir, attachmentThreshold);
             FormUtils.populateMapFromMultipart(params, body, decode);
         } else {
+            String enc = HttpUtils.getEncoding(mt, "UTF-8");
             FormUtils.populateMapFromString(params, 
-                                            FormUtils.readBody(is, mt), 
+                                            FormUtils.readBody(is, enc),
+                                            enc,
                                             decode,
                                             mc != null ? mc.getHttpServletRequest() : null);
         }

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java?rev=1163607&r1=1163606&r2=1163607&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
Wed Aug 31 12:41:50 2011
@@ -29,7 +29,6 @@ import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.common.util.StringUtils;
@@ -61,28 +60,30 @@ public final class FormUtils {
         }
     }
     
-    public static String readBody(InputStream is, MediaType mt) {
+    public static String readBody(InputStream is, String encoding) {
         try {
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             IOUtils.copy(is, bos, 1024);
-            return new String(bos.toByteArray(), HttpUtils.getEncoding(mt, "UTF-8"));
+            return new String(bos.toByteArray(), encoding);
         } catch (Exception ex) {
             throw new WebApplicationException(ex);
         }
     }
     
     public static void populateMapFromString(MultivaluedMap<String, String> params,

-                                             String postBody, boolean decode,
+                                             String postBody, 
+                                             String enc,
+                                             boolean decode,
                                              HttpServletRequest request) {
         if (!StringUtils.isEmpty(postBody)) {
             List<String> parts = Arrays.asList(postBody.split("&"));
             for (String part : parts) {
                 String[] keyValue = part.split("=");
                 // Change to add blank string if key but not value is specified
-                String name = HttpUtils.urlDecode(keyValue[0]);
+                String name = HttpUtils.urlDecode(keyValue[0], enc);
                 if (keyValue.length == 2) {
                     if (decode) {
-                        params.add(name, HttpUtils.urlDecode(keyValue[1]));
+                        params.add(name, HttpUtils.urlDecode(keyValue[1], enc));
                     } else {
                         params.add(name, keyValue[1]);
                     }

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=1163607&r1=1163606&r2=1163607&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
Wed Aug 31 12:41:50 2011
@@ -68,6 +68,10 @@ public final class HttpUtils {
     private HttpUtils() {
     }
     
+    public static String urlDecode(String value, String enc) {
+        return UrlUtils.urlDecode(value, enc);
+    }
+    
     public static String urlDecode(String value) {
         return UrlUtils.urlDecode(value);
     }

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1163607&r1=1163606&r2=1163607&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
Wed Aug 31 12:41:50 2011
@@ -733,9 +733,10 @@ public final class JAXRSUtils {
             m.put(FormUtils.FORM_PARAM_MAP, params);
         
             if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE))
{
-                String body = FormUtils.readBody(m.getContent(InputStream.class), mt);
+                String enc = HttpUtils.getEncoding(mt, "UTF-8");
+                String body = FormUtils.readBody(m.getContent(InputStream.class), enc);
                 HttpServletRequest request = (HttpServletRequest)m.get(AbstractHTTPDestination.HTTP_REQUEST);
-                FormUtils.populateMapFromString(params, (String)body, decode, request);
+                FormUtils.populateMapFromString(params, (String)body, enc, decode, request);
             } else {
                 if (mt != null && "multipart".equalsIgnoreCase(mt.getType()) 
                     && MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mt)) {

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java?rev=1163607&r1=1163606&r2=1163607&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
Wed Aug 31 12:41:50 2011
@@ -22,6 +22,7 @@ package org.apache.cxf.jaxrs.utils;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.lang.reflect.Method;
+import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -86,7 +87,6 @@ import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 import org.easymock.EasyMock;
-
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -96,7 +96,43 @@ public class JAXRSUtilsTest extends Asse
     @Before
     public void setUp() {
     }
+    
+    @Test
+    public void testFormParametersUTF8Encoding() throws Exception {
+        doTestFormParamsWithEncoding("UTF-8", true);
+        doTestFormParamsWithEncoding("UTF-8", false);
+    }
+    
+    @Test
+    public void testFormParametersISO88591Encoding() throws Exception {
+        doTestFormParamsWithEncoding("ISO-8859-1", true);
+    }
 
+    private void doTestFormParamsWithEncoding(String enc, boolean setEnc) throws Exception
{
+        Class[] argType = {String.class, List.class};
+        Method m = Customer.class.getMethod("testFormParam", argType);
+        MessageImpl messageImpl = new MessageImpl();
+        String body = "p1=" + URLEncoder.encode("\u00E4\u00F6\u00FC", enc) + "&p2=2&p2=3";
+        messageImpl.put(Message.REQUEST_URI, "/foo");
+        MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
+        String ct = MediaType.APPLICATION_FORM_URLENCODED;
+        if (setEnc) {
+            ct += ";charset=" + enc;
+        }
+        headers.putSingle("Content-Type", ct);
+        messageImpl.put(Message.PROTOCOL_HEADERS, headers);
+        messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
+        List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
null),
+                                                           null, messageImpl);
+        assertEquals("2 form params should've been identified", 2, params.size());
+        assertEquals("First Form Parameter not matched correctly",
+                     "\u00E4\u00F6\u00FC", params.get(0));
+        List<String> list = (List<String>)params.get(1);
+        assertEquals(2, list.size());
+        assertEquals("2", list.get(0));
+        assertEquals("3", list.get(1));
+    }
+    
     @Test
     public void testSelectBetweenMultipleResourceClasses() throws Exception {
         JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();



Mime
View raw message