camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r829339 - /camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java
Date Sat, 24 Oct 2009 10:20:54 GMT
Author: davsclaus
Date: Sat Oct 24 10:20:54 2009
New Revision: 829339

URL: http://svn.apache.org/viewvc?rev=829339&view=rev
Log:
CAMEL-2104: Stream component now leverages Camels type converter so its send to system out
etc.

Modified:
    camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java

Modified: camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java?rev=829339&r1=829338&r2=829339&view=diff
==============================================================================
--- camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java
(original)
+++ camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java
Sat Oct 24 10:20:54 2009
@@ -119,26 +119,31 @@
 
     private void writeToStream(Exchange exchange) throws IOException, CamelExchangeException
{
         Object body = exchange.getIn().getBody();
-        if (body instanceof String) {
-            Charset charset = endpoint.getCharset();
-            Writer writer = new OutputStreamWriter(outputStream, charset);
-            BufferedWriter bw = new BufferedWriter(writer);
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Writing as text: " + body + " to " + outputStream + " using encoding:"
+ charset);
-            }
-            bw.write((String)body);
-            bw.write("\n");
-            bw.flush();
-            // important: do not close the writer as it will close the standard system.out
etc.
-        } else if (body instanceof byte[]) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Writing as byte[]: " + body + " to " + outputStream);
+
+        // if not a string then try as byte array first
+        if (!(body instanceof String)) {
+            byte[] bytes = exchange.getIn().getBody(byte[].class);
+            if (bytes != null) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Writing as byte[]: " + bytes + " to " + outputStream);
+                }
+                outputStream.write(bytes);
+                return;
             }
-            outputStream.write((byte[])body);
-        } else {
-            throw new CamelExchangeException("The body is neither a String or byte array.
"
-                + "Cannot write body to output stream", exchange);
         }
+
+        // okay now fallback to mandatory converterable to string
+        String s = exchange.getIn().getMandatoryBody(String.class);
+        Charset charset = endpoint.getCharset();
+        Writer writer = new OutputStreamWriter(outputStream, charset);
+        BufferedWriter bw = new BufferedWriter(writer);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Writing as text: " + body + " to " + outputStream + " using encoding:"
+ charset);
+        }
+        bw.write(s);
+        bw.write("\n");
+        bw.flush();
+        // important: do not close the writer as it will close the standard system.out etc.
     }
 
     private void validateUri(String uri) throws Exception {



Mime
View raw message