You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/06/21 08:46:42 UTC

svn commit: r956436 - in /camel/trunk/components: camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java camel-jetty/src/test/java/org/apache/camel/component/jetty/file/JettyFileConsumerTest.java

Author: ningjiang
Date: Mon Jun 21 01:48:03 2010
New Revision: 956436

URL: http://svn.apache.org/viewvc?rev=956436&view=rev
Log:
CAMEL-2833 support for binary file in HttpProducer by applying patch with thanks to Nick

Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/file/JettyFileConsumerTest.java

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=956436&r1=956435&r2=956436&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Mon Jun 21 01:48:03 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.http;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
@@ -27,6 +28,7 @@ import org.apache.camel.Message;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.http.helper.GZIPHelper;
 import org.apache.camel.component.http.helper.HttpProducerHelper;
+import org.apache.camel.converter.IOConverter;
 import org.apache.camel.converter.stream.CachedOutputStream;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.spi.HeaderFilterStrategy;
@@ -36,6 +38,8 @@ import org.apache.commons.httpclient.Hea
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
+import org.apache.commons.httpclient.methods.FileRequestEntity;
+import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
 import org.apache.commons.httpclient.methods.RequestEntity;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
 import org.apache.commons.logging.Log;
@@ -278,14 +282,20 @@ public class HttpProducer extends Defaul
             return null;
         }
 
-        RequestEntity answer = in.getBody(RequestEntity.class);        
+        RequestEntity answer = in.getBody(RequestEntity.class);
         if (answer == null) {
             try {
-                String data = in.getBody(String.class);
+                Object data = in.getBody();
                 if (data != null) {
                     String contentType = ExchangeHelper.getContentType(exchange);
-                    String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
-                    answer = new StringRequestEntity(data, contentType, charset);
+                    if (data instanceof File) {
+                        answer = new FileRequestEntity((File)data, contentType);                        
+                    } else if (data instanceof String) {
+                        String charset = IOConverter.getCharsetName(exchange);
+                        answer = new StringRequestEntity((String)data, contentType, charset);
+                    } else {
+                        answer = new InputStreamRequestEntity(in.getBody(InputStream.class), contentType);
+                    }
                 }
             } catch (UnsupportedEncodingException e) {
                 throw new RuntimeCamelException(e);

Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/file/JettyFileConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/file/JettyFileConsumerTest.java?rev=956436&r1=956435&r2=956436&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/file/JettyFileConsumerTest.java (original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/file/JettyFileConsumerTest.java Mon Jun 21 01:48:03 2010
@@ -31,7 +31,7 @@ import org.junit.Test;
 public class JettyFileConsumerTest extends CamelTestSupport {
     
     private void testingSendingFile(File src) throws Exception {
-        deleteDirectory("target/file");
+        deleteDirectory("target/test");
         FileInputStream fis = new FileInputStream(src);
         String response = template.requestBody("http://localhost:9080/myapp/myservice", fis, String.class);
         assertEquals("Response should be OK ", "OK", response);
@@ -54,6 +54,17 @@ public class JettyFileConsumerTest exten
         testingSendingFile(src);
     }
     
+    @Test
+    public void testSendBinaryFile() throws Exception {
+        deleteDirectory("target/test");
+        File jpg = new File("src/test/resources/java.jpg");
+        String response = template.requestBody("http://localhost:9080/myapp/myservice2", jpg, String.class);
+        assertEquals("Response should be OK ", "OK", response);
+        File des = new File("target/test/java.jpg");
+        assertTrue("The uploaded file should exists", des.exists());
+        assertEquals("This two file should have same size", jpg.length(), des.length());
+    }
+    
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -61,7 +72,10 @@ public class JettyFileConsumerTest exten
                 from("jetty:http://localhost:9080/myapp/myservice")
                     .to("file://target/test?fileName=temp.xml")
                     .setBody(constant("OK"));
-                    
+                
+                from("jetty:http://localhost:9080/myapp/myservice2")
+                    .to("file://target/test?fileName=java.jpg")
+                    .setBody(constant("OK"));
             }
         };
     }