Repository: camel
Updated Branches:
  refs/heads/master 8b3da67b8 -> 2b5d59e2e


CAMEL-9621: camel-restlet - The producer should support message body as bytes 
or streams


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2b5d59e2
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2b5d59e2
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2b5d59e2

Branch: refs/heads/master
Commit: 2b5d59e2e9223972ea1de108fcec32ac606d4aba
Parents: 8b3da67
Author: Claus Ibsen <davscl...@apache.org>
Authored: Fri Feb 19 09:51:23 2016 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Feb 19 09:57:59 2016 +0100

----------------------------------------------------------------------
 .../restlet/DefaultRestletBinding.java          | 44 +++++++++++++++++---
 1 file changed, 39 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/2b5d59e2/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
----------------------------------------------------------------------
diff --git 
a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
 
b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
index 59cb219..57037e4 100644
--- 
a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
+++ 
b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
@@ -32,7 +32,6 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
-
 import javax.xml.transform.dom.DOMSource;
 
 import org.apache.camel.Exchange;
@@ -60,10 +59,12 @@ import org.restlet.data.Status;
 import org.restlet.engine.application.DecodeRepresentation;
 import org.restlet.engine.header.HeaderConstants;
 import org.restlet.representation.ByteArrayRepresentation;
+import org.restlet.representation.EmptyRepresentation;
 import org.restlet.representation.FileRepresentation;
 import org.restlet.representation.InputRepresentation;
 import org.restlet.representation.Representation;
 import org.restlet.representation.StreamRepresentation;
+import org.restlet.representation.StringRepresentation;
 import org.restlet.util.Series;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -162,8 +163,11 @@ public class DefaultRestletBinding implements 
RestletBinding, HeaderFilterStrate
         // Use forms only for PUT, POST and x-www-form-urlencoded
         if ((Method.PUT == method || Method.POST == method) && mediaType == 
MediaType.APPLICATION_WWW_FORM) {
             form = new Form();
+            // must use string based for forms
             String body = exchange.getIn().getBody(String.class);
-            form.add(body, null);
+            if (body != null) {
+                form.add(body, null);
+            }
         }
 
         // login and password are filtered by header filter strategy
@@ -209,13 +213,13 @@ public class DefaultRestletBinding implements 
RestletBinding, HeaderFilterStrate
         } else {
             // include body if PUT or POST
             if (request.getMethod() == Method.PUT || request.getMethod() == 
Method.POST) {
-                String body = exchange.getIn().getBody(String.class);
-                request.setEntity(body, mediaType);
+                Representation body = createRepresentationFromBody(exchange, 
mediaType);
+                request.setEntity(body);
                 LOG.debug("Populate Restlet {} request from exchange body: {} 
using media type {}", method, body, mediaType);
             } else {
                 // no body
                 LOG.debug("Populate Restlet {} request from exchange using 
media type {}", method, mediaType);
-                request.setEntity(null);
+                request.setEntity(new EmptyRepresentation());
             }
         }
 
@@ -543,6 +547,36 @@ public class DefaultRestletBinding implements 
RestletBinding, HeaderFilterStrate
         return set;
     }
 
+    protected Representation createRepresentationFromBody(Exchange exchange, 
MediaType mediaType) {
+        Object body = exchange.getIn().getBody();
+        if (body == null) {
+            return new EmptyRepresentation();
+        }
+
+        // unwrap file
+        if (body instanceof WrappedFile) {
+            body = ((WrappedFile) body).getFile();
+        }
+
+        if (body instanceof InputStream) {
+            return new InputRepresentation((InputStream) body, mediaType);
+        } else if (body instanceof File) {
+            return new FileRepresentation((File) body, mediaType);
+        } else if (body instanceof byte[]) {
+            return new ByteArrayRepresentation((byte[]) body, mediaType);
+        } else if (body instanceof String) {
+            return new StringRepresentation((CharSequence) body, mediaType);
+        }
+
+        // fallback as string
+        body = exchange.getIn().getBody(String.class);
+        if (body != null) {
+            return new StringRepresentation((CharSequence) body, mediaType);
+        } else {
+            return new EmptyRepresentation();
+        }
+    }
+
     public HeaderFilterStrategy getHeaderFilterStrategy() {
         return headerFilterStrategy;
     }

Reply via email to