cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1398302 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/rs/extensions/providers/src/mai...
Date Mon, 15 Oct 2012 14:27:08 GMT
Author: sergeyb
Date: Mon Oct 15 14:27:07 2012
New Revision: 1398302

URL: http://svn.apache.org/viewvc?rev=1398302&view=rev
Log:
[CXF-4563] Fixing the way empty responses are read on the client side

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AbstractResponseContextImpl.java
    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/ProviderFactory.java
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: 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=1398302&r1=1398301&r2=1398302&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java Mon Oct
15 14:27:07 2012
@@ -32,7 +32,7 @@ import java.lang.annotation.Target;
  * injected instead  
  * 
  */
-@Target(ElementType.PARAMETER)
+@Target({ElementType.PARAMETER, ElementType.METHOD })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Nullable {
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AbstractResponseContextImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AbstractResponseContextImpl.java?rev=1398302&r1=1398301&r2=1398302&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AbstractResponseContextImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AbstractResponseContextImpl.java
Mon Oct 15 14:27:07 2012
@@ -142,6 +142,7 @@ public abstract class AbstractResponseCo
     }
     
     public void setStatus(int status) {
+        m.getExchange().put(Message.RESPONSE_CODE, status);
         m.put(Message.RESPONSE_CODE, status);
         ((ResponseImpl)r).setStatus(status);
         updateMessageResponse();

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=1398302&r1=1398301&r2=1398302&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
Mon Oct 15 14:27:07 2012
@@ -22,6 +22,7 @@ package org.apache.cxf.jaxrs.provider;
 import java.util.List;
 
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
@@ -148,16 +149,25 @@ public abstract class AbstractConfigurab
     
     protected boolean isPayloadEmpty(HttpHeaders headers) {
         if (headers != null) {
-            List<String> values = headers.getRequestHeader(HttpHeaders.CONTENT_LENGTH);
-            if (values.size() == 1) {
+            return isPayloadEmpty(headers.getRequestHeaders());
+        } else {
+            return false;
+        }
+    }
+    
+    protected boolean isPayloadEmpty(MultivaluedMap<String, String> headers) {
+        if (headers != null) {
+            String value = headers.getFirst(HttpHeaders.CONTENT_LENGTH);
+            if (value != null) {
                 try {
-                    Long len = Long.valueOf(values.get(0));
+                    Long len = Long.valueOf(value);
                     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=1398302&r1=1398301&r2=1398302&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
Mon Oct 15 14:27:07 2012
@@ -200,10 +200,6 @@ public abstract class AbstractJAXBProvid
         jaxbElementClassMap = map;
     }
     
-    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);

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=1398302&r1=1398301&r2=1398302&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
Mon Oct 15 14:27:07 2012
@@ -141,7 +141,7 @@ 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 (isPayloadEmpty(headers)) {
             if (AnnotationUtils.getAnnotation(anns, Nullable.class) != null) {
                 return null;
             } else {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=1398302&r1=1398301&r2=1398302&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
Mon Oct 15 14:27:07 2012
@@ -244,13 +244,14 @@ public final class ProviderFactory {
         Message responseMessage = isRequestor ? m.getExchange().getInMessage() 
                                               : m.getExchange().getOutMessage();
         if (responseMessage != null) {
-            if (!responseMessage.containsKey(Message.CONTENT_TYPE)) {
+            Object ctProperty = responseMessage.get(Message.CONTENT_TYPE);
+            if (ctProperty == null) {
                 List<MediaType> accepts = requestHeaders.getAcceptableMediaTypes();
                 if (accepts.size() > 0) {
                     mt = accepts.get(0);
                 }
             } else {
-                mt = MediaType.valueOf(responseMessage.get(Message.CONTENT_TYPE).toString());
+                mt = MediaType.valueOf(ctProperty.toString());
             }
         } else {
             mt = requestHeaders.getMediaType();

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java?rev=1398302&r1=1398301&r2=1398302&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
Mon Oct 15 14:27:07 2012
@@ -194,7 +194,7 @@ public class JSONProvider<T> extends Abs
         MultivaluedMap<String, String> headers, InputStream is) 
         throws IOException {
         
-        if (isPayloadEmpty()) {
+        if (isPayloadEmpty(headers)) {
             if (AnnotationUtils.getAnnotation(anns, Nullable.class) != null) {
                 return null;
             } else {

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=1398302&r1=1398301&r2=1398302&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
Mon Oct 15 14:27:07 2012
@@ -1083,6 +1083,37 @@ public class JAXRSClientServerBookTest e
         assertEquals(444L, book.getId());
     }
     
+    @Test(expected = ClientException.class)
+    public void testEmptyJSON() {
+        doTestEmptyResponse("application/json");
+    }
+    
+    @Test(expected = ClientException.class)
+    public void testEmptyJAXB() {
+        doTestEmptyResponse("application/xml");
+    }
+    
+    private void doTestEmptyResponse(String mt) {
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/emptybook");
+        WebClient.getConfig(wc).getInInterceptors().add(new ReplaceStatusInterceptor());
+        wc.accept(mt);
+        wc.get(Book.class);
+    }
+    
+    @Test(expected = ClientException.class)
+    public void testEmptyResponseProxy() {
+        BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
+        WebClient.getConfig(store).getInInterceptors().add(new ReplaceStatusInterceptor());
+        store.getEmptyBook();
+    }
+    
+    @Test
+    public void testEmptyResponseProxyNullable() {
+        BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
+        WebClient.getConfig(store).getInInterceptors().add(new ReplaceStatusInterceptor());
+        assertNull(store.getEmptyBookNullable());
+    }
+    
     @Test
     public void testFormattedJSON() {
         WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/123");
@@ -1909,4 +1940,14 @@ public class JAXRSClientServerBookTest e
         }
     }
 
+    public static class ReplaceStatusInterceptor extends AbstractPhaseInterceptor<Message>
{
+        public ReplaceStatusInterceptor() {
+            super(Phase.READ);
+        }
+
+        public void handleMessage(Message message) throws Fault {
+            message.getExchange().put(Message.RESPONSE_CODE, 200);
+        }
+    }
+    
 }



Mime
View raw message