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 2014/06/28 10:11:09 UTC

git commit: CAMEL-7354: camel-spark component.

Repository: camel
Updated Branches:
  refs/heads/master a4c8f76ca -> 1d11fff83


CAMEL-7354: camel-spark component.


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

Branch: refs/heads/master
Commit: 1d11fff833b07ed7ae8a23f175992763919dc038
Parents: a4c8f76
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Jun 28 09:12:04 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Jun 28 10:10:59 2014 +0200

----------------------------------------------------------------------
 .../camel/component/spark/SparkConverter.java   | 79 ++++++++++++++++++++
 .../services/org/apache/camel/TypeConverter     | 18 +++++
 .../spark/CamelSparkRequestBeanTest.java        | 50 +++++++++++++
 3 files changed, 147 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1d11fff8/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConverter.java b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConverter.java
new file mode 100644
index 0000000..087d5fa
--- /dev/null
+++ b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConverter.java
@@ -0,0 +1,79 @@
+/**
+ * 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.spark;
+
+import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
+import org.apache.camel.spi.TypeConverterRegistry;
+import spark.Request;
+import spark.Response;
+
+@Converter
+public final class SparkConverter {
+
+    /**
+     * A fallback converter that allows us to easily call Java beans and use the raw Spark {@link Request} as parameter types.
+     */
+    @FallbackConverter
+    public static Object convertToRequest(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
+        // if we want to covert to Request
+        if (value != null && Request.class.isAssignableFrom(type)) {
+
+            // okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the NettyHttpMessage
+            // so if the message instance is a NettyHttpMessage and its body is the value, then we can grab the
+            // HttpRequest from the NettyHttpMessage
+            SparkMessage msg;
+            if (exchange.hasOut()) {
+                msg = exchange.getOut(SparkMessage.class);
+            } else {
+                msg = exchange.getIn(SparkMessage.class);
+            }
+            if (msg != null) {
+                return msg.getRequest();
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * A fallback converter that allows us to easily call Java beans and use the raw Spark {@link Response} as parameter types.
+     */
+    @FallbackConverter
+    public static Object convertToResponse(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
+        // if we want to covert to Response
+        if (value != null && Response.class.isAssignableFrom(type)) {
+
+            // okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the NettyHttpMessage
+            // so if the message instance is a NettyHttpMessage and its body is the value, then we can grab the
+            // HttpRequest from the NettyHttpMessage
+            SparkMessage msg;
+            if (exchange.hasOut()) {
+                msg = exchange.getOut(SparkMessage.class);
+            } else {
+                msg = exchange.getIn(SparkMessage.class);
+            }
+            if (msg != null) {
+                return msg.getResponse();
+            }
+        }
+
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1d11fff8/components/camel-spark/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/main/resources/META-INF/services/org/apache/camel/TypeConverter b/components/camel-spark/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
new file mode 100644
index 0000000..c2d382d
--- /dev/null
+++ b/components/camel-spark/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.camel.component.spark.SparkConverter
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1d11fff8/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRequestBeanTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRequestBeanTest.java b/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRequestBeanTest.java
new file mode 100644
index 0000000..5c7436d
--- /dev/null
+++ b/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRequestBeanTest.java
@@ -0,0 +1,50 @@
+/**
+ * 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.spark;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+import spark.Request;
+
+public class CamelSparkRequestBeanTest extends BaseSparkTest {
+
+    @Test
+    public void testSparkGet() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+        String out = template.requestBody("http://0.0.0.0:" + getPort() + "/hello/camel/to/world", null, String.class);
+        assertEquals("Bye big world from camel", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("spark:get:/hello/*/to/*")
+                        .to("mock:foo")
+                        .bean(CamelSparkRequestBeanTest.class, "doSomething");
+            }
+        };
+    }
+
+    public String doSomething(Request request) {
+        return "Bye big " + request.splat()[1] + " from " + request.splat()[0];
+    }
+}