cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1125323 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/provider/
Date Fri, 20 May 2011 11:05:21 GMT
Author: sergeyb
Date: Fri May 20 11:05:21 2011
New Revision: 1125323

URL: http://svn.apache.org/viewvc?rev=1125323&view=rev
Log:
[CXF-3540] Fixing PrimitiveTextProvider to check a charset parameter

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java?rev=1125323&r1=1125322&r2=1125323&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
Fri May 20 11:05:21 2011
@@ -49,7 +49,7 @@ public class PrimitiveTextProvider 
                            MultivaluedMap<String, String> headers, InputStream is)
throws IOException {
         
         return InjectionUtils.handleParameter(
-                    IOUtils.readStringFromStream(is), 
+                    IOUtils.toString(is, HttpUtils.getEncoding(mt, "UTF-8")), 
                     false,
                     type,
                     anns,

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java?rev=1125323&r1=1125322&r2=1125323&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java Fri
May 20 11:05:21 2011
@@ -65,17 +65,12 @@ public final class FormUtils {
         try {
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             IOUtils.copy(is, bos, 1024);
-            return new String(bos.toByteArray(), getCharset(mt));
+            return new String(bos.toByteArray(), HttpUtils.getEncoding(mt, "UTF-8"));
         } catch (Exception ex) {
             throw new WebApplicationException(ex);
         }
     }
     
-    private static String getCharset(MediaType mt) {
-        String charset = mt == null ? "UTF-8" : mt.getParameters().get("charset");
-        return charset == null ? "UTF-8" : charset;
-    }
-    
     public static void populateMapFromString(MultivaluedMap<String, String> params,

                                              String postBody, boolean decode,
                                              HttpServletRequest request) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=1125323&r1=1125322&r2=1125323&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java Fri
May 20 11:05:21 2011
@@ -319,4 +319,9 @@ public final class HttpUtils {
         }
         return defaultEncoding;
     }
+    
+    public static String getEncoding(MediaType mt, String defaultEncoding) {
+        String charset = mt == null ? "UTF-8" : mt.getParameters().get("charset");
+        return charset == null ? "UTF-8" : charset;
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java?rev=1125323&r1=1125322&r2=1125323&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
Fri May 20 11:05:21 2011
@@ -248,6 +248,21 @@ public class FormEncodingProviderTest ex
         assertEquals(helloStringISO88591, "name=" + value);
     }
 
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testReadChineeseChars() throws Exception {
+        
+        String s = "name=中文";
+        
+        MultivaluedMap<String, String> mvMap = 
+            (MultivaluedMap<String, String>)ferp.readFrom((Class)MultivaluedMap.class,
null,
+                new Annotation[]{}, 
+                MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED + ";charset=UTF-8"),
null, 
+                new ByteArrayInputStream(s.getBytes("UTF-8")));
+        String value = mvMap.getFirst("name");
+        assertEquals(s, "name=" + value);
+    }
+    
     @Test
     public void testReadableMap() {
         assertTrue(ferp.isReadable(MultivaluedMap.class, null, null, null));

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1125323&r1=1125322&r2=1125323&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Fri May 20 11:05:21 2011
@@ -88,6 +88,40 @@ import org.junit.Test;
 
 public class JAXBElementProviderTest extends Assert {
 
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testReadFromISO() throws Exception {
+        
+        String eWithAcute = "\u00E9";
+        String nameStringUTF16 = "F" + eWithAcute + "lix";
+        String bookStringUTF16 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
+                                 + "<Book><name>" + nameStringUTF16 + "</name></Book>";
+        
+        byte[] iso88591bytes = bookStringUTF16.getBytes("ISO-8859-1");
+        
+        JAXBElementProvider p = new JAXBElementProvider();
+        Book book = (Book)p.readFrom((Class)Book.class, null,
+                new Annotation[]{}, 
+                MediaType.valueOf(MediaType.APPLICATION_XML), null, 
+                new ByteArrayInputStream(iso88591bytes));
+        assertEquals(book.getName(), nameStringUTF16);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testReadChineeseChars() throws Exception {
+        
+        String nameStringUTF16 = "中文";
+        
+        String bookStringUTF16 = "<Book><name>" + nameStringUTF16 + "</name></Book>";
+        JAXBElementProvider p = new JAXBElementProvider();
+        Book book = (Book)p.readFrom((Class)Book.class, null,
+                new Annotation[]{}, 
+                MediaType.valueOf(MediaType.APPLICATION_XML + ";charset=UTF-8"), null, 
+                new ByteArrayInputStream(bookStringUTF16.getBytes("UTF-8")));
+        assertEquals(book.getName(), nameStringUTF16);
+    }
+    
     @Test
     public void testSingleJAXBContext() throws Exception {
         ClassResourceInfo cri = 

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java?rev=1125323&r1=1125322&r2=1125323&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
Fri May 20 11:05:21 2011
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxrs.provider;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.lang.annotation.Annotation;
 import java.util.Arrays;
 
 import javax.ws.rs.core.MediaType;
@@ -116,10 +117,20 @@ public class PrimitiveTextProviderTest e
         byte[] iso88591bytes = helloStringUTF16.getBytes("ISO-8859-1");
         String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
         
-        System.out.println(helloStringISO88591);
-        
         assertEquals(helloStringISO88591, os.toString("ISO-8859-1")); 
     }
     
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testReadChineeseChars() throws Exception {
+        String s = "中文";
+        
+        MessageBodyReader p = new PrimitiveTextProvider();
         
+        String value = (String)p.readFrom((Class)String.class, null,
+                new Annotation[]{}, 
+                MediaType.valueOf(MediaType.APPLICATION_XML + ";charset=UTF-8"), null, 
+                new ByteArrayInputStream(s.getBytes("UTF-8")));
+        assertEquals(value, value);
+    }    
 }



Mime
View raw message