camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r647575 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/impl/converter/ main/java/org/apache/camel/model/dataformat/ test/java/org/apache/camel/impl/
Date Sun, 13 Apr 2008 15:06:25 GMT
Author: davsclaus
Date: Sun Apr 13 08:06:22 2008
New Revision: 647575

URL: http://svn.apache.org/viewvc?rev=647575&view=rev
Log:
CAMEL-322 - Added charset option (for encoding) to string data format

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/StringDataFormat.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java?rev=647575&r1=647574&r2=647575&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
Sun Apr 13 08:06:22 2008
@@ -119,7 +119,16 @@
      * Uses the String data format
      */
     public T string() {
-        return dataFormat(new StringDataFormat());
+        return string(null);
+    }
+
+    /**
+     * Uses the String data format supporting encoding using given charset
+     */
+    public T string(String charset) {
+        StringDataFormat sdf = new StringDataFormat();
+        sdf.setCharset(charset);
+        return dataFormat(sdf);
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/StringDataFormat.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/StringDataFormat.java?rev=647575&r1=647574&r2=647575&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/StringDataFormat.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/StringDataFormat.java
Sun Apr 13 08:06:22 2008
@@ -19,8 +19,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.converter.IOConverter;
@@ -28,20 +26,42 @@
 import org.apache.camel.util.ExchangeHelper;
 
 /**
- * The <a href="http://activemq.apache.org/camel/data-format.html">data format</a>
- * using Java Serialiation.
+ * The text based <a href="http://activemq.apache.org/camel/data-format.html">data
format</a> supporting
+ * charset encoding.
  *
  * @version $Revision$
  */
 public class StringDataFormat implements DataFormat {
+
+    private String charset;
+
+    public StringDataFormat(String charset) {
+        this.charset = charset;
+    }
+
     public void marshal(Exchange exchange, Object graph, OutputStream stream) throws IOException
{
         String text = ExchangeHelper.convertToType(exchange, String.class, graph);
-        Writer out = new OutputStreamWriter(stream);
-        out.write(text);
-        out.flush();
+
+        byte[] bytes;
+        if (charset != null) {
+            bytes = text.getBytes(charset);
+        } else {
+            bytes = text.getBytes();
+        }
+        stream.write(bytes);
     }
 
     public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, ClassNotFoundException
{
-        return IOConverter.toString(stream);
+        byte[] bytes = IOConverter.toBytes(stream);
+
+        String answer;
+        if (charset != null) {
+            answer = new String(bytes, charset);
+        } else {
+            answer = new String(bytes);
+        }
+
+        return answer;
     }
+    
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java?rev=647575&r1=647574&r2=647575&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConverterRegistry.java
Sun Apr 13 08:06:22 2008
@@ -24,7 +24,7 @@
  */
 public interface TypeConverterRegistry {
     /**
-     * Allows a new type converter to be bregistered
+     * Allows a new type converter to be registered
      *
      * @param toType        the type to convert to
      * @param fromType      the type to convert from

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java?rev=647575&r1=647574&r2=647575&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java
Sun Apr 13 08:06:22 2008
@@ -17,6 +17,9 @@
 package org.apache.camel.model.dataformat;
 
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAccessType;
 
 import org.apache.camel.impl.RouteContext;
 import org.apache.camel.spi.DataFormat;
@@ -27,9 +30,23 @@
  * @version $Revision$
  */
 @XmlRootElement(name = "string")
+@XmlAccessorType(XmlAccessType.FIELD)
 public class StringDataFormat extends DataFormatType {
+
+    @XmlAttribute(required = false)
+    private String charset;
+
     @Override
     protected DataFormat createDataFormat(RouteContext routeContext) {
-        return new org.apache.camel.impl.StringDataFormat();
+        return new org.apache.camel.impl.StringDataFormat(charset);
+    }
+
+    public String getCharset() {
+        return charset;
+    }
+
+    public void setCharset(String charset) {
+        this.charset = charset;
     }
+    
 }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java?rev=647575&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
Sun Apr 13 08:06:22 2008
@@ -0,0 +1,176 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl;
+
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelTemplate;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.TestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test of the string data format.
+ */
+public class StringDataFormatTest extends TestSupport {
+
+    private CamelContext context;
+    private CamelTemplate template;
+
+    protected void setUp() throws Exception {
+        context = new DefaultCamelContext();
+        template = new CamelTemplate(context);
+        template.start();
+    }
+
+    protected void tearDown() throws Exception {
+        template.stop();
+        context.stop();
+    }
+
+    public void testMarshalUTF8() throws Exception {
+        // NOTE: We are using a processor to do the assertions as the mock endpoint (Camel)
does not yet support
+        // type conversion using byte and strings where you can set a charset encoding
+
+        // include a UTF-8 char in the text \u0E08 is a Thai elephant
+        final String title = "Hello Thai Elephant \u0E08";
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").marshal().string("UTF-8").process(new MyBookProcessor("UTF-8",
title));
+            }
+        });
+        context.start();
+
+        MyBook book = new MyBook();
+        book.setTitle(title);
+
+        template.sendBody("direct:start", book);
+    }
+
+    public void testMarshalNoEncoding() throws Exception {
+        // NOTE: We are using a processor to do the assertions as the mock endpoint (Camel)
does not yet support
+        // type conversion using byte and strings where you can set a charset encoding
+
+        final String title = "Hello World";
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").marshal().string().process(new MyBookProcessor(null,
title));
+            }
+        });
+        context.start();
+
+        MyBook book = new MyBook();
+        book.setTitle(title);
+
+        template.sendBody("direct:start", book);
+    }
+
+
+    public void testUnmarshalUTF8() throws Exception {
+        // NOTE: Here we can use a MockEndpoint as we unmarshal the inputstream to String
+
+        // include a UTF-8 char in the text \u0E08 is a Thai elephant
+        final String title = "Hello Thai Elephant \u0E08";
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").unmarshal().string("UTF-8").to("mock:unmarshal");
+            }
+        });
+        context.start();
+
+        byte[] bytes = title.getBytes("UTF-8");
+        InputStream in = new ByteArrayInputStream(bytes);
+
+        template.sendBody("direct:start", in);
+
+        MockEndpoint mock = context.getEndpoint("mock:unmarshal", MockEndpoint.class);
+        mock.setExpectedMessageCount(1);
+        mock.expectedBodiesReceived(title);
+    }
+
+    public void testUnmarshalNoEncoding() throws Exception {
+        // NOTE: Here we can use a MockEndpoint as we unmarshal the inputstream to String
+
+        final String title = "Hello World";
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").unmarshal().string().to("mock:unmarshal");
+            }
+        });
+        context.start();
+
+        byte[] bytes = title.getBytes();
+        InputStream in = new ByteArrayInputStream(bytes);
+
+        template.sendBody("direct:start", in);
+
+        MockEndpoint mock = context.getEndpoint("mock:unmarshal", MockEndpoint.class);
+        mock.setExpectedMessageCount(1);
+        mock.expectedBodiesReceived(title);
+    }
+
+    private class MyBookProcessor implements Processor {
+
+        private String encoding;
+        private String title;
+
+        public MyBookProcessor(String encoding, String title) {
+            this.encoding = encoding;
+            this.title = title;
+        }
+
+        public void process(Exchange exchange) throws Exception {
+            byte[] body = exchange.getIn().getBody(byte[].class);
+
+            String text;
+            if (encoding != null) {
+                text = new String(body, encoding);
+            } else {
+                text = new String(body);
+            }
+
+            // does the testing
+            assertEquals(text, title);
+        }
+    }
+
+    private class MyBook {
+        private String title;
+
+        public String getTitle() {
+            return title;
+        }
+
+        public void setTitle(String title) {
+            this.title = title;
+        }
+
+        public String toString() {
+            // Camel will fallback to object toString converter and thus we get this text
+            return title;
+        }
+    }
+
+}



Mime
View raw message