cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1497090 - in /cxf/trunk/rt: rs/client/src/main/java/org/apache/cxf/jaxrs/client/ transports/http/src/main/java/org/apache/cxf/transport/http/ transports/http/src/test/java/org/apache/cxf/transport/http/
Date Wed, 26 Jun 2013 20:50:42 GMT
Author: sergeyb
Date: Wed Jun 26 20:50:41 2013
New Revision: 1497090

URL: http://svn.apache.org/r1497090
Log:
[CXF-5093] Initial try to update HTTPConduit and also RS client code to better deal with requests
with no payloads

Modified:
    cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
    cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java

Modified: cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=1497090&r1=1497089&r2=1497090&view=diff
==============================================================================
--- cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java (original)
+++ cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java Wed
Jun 26 20:50:41 2013
@@ -45,6 +45,7 @@ import javax.ws.rs.WebApplicationExcepti
 import javax.ws.rs.client.ResponseProcessingException;
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.EntityTag;
+import javax.ws.rs.core.Form;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
@@ -906,10 +907,10 @@ public abstract class AbstractClient imp
         
         m.put(Message.CONTENT_TYPE, headers.getFirst(HttpHeaders.CONTENT_TYPE));
         
+        body = checkIfBodyEmpty(body);
+        setEmptyRequestPropertyIfNeeded(m, body);    
+        
         m.setContent(List.class, getContentsList(body));
-        if (body == null) {
-            setEmptyRequestProperty(m, httpMethod);
-        }
         
         m.put(URITemplate.TEMPLATE_PARAMETERS, getState().getTemplates());
         
@@ -930,6 +931,23 @@ public abstract class AbstractClient imp
         return m;
     }
     
+    protected void setEmptyRequestPropertyIfNeeded(Message outMessage, Object body) {
+        if (body == null) {
+            outMessage.put("org.apache.cxf.empty.request", true);
+        }
+    }
+
+    
+    protected Object checkIfBodyEmpty(Object body) {
+        if (body != null 
+            && (body.getClass() == String.class && ((String)body).length()
== 0 
+            || body.getClass() == Form.class && ((Form)body).asMap().isEmpty()
+            || Map.class.isAssignableFrom(body.getClass()) && ((Map<?, ?>)body).isEmpty()))
{
+            body = null;
+        }
+        return body;
+    }
+    
     protected Map<String, Object> getRequestContext(Message outMessage) {
         Map<String, Object> invContext 
             = CastUtils.cast((Map<?, ?>)outMessage.get(Message.INVOCATION_CONTEXT));
@@ -984,12 +1002,6 @@ public abstract class AbstractClient imp
         exchange.putAll(reqContext);
     }
     
-    protected void setEmptyRequestProperty(Message outMessage, String httpMethod) {
-        if ("POST".equals(httpMethod)) {
-            outMessage.put("org.apache.cxf.post.empty", true);
-        }
-    }
-    
     protected void setPlainOperationNameProperty(Message outMessage, String name) {
         outMessage.getExchange().put("org.apache.cxf.resource.operation.name", name);
     }

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java?rev=1497090&r1=1497089&r2=1497090&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
Wed Jun 26 20:50:41 2013
@@ -166,7 +166,8 @@ public abstract class HTTPConduit 
 
     private static final String HTTP_POST_METHOD = "POST"; 
     private static final String HTTP_PUT_METHOD = "PUT";
-    
+    private static final Set<String> KNOWN_HTTP_VERBS_WITH_NO_CONTENT = 
+        new HashSet<String>(Arrays.asList(new String[]{"GET", "DELETE", "HEAD", "OPTIONS",
"TRACE"}));
     /**
      * This constant is the Message(Map) key for a list of visited URLs that
      * is used in redirect loop protection.
@@ -1264,21 +1265,16 @@ public abstract class HTTPConduit 
             
             // Trust is okay, set up for writing the request.
             
-            // If this is a GET method we must not touch the output
-            // stream as this automatically turns the request into a POST.
-            // Nor it should be done in case of DELETE/HEAD/OPTIONS 
-            // - strangely, empty PUTs work ok
             String method = getMethod();
-            if (!"POST".equals(method)
-                && !"PUT".equals(method)) {
+            if (KNOWN_HTTP_VERBS_WITH_NO_CONTENT.contains(method)) {
                 handleNoOutput();
                 return;
             }
-            if (outMessage.get("org.apache.cxf.post.empty") != null) {
+
+            if (outMessage.get("org.apache.cxf.empty.request") != null) {
                 handleNoOutput();
                 return;
             }
-            
             setupWrappedStream();
         }
         

Modified: cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java?rev=1497090&r1=1497089&r2=1497090&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java
(original)
+++ cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java
Wed Jun 26 20:50:41 2013
@@ -42,6 +42,7 @@ import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.io.AbstractThresholdOutputStream;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageContentsList;
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.transport.MessageObserver;
@@ -138,7 +139,7 @@ public class HTTPConduitURLEasyMockTest 
     public void testSend() throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, false);
-        Message message = new MessageImpl();
+        Message message = createMessage();
         conduit.prepare(message);
         verifySentMessage(conduit, message, "POST");
         finalVerify();
@@ -148,14 +149,20 @@ public class HTTPConduitURLEasyMockTest 
     public void testSendWithHeaders() throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, false);
-        Message message = new MessageImpl();
-        message.put("Content-Type", "text/xml;charset=utf8");
+        Message message = createMessage();
         setUpHeaders(message);
         conduit.prepare(message);
         verifySentMessage(conduit, message, true, "POST", false);
         finalVerify();
     }
     
+    private Message createMessage() {
+        Message message = new MessageImpl();
+        message.put("Content-Type", "text/xml;charset=utf8");
+        message.setContent(List.class, new MessageContentsList("<body/>"));
+        return message;
+    }
+    
     public void testSendWithHeadersCheckErrorStream() throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, false);
@@ -171,7 +178,7 @@ public class HTTPConduitURLEasyMockTest 
     public void testSendHttpConnection() throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, false);
-        Message message = new MessageImpl();
+        Message message = createMessage();
         conduit.prepare(message);
         verifySentMessage(conduit, message, "POST");
         finalVerify();
@@ -181,7 +188,7 @@ public class HTTPConduitURLEasyMockTest 
     public void testSendHttpConnectionAutoRedirect() throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, true);
-        Message message = new MessageImpl();
+        Message message = createMessage();
         conduit.prepare(message);
         verifySentMessage(conduit, message, "POST");
         finalVerify();
@@ -216,7 +223,7 @@ public class HTTPConduitURLEasyMockTest 
         throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, false);
-        Message message = new MessageImpl();
+        Message message = createMessage();
         conduit.prepare(message);
         message.put(Message.PROCESS_ONEWAY_RESPONSE, Boolean.TRUE);
         verifySentMessage(conduit, 
@@ -233,7 +240,7 @@ public class HTTPConduitURLEasyMockTest 
         throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, false);
-        Message message = new MessageImpl();
+        Message message = createMessage();
         conduit.prepare(message);
         verifySentMessage(conduit, 
                           message, 
@@ -249,7 +256,7 @@ public class HTTPConduitURLEasyMockTest 
         throws Exception {
         control = EasyMock.createNiceControl();
         HTTPConduit conduit = setUpConduit(true, false);
-        Message message = new MessageImpl();
+        Message message = createMessage();
         conduit.prepare(message);
         verifySentMessage(conduit, 
                           message, 



Mime
View raw message