cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1307387 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Fri, 30 Mar 2012 12:48:15 GMT
Author: sergeyb
Date: Fri Mar 30 12:48:14 2012
New Revision: 1307387

URL: http://svn.apache.org/viewvc?rev=1307387&view=rev
Log:
[CXF-4217] Introduce Nullable annotation to help override the handling of empty payloads by
the JAXB provider

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java   (with
props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java?rev=1307387&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java Fri Mar
30 12:48:14 2012
@@ -0,0 +1,38 @@
+/**
+ * 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.ext;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Some JAX-RS providers, notably JAXB-based ones
+ * return 400 when the incoming payload is empty.
+ * This annotation can be attached to a method parameter
+ * identifying a request body in order to get a null 
+ * injected instead  
+ * 
+ */
+@Target(ElementType.PARAMETER)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Nullable {
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java?rev=1307387&r1=1307386&r2=1307387&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
Fri Mar 30 12:48:14 2012
@@ -21,6 +21,8 @@ package org.apache.cxf.jaxrs.provider;
 
 import java.util.List;
 
+import javax.ws.rs.core.HttpHeaders;
+
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
@@ -143,4 +145,19 @@ public abstract class AbstractConfigurab
     public void init(List<ClassResourceInfo> resources) {
         // complete
     }
+    
+    protected boolean isPayloadEmpty(HttpHeaders headers) {
+        if (headers != null) {
+            List<String> values = headers.getRequestHeader(HttpHeaders.CONTENT_LENGTH);
+            if (values.size() == 1) {
+                try {
+                    Long len = Long.valueOf(values.get(0));
+                    return len <= 0;
+                } catch (NumberFormatException ex) {
+                    // ignore
+                }
+            }
+        }
+        return false;
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1307387&r1=1307386&r2=1307387&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Fri Mar 30 12:48:14 2012
@@ -38,7 +38,6 @@ import java.util.Set;
 import java.util.logging.Logger;
 
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
@@ -173,18 +172,14 @@ public abstract class AbstractJAXBProvid
         jaxbElementClassMap = map;
     }
     
-    protected void checkContentLength() {
-        if (mc != null) {
-            HttpHeaders headers = mc.getHttpHeaders();
-            if (headers != null) {
-                List<String> values = mc.getHttpHeaders().getRequestHeader(HttpHeaders.CONTENT_LENGTH);
-                if (values.size() == 1 && "0".equals(values.get(0))) {
-                    String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY",
BUNDLE).toString();
-                    LOG.warning(message);
-                    throw new WebApplicationException(400);
-                }
-            }
-        }
+    protected boolean isPayloadEmpty() {
+        return mc != null ? isPayloadEmpty(mc.getHttpHeaders()) : false;     
+    }
+    
+    protected void reportEmptyContentLength() {
+        String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
+        LOG.warning(message);
+        throw new WebApplicationException(400);
     }
     
     protected <X> X getStreamHandlerFromCurrentMessage(Class<X> staxCls) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1307387&r1=1307386&r2=1307387&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Fri Mar 30 12:48:14 2012
@@ -60,6 +60,7 @@ import javax.xml.transform.Source;
 import org.apache.cxf.common.jaxb.NamespaceMapper;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.jaxrs.ext.xml.XSISchemaLocation;
@@ -153,10 +154,16 @@ public class JAXBElementProvider<T> exte
     public T readFrom(Class<T> type, Type genericType, Annotation[] anns, MediaType
mt, 
         MultivaluedMap<String, String> headers, InputStream is) 
         throws IOException {
+        if (isPayloadEmpty()) {
+            if (AnnotationUtils.getAnnotation(anns, Nullable.class) != null) {
+                return null;
+            } else {
+                reportEmptyContentLength();
+            }
+        }
+        
         try {
             
-            checkContentLength();
-            
             boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(type);
             Class<?> theGenericType = isCollection ? InjectionUtils.getActualType(genericType)
: type;
             Class<?> theType = getActualType(theGenericType, genericType, anns);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java?rev=1307387&r1=1307386&r2=1307387&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
Fri Mar 30 12:48:14 2012
@@ -44,7 +44,6 @@ import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
-import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyReader;
@@ -123,13 +122,10 @@ public class MultipartProvider extends A
     }
     
     protected void checkContentLength() {
-        if (mc != null) {
-            List<String> values = mc.getHttpHeaders().getRequestHeader(HttpHeaders.CONTENT_LENGTH);
-            if (values.size() == 1 && "0".equals(values.get(0))) {
-                String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
-                LOG.warning(message);
-                throw new WebApplicationException(400);
-            }
+        if (mc != null && isPayloadEmpty(mc.getHttpHeaders())) {
+            String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
+            LOG.warning(message);
+            throw new WebApplicationException(400);
         }
     }
     

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1307387&r1=1307386&r2=1307387&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Fri
Mar 30 12:48:14 2012
@@ -75,6 +75,7 @@ import javax.xml.transform.dom.DOMSource
 import org.apache.cxf.annotations.GZIP;
 import org.apache.cxf.common.util.ProxyHelper;
 import org.apache.cxf.helpers.XMLUtils;
+import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.Oneway;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.SearchContext;
@@ -753,6 +754,17 @@ public class BookStore {
     }
 
     @POST
+    @Path("/books/null")
+    @Produces("application/xml")
+    @Consumes("application/xml")
+    public Book handleNullBook(@Nullable Book book) {
+        if (book != null) {
+            throw new WebApplicationException(400);
+        }
+        return new Book("Default Book", 222L);
+    }
+    
+    @POST
     @Path("/books")
     @Produces("text/xml")
     @Consumes("application/xml")

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1307387&r1=1307386&r2=1307387&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Fri Mar 30 12:48:14 2012
@@ -1337,6 +1337,19 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testAddBookEmptyContent() throws Exception {
+        Response r = WebClient.create("http://localhost:" + PORT + "/bookstore/books").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);
+        assertEquals("Default Book", defaultBook.getName());
+    }
+    
+    @Test
     public void testAddBook() throws Exception {
         doAddBook("http://localhost:" + PORT + "/bookstore/books");               
     }



Mime
View raw message