You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2012/03/30 18:25:05 UTC
svn commit: r1307507 - in /cxf/branches/2.3.x-fixes: ./
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/
Author: sergeyb
Date: Fri Mar 30 16:25:04 2012
New Revision: 1307507
URL: http://svn.apache.org/viewvc?rev=1307507&view=rev
Log:
Merged revisions 1307497 via svnmerge from
https://svn.apache.org/repos/asf/cxf/branches/2.4.x-fixes
................
r1307497 | sergeyb | 2012-03-30 17:02:17 +0100 (Fri, 30 Mar 2012) | 16 lines
Merged revisions 1307492 via svnmerge from
https://svn.apache.org/repos/asf/cxf/branches/2.5.x-fixes
................
r1307492 | sergeyb | 2012-03-30 16:55:06 +0100 (Fri, 30 Mar 2012) | 9 lines
Merged revisions 1307387 via svnmerge from
https://svn.apache.org/repos/asf/cxf/trunk
........
r1307387 | sergeyb | 2012-03-30 13:48:14 +0100 (Fri, 30 Mar 2012) | 1 line
[CXF-4217] Introduce Nullable annotation to help override the handling of empty payloads by the JAXB provider
........
................
................
Added:
cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java
- copied unchanged from r1307497, cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java
Modified:
cxf/branches/2.3.x-fixes/ (props changed)
cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
Merged /cxf/branches/2.5.x-fixes:r1307492
Merged /cxf/branches/2.4.x-fixes:r1307497
Merged /cxf/trunk:r1307387
Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java?rev=1307507&r1=1307506&r2=1307507&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java (original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractConfigurableProvider.java Fri Mar 30 16:25:04 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/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1307507&r1=1307506&r2=1307507&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java (original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java Fri Mar 30 16:25:04 2012
@@ -40,7 +40,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.Response;
import javax.ws.rs.ext.ContextResolver;
@@ -181,20 +180,16 @@ 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 <T> T getStaxHandlerFromCurrentMessage(Class<T> staxCls) {
Message m = PhaseInterceptorChain.getCurrentMessage();
if (m != null) {
Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1307507&r1=1307506&r2=1307507&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Fri Mar 30 16:25:04 2012
@@ -55,7 +55,9 @@ import javax.xml.stream.XMLStreamWriter;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.jaxb.NamespaceMapper;
import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.Nullable;
import org.apache.cxf.jaxrs.ext.xml.XMLSource;
+import org.apache.cxf.jaxrs.utils.AnnotationUtils;
import org.apache.cxf.jaxrs.utils.HttpUtils;
import org.apache.cxf.jaxrs.utils.InjectionUtils;
import org.apache.cxf.jaxrs.utils.JAXBUtils;
@@ -125,10 +127,16 @@ public class JAXBElementProvider extends
public Object readFrom(Class<Object> 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/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java?rev=1307507&r1=1307506&r2=1307507&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java (original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java Fri Mar 30 16:25:04 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;
@@ -122,13 +121,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/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1307507&r1=1307506&r2=1307507&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Fri Mar 30 16:25:04 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;
@@ -722,6 +723,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/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1307507&r1=1307506&r2=1307507&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original)
+++ cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Fri Mar 30 16:25:04 2012
@@ -1179,6 +1179,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");
}