cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6307] Updating PrimitiveTextProvier with text/plain, introducing StringTextProvider
Date Wed, 06 May 2015 14:53:59 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 964e43ca2 -> c7db05bc5


[CXF-6307] Updating PrimitiveTextProvier with text/plain, introducing StringTextProvider


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

Branch: refs/heads/master
Commit: c7db05bc509942f266c494c692d8331f8fdf8ada
Parents: 964e43c
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed May 6 15:53:31 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed May 6 15:53:31 2015 +0100

----------------------------------------------------------------------
 .../jaxrs/provider/PrimitiveTextProvider.java   | 31 +++-----
 .../cxf/jaxrs/provider/ProviderFactory.java     | 12 +--
 .../cxf/jaxrs/provider/StringTextProvider.java  | 78 ++++++++++++++++++++
 .../apache/cxf/jaxrs/utils/InjectionUtils.java  |  8 +-
 .../provider/PrimitiveTextProviderTest.java     |  4 +-
 .../cxf/jaxrs/provider/ProviderFactoryTest.java | 19 ++++-
 .../cxf/systest/jaxrs/JAXRSMultipartTest.java   |  4 +-
 .../cxf/systest/jaxrs/MultipartStore.java       |  4 +-
 .../cxf/systest/jaxrs/StringTextWriter.java     |  4 +-
 9 files changed, 125 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
index 21fe81a..31721b0 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
@@ -24,6 +24,8 @@ import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import javax.ws.rs.Consumes;
+import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyReader;
@@ -35,13 +37,14 @@ import org.apache.cxf.jaxrs.model.ParameterType;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 
+@Consumes("text/plain")
+@Produces("text/plain")
 public class PrimitiveTextProvider<T> extends AbstractConfigurableProvider
     implements MessageBodyReader<T>, MessageBodyWriter<T> {
-    private int bufferSize = IOUtils.DEFAULT_BUFFER_SIZE;
     
     private static boolean isSupported(Class<?> type, MediaType mt) { 
-        boolean isSupported = InjectionUtils.isPrimitive(type);
-        return isSupported && (String.class == type || mt.isCompatible(MediaType.TEXT_PLAIN_TYPE));
+        boolean isPrimitive = InjectionUtils.isPrimitiveOnly(type);
+        return isPrimitive && mt.isCompatible(MediaType.TEXT_PLAIN_TYPE);
     }
     
     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
@@ -51,7 +54,7 @@ public class PrimitiveTextProvider<T> extends AbstractConfigurableProvider
     public T readFrom(Class<T> type, Type genType, Annotation[] anns, MediaType mt,

                       MultivaluedMap<String, String> headers, InputStream is) throws
IOException {
         String string = IOUtils.toString(is, HttpUtils.getEncoding(mt, "UTF-8"));
-        if (String.class != type && StringUtils.isEmpty(string)) {
+        if (StringUtils.isEmpty(string)) {
             reportEmptyContentLength();
         }
         if (type == Character.class) {
@@ -80,23 +83,9 @@ public class PrimitiveTextProvider<T> extends AbstractConfigurableProvider
                         MediaType mt, MultivaluedMap<String, Object> headers,
                         OutputStream os) throws IOException {
         String encoding = HttpUtils.getSetEncoding(mt, headers, "UTF-8");
-        //REVISIT try to avoid instantiating the whole byte array
         byte[] bytes = obj.toString().getBytes(encoding);
-        if (bytes.length > bufferSize) {
-            int pos = 0;
-            while (pos < bytes.length) {
-                int bl = bytes.length - pos;
-                if (bl > bufferSize) {
-                    bl = bufferSize;
-                }
-                os.write(bytes, pos, bl);
-                pos += bl;
-            }
-        } else {
-            os.write(bytes);
-        }
-    }
-    public void setBufferSize(int bufferSize) {
-        this.bufferSize = bufferSize;
+        os.write(bytes);
+        
     }
+    
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index 72cff1c..b59d7e3 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -138,6 +138,7 @@ public abstract class ProviderFactory {
                                     new SourceProvider<Object>(),
                                     new DataSourceProvider<Object>(),
                                     new FormEncodingProvider<Object>(),
+                                    new StringTextProvider(),
                                     new PrimitiveTextProvider<Object>(),
                                     createProvider(JAXB_PROVIDER_NAME),
                                     createProvider(JSON_PROVIDER_NAME),
@@ -760,17 +761,16 @@ public abstract class ProviderFactory {
             MessageBodyReader<?> e1 = p1.getProvider();
             MessageBodyReader<?> e2 = p2.getProvider();
             
-            int result = compareClasses(e1, e2);
-            if (result != 0) {
-                return result;
-            }
-            
             List<MediaType> types1 = JAXRSUtils.getProviderConsumeTypes(e1);
             types1 = JAXRSUtils.sortMediaTypes(types1, null);
             List<MediaType> types2 = JAXRSUtils.getProviderConsumeTypes(e2);
             types2 = JAXRSUtils.sortMediaTypes(types2, null);
     
-            return JAXRSUtils.compareSortedMediaTypes(types1, types2, null);
+            int result = JAXRSUtils.compareSortedMediaTypes(types1, types2, null);
+            if (result != 0) {
+                return result;
+            }
+            return compareClasses(e1, e2);
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringTextProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringTextProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringTextProvider.java
new file mode 100644
index 0000000..e6e25da
--- /dev/null
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringTextProvider.java
@@ -0,0 +1,78 @@
+/**
+ * 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.provider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
+
+public class StringTextProvider extends AbstractConfigurableProvider
+    implements MessageBodyReader<String>, MessageBodyWriter<String> {
+    private int bufferSize = IOUtils.DEFAULT_BUFFER_SIZE;
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
+        return String.class == type;
+    }
+
+    public String readFrom(Class<String> type, Type genType, Annotation[] anns, MediaType
mt, 
+                      MultivaluedMap<String, String> headers, InputStream is) throws
IOException {
+        return IOUtils.toString(is, HttpUtils.getEncoding(mt, "UTF-8"));
+    }
+
+    public long getSize(String t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
+        return -1;
+    }
+
+    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
+        return String.class == type;
+    }
+
+    public void writeTo(String obj, Class<?> type, Type genType, Annotation[] anns,

+                        MediaType mt, MultivaluedMap<String, Object> headers,
+                        OutputStream os) throws IOException {
+        String encoding = HttpUtils.getSetEncoding(mt, headers, "UTF-8");
+        //REVISIT try to avoid instantiating the whole byte array
+        byte[] bytes = obj.getBytes(encoding);
+        if (bytes.length > bufferSize) {
+            int pos = 0;
+            while (pos < bytes.length) {
+                int bl = bytes.length - pos;
+                if (bl > bufferSize) {
+                    bl = bufferSize;
+                }
+                os.write(bytes, pos, bl);
+                pos += bl;
+            }
+        } else {
+            os.write(bytes);
+        }
+    }
+    public void setBufferSize(int bufferSize) {
+        this.bufferSize = bufferSize;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
index 2b6e7e4..2d95659 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
@@ -1252,13 +1252,15 @@ public final class InjectionUtils {
         }
         return false;
     }
-    
     public static boolean isPrimitive(Class<?> type) {
+        return String.class == type  
+            || isPrimitiveOnly(type);
+    }
+    public static boolean isPrimitiveOnly(Class<?> type) {
         return type.isPrimitive() 
             || Number.class.isAssignableFrom(type)
             || Boolean.class == type
-            || Character.class == type
-            || String.class == type;
+            || Character.class == type;
     }
     
     public static String decodeValue(String value, boolean decode, ParameterType param) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
index 54428d1..95fb77f 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProviderTest.java
@@ -76,7 +76,7 @@ public class PrimitiveTextProviderTest extends Assert {
     @Test
     public void testStringIsWriteable() {
         MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
-        assertTrue(p.isWriteable(String.class, null, null, MediaType.TEXT_PLAIN_TYPE)
+        assertFalse(p.isWriteable(String.class, null, null, MediaType.TEXT_PLAIN_TYPE)
                    && p.isWriteable(String.class, null, null, MediaType.valueOf("text/custom")));
     }
     
@@ -113,7 +113,7 @@ public class PrimitiveTextProviderTest extends Assert {
     @Test
     public void testStringIsReadable() {
         MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
-        assertTrue(p.isReadable(String.class, null, null, MediaType.TEXT_PLAIN_TYPE)
+        assertFalse(p.isReadable(String.class, null, null, MediaType.TEXT_PLAIN_TYPE)
                    && p.isReadable(String.class, null, null, MediaType.valueOf("text/custom")));
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
index 1268538..640f537 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
@@ -225,6 +225,21 @@ public class ProviderFactoryTest extends Assert {
     }
     
     @Test
+    public void testMessageBodyReaderString() throws Exception {
+        ProviderFactory pf = ServerProviderFactory.getInstance();
+        MessageBodyReader<String> mbr = pf.createMessageBodyReader(String.class, String.class,
new Annotation[]{}, 
+                                   MediaType.APPLICATION_XML_TYPE, new MessageImpl());
+        assertTrue(mbr instanceof StringTextProvider);
+    }
+    @Test
+    public void testMessageBodyWriterString() throws Exception {
+        ProviderFactory pf = ServerProviderFactory.getInstance();
+        MessageBodyWriter<String> mbr = pf.createMessageBodyWriter(String.class, String.class,
new Annotation[]{}, 
+                                   MediaType.APPLICATION_XML_TYPE, new MessageImpl());
+        assertTrue(mbr instanceof StringTextProvider);
+    }
+    
+    @Test
     public void testMessageBodyHandlerHierarchy() throws Exception {
         ProviderFactory pf = ServerProviderFactory.getInstance();
         List<Object> providers = new ArrayList<Object>();
@@ -291,7 +306,7 @@ public class ProviderFactoryTest extends Assert {
     
     @Test
     public void testGetStringProvider() throws Exception {
-        verifyProvider(String.class, PrimitiveTextProvider.class, "text/plain");
+        verifyProvider(String.class, StringTextProvider.class, "text/plain");
     }
     
     @Test
@@ -353,7 +368,7 @@ public class ProviderFactoryTest extends Assert {
        
     @Test
     public void testGetStringProviderWildCard() throws Exception {
-        verifyProvider(String.class, PrimitiveTextProvider.class, "text/*");
+        verifyProvider(String.class, StringTextProvider.class, "text/*");
     }
     
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
index e89a3ea..16dfeaf 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
@@ -374,7 +374,9 @@ public class JAXRSMultipartTest extends AbstractBusClientServerTestBase
{
     public void testUseProxyToAddBookAndSimpleParts() throws Exception {
         MultipartStore store = 
             JAXRSClientFactory.create("http://localhost:" + PORT, MultipartStore.class);
-        Book b = store.testAddBookAndSimpleParts(new Book("CXF in Action", 124L), "1", "2");
+        HTTPConduit conduit = WebClient.getConfig(store).getHttpConduit();
+        conduit.getClient().setReceiveTimeout(1000000);
+        Book b = store.testAddBookAndSimpleParts(new Book("CXF in Action", 124L), "1", 2);
         assertEquals(124L, b.getId());
         assertEquals("CXF in Action - 12", b.getName());
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
index 9924810..d0c4288 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
@@ -459,8 +459,8 @@ public class MultipartStore {
     public Book testAddBookAndSimpleParts(
         @Multipart(value = "rootPart", type = "text/xml") Book b1,
         @Multipart(value = "simplePart1") String simplePart1,
-        @Multipart(value = "simplePart2") String simplePart2) throws Exception {
-        return new Book(b1.getName() + " - " + simplePart1 + simplePart2, b1.getId());  

+        @Multipart(value = "simplePart2") Integer simplePart2) throws Exception {
+        return new Book(b1.getName() + " - " + simplePart1 + simplePart2.toString(), b1.getId());
  
     }
     
     @POST

http://git-wip-us.apache.org/repos/asf/cxf/blob/c7db05bc/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
index 2319511..220643e 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
@@ -30,9 +30,9 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
-import org.apache.cxf.jaxrs.provider.PrimitiveTextProvider;
+import org.apache.cxf.jaxrs.provider.StringTextProvider;
 
-public class StringTextWriter extends PrimitiveTextProvider<String> {
+public class StringTextWriter extends StringTextProvider {
     
     @Context
     private UriInfo ui;


Mime
View raw message