Author: davsclaus
Date: Mon Jun 18 13:55:14 2012
New Revision: 1351344

URL: http://svn.apache.org/viewvc?rev=1351344&view=rev
Log:
CAMEL-5367: Fixed chunked=false corrupting binary data when Camel jetty writes 
response to clients.

Modified:
    camel/branches/camel-2.8.x/   (props changed)
    
camel/branches/camel-2.8.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
    
camel/branches/camel-2.8.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
    
camel/branches/camel-2.8.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1351342
  Merged /camel/branches/camel-2.9.x:r1351343

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-2.8.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java?rev=1351344&r1=1351343&r2=1351344&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
 (original)
+++ 
camel/branches/camel-2.8.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
 Mon Jun 18 13:55:14 2012
@@ -307,13 +307,18 @@ public class DefaultHttpBinding implemen
             }
         }
 
-        // other kind of content type
-        InputStream is = null;
+        // prefer streaming
+        InputStream is;
         if (checkChunked(message, exchange)) {
             is = message.getBody(InputStream.class);
+        } else {
+            // try to use input stream first, so we can copy directly
+            is = 
exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, 
message.getBody());
         }
+
         if (is != null) {
             ServletOutputStream os = response.getOutputStream();
+            LOG.trace("Writing direct response from source input stream to 
servlet output stream");
             try {
                 // copy directly from input stream to output stream
                 IOHelper.copy(is, os);
@@ -322,16 +327,20 @@ public class DefaultHttpBinding implemen
                 IOHelper.close(is);
             }
         } else {
-            // not convertable as a stream so try as a String
+            // not convertable as a stream so fallback as a String
             String data = message.getBody(String.class);
             if (data != null) {
+                LOG.debug("Cannot write from source input stream, falling back 
to using String content. For binary content this can be a problem.");
                 // set content length and encoding before we write data
                 String charset = IOHelper.getCharsetName(exchange, true);
                 final int dataByteLength = data.getBytes(charset).length;
                 response.setCharacterEncoding(charset);
                 response.setContentLength(dataByteLength);
-                response.getWriter().print(data);
-                response.getWriter().flush();
+                try {
+                    response.getWriter().print(data);
+                } finally {
+                    response.getWriter().flush();
+                }
             }
         }
     }

Modified: 
camel/branches/camel-2.8.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java?rev=1351344&r1=1351343&r2=1351344&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
 (original)
+++ 
camel/branches/camel-2.8.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
 Mon Jun 18 13:55:14 2012
@@ -290,14 +290,19 @@ public class DefaultHttpBinding implemen
             }
         }
 
-        // other kind of content type
-        InputStream is = null;
+        // prefer streaming
+        InputStream is;
         if (checkChunked(message, exchange)) {
             is = message.getBody(InputStream.class);
+        } else {
+            // try to use input stream first, so we can copy directly
+            is = 
exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, 
message.getBody());
         }
+
         if (is != null) {
             ServletOutputStream os = response.getOutputStream();
             try {
+                LOG.trace("Writing direct response from source input stream to 
servlet output stream");
                 // copy directly from input stream to output stream
                 IOHelper.copy(is, os);
             } finally {
@@ -308,10 +313,17 @@ public class DefaultHttpBinding implemen
             // not convertable as a stream so try as a String
             String data = message.getBody(String.class);
             if (data != null) {
+                LOG.debug("Cannot write from source input stream, falling back 
to using String content. For binary content this can be a problem.");
                 // set content length before we write data
-                response.setContentLength(data.length());
-                response.getWriter().print(data);
-                response.getWriter().flush();
+                String charset = IOHelper.getCharsetName(exchange, true);
+                final int dataByteLength = data.getBytes(charset).length;
+                response.setCharacterEncoding(charset);
+                response.setContentLength(dataByteLength);
+                try {
+                    response.getWriter().print(data);
+                } finally {
+                    response.getWriter().flush();
+                }
             }
         }
     }

Modified: 
camel/branches/camel-2.8.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java?rev=1351344&r1=1351343&r2=1351344&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
 (original)
+++ 
camel/branches/camel-2.8.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
 Mon Jun 18 13:55:14 2012
@@ -64,9 +64,6 @@ public class HttpClientRouteTest extends
         log.info("Headers: " + headers);
         
         assertTrue("Should be more than one header but was: " + headers, 
headers.size() > 0);
-        
-        // should get the Content-Length
-        assertNotNull("Should get the content-length", 
headers.get("Content-Length"));
     }
     
     @Test


Reply via email to