You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/06/18 15:53:19 UTC

svn commit: r1351343 - in /camel/branches/camel-2.9.x: ./ components/camel-http/src/main/java/org/apache/camel/component/http/ components/camel-http4/src/main/java/org/apache/camel/component/http4/ components/camel-jetty/src/test/java/org/apache/camel/...

Author: davsclaus
Date: Mon Jun 18 13:53:18 2012
New Revision: 1351343

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

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

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

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

Modified: camel/branches/camel-2.9.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java?rev=1351343&r1=1351342&r2=1351343&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java (original)
+++ camel/branches/camel-2.9.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java Mon Jun 18 13:53:18 2012
@@ -309,13 +309,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);
@@ -324,16 +329,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.9.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java?rev=1351343&r1=1351342&r2=1351343&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java (original)
+++ camel/branches/camel-2.9.x/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java Mon Jun 18 13:53:18 2012
@@ -292,14 +292,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 {
@@ -310,10 +315,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.9.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java?rev=1351343&r1=1351342&r2=1351343&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java (original)
+++ camel/branches/camel-2.9.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java Mon Jun 18 13:53:18 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