camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r771351 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/processor/ camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/processor/ components...
Date Mon, 04 May 2009 16:28:59 GMT
Author: davsclaus
Date: Mon May  4 16:28:57 2009
New Revision: 771351

URL: http://svn.apache.org/viewvc?rev=771351&view=rev
Log:
CAMEL-1573: Added charset option to convertBodyTo. And fixed bug loading byte[] classes using
loadClass.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/convertBody.xml

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java?rev=771351&r1=771350&r2=771351&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java
Mon May  4 16:28:57 2009
@@ -39,6 +39,8 @@
 public class ConvertBodyDefinition extends ProcessorDefinition<ProcessorDefinition>
{
     @XmlAttribute
     private String type;
+    @XmlAttribute(required = false)
+    private String charset;
     @XmlTransient
     private Class typeClass;
 
@@ -54,6 +56,12 @@
         setType(typeClass.getName());
     }
 
+    public ConvertBodyDefinition(Class typeClass, String charset) {
+        setTypeClass(typeClass);
+        setType(typeClass.getName());
+        setCharset(charset);
+    }
+
     @Override
     public String toString() {        
         return "convertBodyTo[" + getType() + "]";
@@ -66,7 +74,7 @@
 
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
-        return new ConvertBodyProcessor(getTypeClass());
+        return new ConvertBodyProcessor(getTypeClass(), getCharset());
     }
 
     @Override
@@ -91,6 +99,14 @@
         this.typeClass = typeClass;
     }
 
+    public String getCharset() {
+        return charset;
+    }
+
+    public void setCharset(String charset) {
+        this.charset = charset;
+    }
+
     public Class getTypeClass() {
         if (typeClass == null) {
             Class clazz = createTypeClass();
@@ -102,27 +118,4 @@
         return typeClass;
     }
     
-    // Fluent API
-    //-------------------------------------------------------------------------
-    /**
-     * Sets the type class that you want ConvertBodyType to covert
-     *
-     * @param typeClass  the type class that you want to covert body instance to
-     * @return the builder
-     */
-    public ConvertBodyDefinition typeClass(Class typeClass) {
-        setTypeClass(typeClass);
-        return this;
-    }
-    
-    /**
-     * Sets the type class name that you want to covert body instance to
-     *
-     * @param type  the type class name
-     * @return the builder
-     */
-    public ConvertBodyDefinition type(String type) {
-        setType(type);
-        return this;
-    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=771351&r1=771350&r2=771351&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Mon
May  4 16:28:57 2009
@@ -1744,14 +1744,15 @@
     }
     
     /**
-     * Converts the IN message body to the specified class type
+     * Converts the IN message body to the specified type
      *
-     * @param typeString the type to convert to as a fully qualified classname
+     * @param type the type to convert to
+     * @param charset the charset to use by type converters (not all converters support specifc
charset)
      * @return the builder
      */
     @SuppressWarnings("unchecked")
-    public Type convertBodyTo(String typeString) {
-        addOutput(new ConvertBodyDefinition(typeString));
+    public Type convertBodyTo(Class type, String charset) {
+        addOutput(new ConvertBodyDefinition(type, charset));
         return (Type) this;
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java?rev=771351&r1=771350&r2=771351&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
Mon May  4 16:28:57 2009
@@ -29,9 +29,16 @@
  */
 public class ConvertBodyProcessor implements Processor {
     private final Class type;
+    private final String charset;
 
     public ConvertBodyProcessor(Class type) {
         this.type = type;
+        this.charset = null;
+    }
+
+    public ConvertBodyProcessor(Class type, String charset) {
+        this.type = type;
+        this.charset = charset;
     }
 
     @Override
@@ -41,7 +48,10 @@
 
     @SuppressWarnings("unchecked")
     public void process(Exchange exchange) throws Exception {
-        Message in = exchange.getIn();        
+        Message in = exchange.getIn();
+        if (charset != null) {
+            exchange.setProperty(Exchange.CHARSET_NAME, charset);
+        }
         Object value = in.getMandatoryBody(type);
 
         if (exchange.getPattern().isOutCapable()) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=771351&r1=771350&r2=771351&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Mon May 
4 16:28:57 2009
@@ -508,6 +508,11 @@
         // must clean the name so its pure java name, eg remoing \n or whatever people can
do in the Spring XML
         name = normalizeClassName(name);
 
+        // special for byte[] as its common to use
+        if ("java.lang.byte[]".equals(name) || "byte[]".equals(name)) {
+            return byte[].class;
+        }
+
         // try context class loader first
         Class clazz = doLoadClass(name, Thread.currentThread().getContextClassLoader());
         if (clazz == null) {
@@ -894,7 +899,7 @@
     public static String normalizeClassName(String name) {
         StringBuffer sb = new StringBuffer(name.length());
         for (char ch : name.toCharArray()) {
-            if (ch == '.' || Character.isJavaIdentifierPart(ch)) {
+            if (ch == '.' || ch == '[' || ch == ']' || Character.isJavaIdentifierPart(ch))
{
                 sb.append(ch);
             }
         }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java?rev=771351&r1=771350&r2=771351&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java Mon
May  4 16:28:57 2009
@@ -50,6 +50,29 @@
         assertMockEndpointsSatisfied();
     }
 
+    public void testConvertCharset() throws Exception {
+        byte[] body = "Hello World".getBytes("iso-8859-1");
+
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.expectedBodiesReceived(body);
+
+        template.sendBody("direct:charset", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testConvertCharsetFail() throws Exception {
+        byte[] body = "Hello World".getBytes("utf-8");
+
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.expectedBodiesReceived(body);
+
+        template.sendBody("direct:charset2", "Hello World");
+
+        // should NOT be okay as we expected utf-8 but got it in utf-16
+        result.assertIsNotSatisfied();
+    }
+
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
@@ -58,6 +81,10 @@
                 from("direct:start").convertBodyTo(Integer.class).to("mock:result");
 
                 from("direct:invalid").convertBodyTo(Date.class).to("mock:result");
+
+                from("direct:charset").convertBodyTo(byte[].class, "iso-8859-1").to("mock:result");
+
+                from("direct:charset2").convertBodyTo(byte[].class, "utf-16").to("mock:result");
             }
         };
     }

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/convertBody.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/convertBody.xml?rev=771351&r1=771350&r2=771351&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/convertBody.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/convertBody.xml
Mon May  4 16:28:57 2009
@@ -22,27 +22,42 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-  <!-- START SNIPPET: example -->
-  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
-	<route>
-		<from uri="direct:start" />
-		<convertBodyTo type="java.lang.Integer" />
-		<to uri="mock:result" />
-	</route>
-	<route errorHandlerRef="deadLetterErrorHandler">
-		<from uri="direct:invalid" />
-		<convertBodyTo type="java.util.Date" />
-		<to uri="mock:result" />
-	</route>
-  </camelContext>
-  
-  <bean id="deadLetterErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">
-  	<property name="deadLetterUri" value="mock:dead"/>
-  	<property name="redeliveryPolicy" ref="redeliveryPolicy"/>
-  </bean>
-  
-   <bean id="redeliveryPolicy" class="org.apache.camel.processor.RedeliveryPolicy">
       
+    <!-- START SNIPPET: example -->
+    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+
+        <route>
+            <from uri="direct:start"/>
+            <convertBodyTo type="java.lang.Integer"/>
+            <to uri="mock:result"/>
+        </route>
+
+        <route errorHandlerRef="deadLetterErrorHandler">
+            <from uri="direct:invalid"/>
+            <convertBodyTo type="java.util.Date"/>
+            <to uri="mock:result"/>
+        </route>
+
+        <route>
+            <from uri="direct:charset"/>
+            <convertBodyTo type="java.lang.byte[]" charset="iso-8859-1"/>
+            <to uri="mock:result"/>
+        </route>
+
+        <route>
+            <from uri="direct:charset2"/>
+            <convertBodyTo type="java.lang.byte[]" charset="utf-16"/>
+            <to uri="mock:result"/>
+        </route>
+
+    </camelContext>
+
+    <bean id="deadLetterErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">
+        <property name="deadLetterUri" value="mock:dead"/>
+        <property name="redeliveryPolicy" ref="redeliveryPolicy"/>
+    </bean>
+
+    <bean id="redeliveryPolicy" class="org.apache.camel.processor.RedeliveryPolicy">
         <property name="maximumRedeliveries" value="0"/>
     </bean>
-  <!-- END SNIPPET: example -->
+    <!-- END SNIPPET: example -->
 </beans>



Mime
View raw message