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-6320] 400 should be returned when a primitive MBR gets an empty payload
Date Wed, 01 Apr 2015 15:35:49 GMT
Repository: cxf
Updated Branches:
  refs/heads/master cf7f58139 -> 02a35f0bf


[CXF-6320] 400 should be returned when a primitive MBR gets an empty payload


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

Branch: refs/heads/master
Commit: 02a35f0bf624ec6230ee85720b49661e03520c18
Parents: cf7f581
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed Apr 1 16:35:31 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed Apr 1 16:35:31 2015 +0100

----------------------------------------------------------------------
 .../provider/AbstractConfigurableProvider.java  | 14 ++-
 .../jaxrs/provider/AbstractJAXBProvider.java    | 14 ---
 .../jaxrs/provider/PrimitiveTextProvider.java   | 16 ++--
 .../apache/cxf/jaxrs/utils/InjectionUtils.java  |  4 +-
 .../provider/PrimitiveTextProviderTest.java     | 97 ++++++++++++++++++--
 .../cxf/systest/jaxrs/JAXRSSoapBookTest.java    |  7 +-
 6 files changed, 117 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/02a35f0b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
index b24c647..ae201a6 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
@@ -20,16 +20,23 @@
 package org.apache.cxf.jaxrs.provider;
 
 import java.util.List;
+import java.util.ResourceBundle;
+import java.util.logging.Logger;
 
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.NoContentException;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
+import org.apache.cxf.common.i18n.BundleUtils;
+import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 
 public abstract class AbstractConfigurableProvider {
-
+    protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractJAXBProvider.class);
+    protected static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
+    
     private List<String> consumeMediaTypes;
     private List<String> produceMediaTypes;
     private boolean enableBuffering;
@@ -170,4 +177,9 @@ public abstract class AbstractConfigurableProvider {
         
         return false;
     }
+    protected void reportEmptyContentLength() throws NoContentException {
+        String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
+        LOG.warning(message);
+        throw new NoContentException(message);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/02a35f0b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
index 8105259..b456916 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
@@ -33,14 +33,11 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.ResourceBundle;
 import java.util.Set;
-import java.util.logging.Logger;
 
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.NoContentException;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ContextResolver;
 import javax.ws.rs.ext.MessageBodyReader;
@@ -67,9 +64,7 @@ import org.w3c.dom.Element;
 import org.xml.sax.helpers.DefaultHandler;
 
 import org.apache.cxf.annotations.SchemaValidation;
-import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.jaxb.JAXBUtils;
-import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
@@ -90,9 +85,6 @@ import org.apache.cxf.staxutils.transform.TransformUtils;
 public abstract class AbstractJAXBProvider<T> extends AbstractConfigurableProvider
     implements MessageBodyReader<T>, MessageBodyWriter<T> {
     
-    protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractJAXBProvider.class);
-
-    protected static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
     protected static final String NS_MAPPER_PROPERTY_RI = "com.sun.xml.bind.namespacePrefixMapper";
     protected static final String NS_MAPPER_PROPERTY_RI_INT = "com.sun.xml.internal.bind.namespacePrefixMapper";
     private static final String JAXB_DEFAULT_NAMESPACE = "##default";
@@ -269,12 +261,6 @@ public abstract class AbstractJAXBProvider<T> extends AbstractConfigurableProvid
         jaxbElementClassMap = map;
     }
     
-    protected void reportEmptyContentLength() throws NoContentException {
-        String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
-        LOG.warning(message);
-        throw new NoContentException(message);
-    }
-    
     protected <X> X getStreamHandlerFromCurrentMessage(Class<X> staxCls) {
         Message m = PhaseInterceptorChain.getCurrentMessage();
         if (m != null) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/02a35f0b/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 93a6135..21fe81a 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
@@ -35,23 +35,27 @@ import org.apache.cxf.jaxrs.model.ParameterType;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 
-public class PrimitiveTextProvider<T> 
+public class PrimitiveTextProvider<T> extends AbstractConfigurableProvider
     implements MessageBodyReader<T>, MessageBodyWriter<T> {
     private int bufferSize = IOUtils.DEFAULT_BUFFER_SIZE;
     
-    private static boolean isSupported(Class<?> type) { 
-        return InjectionUtils.isPrimitive(type);
+    private static boolean isSupported(Class<?> type, MediaType mt) { 
+        boolean isSupported = InjectionUtils.isPrimitive(type);
+        return isSupported && (String.class == type || mt.isCompatible(MediaType.TEXT_PLAIN_TYPE));
     }
     
     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
-        return isSupported(type);
+        return isSupported(type, mt);
     }
 
     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)) {
+            reportEmptyContentLength();
+        }
         if (type == Character.class) {
-            char character = StringUtils.isEmpty(string) ? ' ' : string.charAt(0);
+            char character = string.charAt(0);
             return type.cast(Character.valueOf(character));
         }
         return InjectionUtils.handleParameter(
@@ -69,7 +73,7 @@ public class PrimitiveTextProvider<T>
     }
 
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
-        return isSupported(type);
+        return isSupported(type, mt);
     }
 
     public void writeTo(T obj, Class<?> type, Type genType, Annotation[] anns, 

http://git-wip-us.apache.org/repos/asf/cxf/blob/02a35f0b/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 631d2e4..2b6e7e4 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
@@ -1256,8 +1256,8 @@ public final class InjectionUtils {
     public static boolean isPrimitive(Class<?> type) {
         return type.isPrimitive() 
             || Number.class.isAssignableFrom(type)
-            || Boolean.class.isAssignableFrom(type)
-            || Character.class.isAssignableFrom(type)
+            || Boolean.class == type
+            || Character.class == type
             || String.class == type;
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/02a35f0b/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 78863cd..54428d1 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
@@ -26,6 +26,7 @@ import java.util.Arrays;
 
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.NoContentException;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 
@@ -37,21 +38,83 @@ import org.junit.Test;
 public class PrimitiveTextProviderTest extends Assert {
     
     @Test
-    public void testIsWriteable() {
+    public void testNumberIsWriteable() {
         MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
-        assertTrue(p.isWriteable(Byte.TYPE, null, null, null)
-                   && p.isWriteable(Byte.class, null, null, null)
-                   && p.isWriteable(Boolean.TYPE, null, null, null)
-                   && p.isWriteable(Boolean.class, null, null, null));
+        assertTrue(p.isWriteable(Byte.class, null, null, MediaType.TEXT_PLAIN_TYPE));
     }
     
     @Test
-    public void testIsReadable() {
+    public void testNumberIsNotWriteable() {
+        MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
+        assertFalse(p.isWriteable(Byte.class, null, null, MediaType.valueOf("text/custom")));
+    }
+    
+    @Test
+    public void testBooleanIsWriteable() {
+        MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
+        assertTrue(p.isWriteable(Boolean.class, null, null, MediaType.TEXT_PLAIN_TYPE));
+    }
+    
+    @Test
+    public void testBooleanIsNotWriteable() {
+        MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
+        assertFalse(p.isWriteable(Boolean.class, null, null, MediaType.valueOf("text/custom")));
+    }
+    
+    @Test
+    public void testCharacterIsWriteable() {
+        MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
+        assertTrue(p.isWriteable(Character.class, null, null, MediaType.TEXT_PLAIN_TYPE));
+    }
+    
+    @Test
+    public void testCharacterIsNotWriteable() {
+        MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
+        assertFalse(p.isWriteable(Character.class, null, null, MediaType.valueOf("text/custom")));
+    }
+    
+    @Test
+    public void testStringIsWriteable() {
+        MessageBodyWriter<Object> p = new PrimitiveTextProvider<Object>();
+        assertTrue(p.isWriteable(String.class, null, null, MediaType.TEXT_PLAIN_TYPE)
+                   && p.isWriteable(String.class, null, null, MediaType.valueOf("text/custom")));
+    }
+    
+    @Test
+    public void testNumberIsReadable() {
         MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
-        assertTrue(p.isReadable(Byte.TYPE, null, null, null)
-                   && p.isReadable(Byte.class, null, null, null)
-                   && p.isReadable(Boolean.TYPE, null, null, null)
-                   && p.isReadable(Boolean.class, null, null, null));
+        assertTrue(p.isReadable(Byte.class, null, null, MediaType.TEXT_PLAIN_TYPE));
+    }
+    @Test
+    public void testNumberIsNotReadable() {
+        MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
+        assertFalse(p.isReadable(Byte.class, null, null, MediaType.valueOf("text/custom")));
+    }
+    @Test
+    public void testBooleanIsReadable() {
+        MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
+        assertTrue(p.isReadable(Boolean.class, null, null, MediaType.TEXT_PLAIN_TYPE));
+    }
+    @Test
+    public void testBooleanIsNotReadable() {
+        MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
+        assertFalse(p.isReadable(Boolean.class, null, null, MediaType.valueOf("text/custom")));
+    }
+    @Test
+    public void testCharacterIsReadable() {
+        MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
+        assertTrue(p.isReadable(Character.class, null, null, MediaType.TEXT_PLAIN_TYPE));
+    }
+    @Test
+    public void testCharacterIsNotReadable() {
+        MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
+        assertFalse(p.isReadable(Character.class, null, null, MediaType.valueOf("text/custom")));
+    }
+    @Test
+    public void testStringIsReadable() {
+        MessageBodyReader<Object> p = new PrimitiveTextProvider<Object>();
+        assertTrue(p.isReadable(String.class, null, null, MediaType.TEXT_PLAIN_TYPE)
+                   && p.isReadable(String.class, null, null, MediaType.valueOf("text/custom")));
     }
     
     @SuppressWarnings("unchecked")
@@ -69,6 +132,20 @@ public class PrimitiveTextProviderTest extends Assert {
         assertEquals(1, valueRead.byteValue());
         
     }
+    @SuppressWarnings({
+        "unchecked", "rawtypes"
+    })
+    @Test(expected = NoContentException.class)
+    public void testReadEmptyByte() throws Exception {
+        MessageBodyReader<?> p = new PrimitiveTextProvider<Object>();
+        
+        p.readFrom((Class)Byte.TYPE, null, null, 
+                                          null, 
+                                          null, 
+                                          new ByteArrayInputStream("".getBytes()));
+        
+        
+    }
     
     @SuppressWarnings({ "unchecked", "rawtypes" })
     @Test

http://git-wip-us.apache.org/repos/asf/cxf/blob/02a35f0b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
index 5393490..655c355 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
@@ -172,9 +172,12 @@ public class JAXRSSoapBookTest extends AbstractBusClientServerTestBase
{
     
     @Test
     public void testGetAll() throws Exception {
+        URL url = new URL("http://localhost:" + PORT + "/test/services/rest2/myRestService");
+        
+        URLConnection connect = url.openConnection();
+        connect.addRequestProperty("Accept", "text/plain");
+        InputStream in = connect.getInputStream();
         
-        InputStream in = getHttpInputStream("http://localhost:" + PORT 
-                                            + "/test/services/rest2/myRestService");
         assertEquals("0", getStringFromInputStream(in));
                 
     }


Mime
View raw message