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 2013/06/14 11:04:51 UTC

git commit: CAMEL-6327: Added xml type converters to Netty.

Updated Branches:
  refs/heads/master 3c59d3f2a -> cd7714207


CAMEL-6327: Added xml type converters to Netty.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cd771420
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cd771420
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cd771420

Branch: refs/heads/master
Commit: cd77142078b960a31f59791b35ccd3cbe618c317
Parents: 3c59d3f
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Jun 14 11:04:39 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Jun 14 11:04:39 2013 +0200

----------------------------------------------------------------------
 .../netty/http/NettyHttpXMLXPathTest.java       | 51 ++++++++++++++++++++
 .../camel/component/netty/NettyConverter.java   | 50 ++++++++++++++++---
 2 files changed, 94 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/cd771420/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java
new file mode 100644
index 0000000..6fdfd2e
--- /dev/null
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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.component.netty.http;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class NettyHttpXMLXPathTest extends BaseNettyTest {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        return super.createCamelContext();
+    }
+
+    @Test
+    public void testHttpXML() throws Exception {
+        String out = template.requestBody("netty-http:http://localhost:{{port}}/foo", "<person><name>Claus</name></person>", String.class);
+        assertEquals("<quote>Camel rocks</quote>", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("netty-http:http://0.0.0.0:{{port}}/foo")
+                    .choice()
+                        .when().xpath("/person/name = 'Claus'")
+                            .transform(constant("<quote>Camel rocks</quote>"))
+                        .otherwise()
+                            .transform(constant("<quote>Try Camel now</quote>"));
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/cd771420/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConverter.java b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConverter.java
index 0638138..eaebbe3 100644
--- a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConverter.java
+++ b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConverter.java
@@ -21,12 +21,17 @@ import java.io.InputStream;
 import java.io.ObjectInput;
 import java.io.ObjectInputStream;
 import java.io.UnsupportedEncodingException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
+import javax.xml.transform.stream.StreamSource;
 
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
 import org.jboss.netty.buffer.ChannelBuffer;
 import org.jboss.netty.buffer.ChannelBufferInputStream;
 import org.jboss.netty.buffer.ChannelBuffers;
+import org.w3c.dom.Document;
 
 /**
  * A set of converter methods for working with Netty types
@@ -41,13 +46,13 @@ public final class NettyConverter {
     }
 
     @Converter
-    public static byte[] toByteArray(ChannelBuffer buffer) {
+    public static byte[] toByteArray(ChannelBuffer buffer, Exchange exchange) {
         return buffer.array();
     }
 
     @Converter
     public static String toString(ChannelBuffer buffer, Exchange exchange) throws UnsupportedEncodingException {
-        byte[] bytes = toByteArray(buffer);
+        byte[] bytes = toByteArray(buffer, exchange);
         // use type converter as it can handle encoding set on the Exchange
         if (exchange != null) {
             return exchange.getContext().getTypeConverter().convertTo(String.class, exchange, bytes);
@@ -56,18 +61,18 @@ public final class NettyConverter {
     }
 
     @Converter
-    public static InputStream toInputStream(ChannelBuffer buffer) {
+    public static InputStream toInputStream(ChannelBuffer buffer, Exchange exchange) {
         return new ChannelBufferInputStream(buffer);
     }
 
     @Converter
-    public static ObjectInput toObjectInput(ChannelBuffer buffer) throws IOException {
-        InputStream is = toInputStream(buffer);
+    public static ObjectInput toObjectInput(ChannelBuffer buffer, Exchange exchange) throws IOException {
+        InputStream is = toInputStream(buffer, exchange);
         return new ObjectInputStream(is);
     }
 
     @Converter
-    public static ChannelBuffer toByteBuffer(byte[] bytes) {
+    public static ChannelBuffer toByteBuffer(byte[] bytes, Exchange exchange) {
         ChannelBuffer buf = ChannelBuffers.dynamicBuffer(bytes.length);
         buf.writeBytes(bytes);
         return buf;
@@ -82,6 +87,37 @@ public final class NettyConverter {
         } else {
             bytes = s.getBytes();
         }
-        return toByteBuffer(bytes);
+        return toByteBuffer(bytes, exchange);
     }
+
+    @Converter
+    public static Document toDocument(ChannelBuffer buffer, Exchange exchange) {
+        InputStream is = toInputStream(buffer, exchange);
+        return exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, is);
+    }
+
+    @Converter
+    public static DOMSource toDOMSource(ChannelBuffer buffer, Exchange exchange) {
+        InputStream is = toInputStream(buffer, exchange);
+        return exchange.getContext().getTypeConverter().convertTo(DOMSource.class, exchange, is);
+    }
+
+    @Converter
+    public static SAXSource toSAXSource(ChannelBuffer buffer, Exchange exchange) {
+        InputStream is = toInputStream(buffer, exchange);
+        return exchange.getContext().getTypeConverter().convertTo(SAXSource.class, exchange, is);
+    }
+
+    @Converter
+    public static StreamSource toStreamSource(ChannelBuffer buffer, Exchange exchange) {
+        InputStream is = toInputStream(buffer, exchange);
+        return exchange.getContext().getTypeConverter().convertTo(StreamSource.class, exchange, is);
+    }
+
+    @Converter
+    public static StAXSource toStAXSource(ChannelBuffer buffer, Exchange exchange) {
+        InputStream is = toInputStream(buffer, exchange);
+        return exchange.getContext().getTypeConverter().convertTo(StAXSource.class, exchange, is);
+    }
+
 }