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/27 18:29:25 UTC

[8/8] git commit: CAMEL-7354: camel-spark component.

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/6f83cbeb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6f83cbeb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6f83cbeb

Branch: refs/heads/master
Commit: 6f83cbebda4c1054366e32eb1dd95262ae439b6f
Parents: f994a4e
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Jun 27 18:22:55 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Jun 27 18:25:30 2014 +0200

----------------------------------------------------------------------
 .../camel/component/spark/CamelSpark.java       | 84 ++++++++++++++++++
 .../camel/component/spark/SparkConsumer.java    | 17 ++--
 .../camel/component/spark/SparkEndpoint.java    | 14 ++-
 .../component/spark/SparkRouteBuilder.java      | 91 ++++++++++++++++++++
 .../spark/CamelSparkRouteBuilderPostTest.java   | 45 ++++++++++
 .../spark/CamelSparkRouteBuilderTest.java       | 45 ++++++++++
 6 files changed, 284 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6f83cbeb/components/camel-spark/src/main/java/org/apache/camel/component/spark/CamelSpark.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/main/java/org/apache/camel/component/spark/CamelSpark.java b/components/camel-spark/src/main/java/org/apache/camel/component/spark/CamelSpark.java
new file mode 100644
index 0000000..860ac5a
--- /dev/null
+++ b/components/camel-spark/src/main/java/org/apache/camel/component/spark/CamelSpark.java
@@ -0,0 +1,84 @@
+/**
+ * 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 spark.Route;
+import spark.Spark;
+
+public final class CamelSpark {
+
+    private CamelSpark() {
+    }
+
+    public static void spark(String verb, String path, String accept, Route route) {
+        if ("get".equals(verb)) {
+            if (accept != null) {
+                Spark.get(path, accept, route);
+            } else {
+                Spark.get(path, route);
+            }
+        } else if ("post".equals(verb)) {
+            if (accept != null) {
+                Spark.post(path, accept, route);
+            } else {
+                Spark.post(path, route);
+            }
+        } else if ("put".equals(verb)) {
+            if (accept != null) {
+                Spark.put(path, accept, route);
+            } else {
+                Spark.put(path, route);
+            }
+        } else if ("patch".equals(verb)) {
+            if (accept != null) {
+                Spark.patch(path, accept, route);
+            } else {
+                Spark.patch(path, route);
+            }
+        } else if ("delete".equals(verb)) {
+            if (accept != null) {
+                Spark.delete(path, accept, route);
+            } else {
+                Spark.delete(path, route);
+            }
+        } else if ("head".equals(verb)) {
+            if (accept != null) {
+                Spark.head(path, accept, route);
+            } else {
+                Spark.head(path, route);
+            }
+        } else if ("trace".equals(verb)) {
+            if (accept != null) {
+                Spark.trace(path, accept, route);
+            } else {
+                Spark.trace(path, route);
+            }
+        } else if ("connect".equals(verb)) {
+            if (accept != null) {
+                Spark.connect(path, accept, route);
+            } else {
+                Spark.connect(path, route);
+            }
+        } else if ("options".equals(verb)) {
+            if (accept != null) {
+                Spark.options(path, accept, route);
+            } else {
+                Spark.options(path, route);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6f83cbeb/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConsumer.java b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConsumer.java
index 16d350d..58390f0 100644
--- a/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConsumer.java
+++ b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkConsumer.java
@@ -19,7 +19,6 @@ package org.apache.camel.component.spark;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
-import spark.Spark;
 
 public class SparkConsumer extends DefaultConsumer {
 
@@ -39,20 +38,16 @@ public class SparkConsumer extends DefaultConsumer {
     protected void doStart() throws Exception {
         super.doStart();
 
-        String path = getEndpoint().getPath();
         String verb = getEndpoint().getVerb();
+        String path = getEndpoint().getPath();
         String accept = getEndpoint().getAccept();
 
-        // TODO: reuse our spark route builder DSL instead of this code
-
-        if ("get".equals(verb)) {
-            log.info("get(/{})", verb);
-            if (accept != null) {
-                Spark.get(path, accept, route);
-            } else {
-                Spark.get(path, route);
-            }
+        if (accept != null) {
+            log.info("Spark: {}({}) accepting: {}", new Object[]{verb, path, accept});
+        } else {
+            log.info("Spark: {}({})", verb, path);
         }
+        CamelSpark.spark(verb, path, accept, route);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/6f83cbeb/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkEndpoint.java b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkEndpoint.java
index 9d291ca..db88544 100644
--- a/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkEndpoint.java
+++ b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkEndpoint.java
@@ -23,6 +23,8 @@ import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
+import org.apache.camel.util.ObjectHelper;
+import spark.route.HttpMethod;
 
 @UriEndpoint(scheme = "spark", consumerClass =  SparkConsumer.class)
 public class SparkEndpoint extends DefaultEndpoint {
@@ -90,7 +92,6 @@ public class SparkEndpoint extends DefaultEndpoint {
     @Override
     public Consumer createConsumer(Processor processor) throws Exception {
         CamelSparkRoute route = new CamelSparkRoute(this, processor);
-
         Consumer consumer = new SparkConsumer(this, processor, route);
         configureConsumer(consumer);
         return consumer;
@@ -100,4 +101,15 @@ public class SparkEndpoint extends DefaultEndpoint {
     public boolean isSingleton() {
         return true;
     }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        ObjectHelper.notEmpty(verb, "verb", this);
+        ObjectHelper.notEmpty(path, "path", this);
+
+        // verb must be supported by Spark
+        HttpMethod.valueOf(verb);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/6f83cbeb/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkRouteBuilder.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkRouteBuilder.java b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkRouteBuilder.java
new file mode 100644
index 0000000..c00b276
--- /dev/null
+++ b/components/camel-spark/src/main/java/org/apache/camel/component/spark/SparkRouteBuilder.java
@@ -0,0 +1,91 @@
+/**
+ * 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.apache.camel.model.RouteDefinition;
+
+/**
+ * A Spark extended {@link org.apache.camel.builder.RouteBuilder} which allows to define routes using the Spark DSL.
+ */
+public abstract class SparkRouteBuilder extends RouteBuilder {
+
+    public RouteDefinition get(String path) {
+        return from("spark:get:" + path);
+    }
+
+    public RouteDefinition get(String path, String accept) {
+        return from("spark:get:" + path + "?accept=" + accept);
+    }
+
+    public RouteDefinition post(String path) {
+        return from("spark:post:" + path);
+    }
+
+    public RouteDefinition post(String path, String accept) {
+        return from("spark:post:" + path + "?accept=" + accept);
+    }
+
+    public RouteDefinition put(String path) {
+        return from("spark:post:" + path);
+    }
+
+    public RouteDefinition put(String path, String accept) {
+        return from("spark:post:" + path + "?accept=" + accept);
+    }
+
+    public RouteDefinition delete(String path) {
+        return from("spark:delete:" + path);
+    }
+
+    public RouteDefinition delete(String path, String accept) {
+        return from("spark:delete:" + path + "?accept=" + accept);
+    }
+
+    public RouteDefinition head(String path) {
+        return from("spark:head:" + path);
+    }
+
+    public RouteDefinition head(String path, String accept) {
+        return from("spark:head:" + path + "?accept=" + accept);
+    }
+
+    public RouteDefinition trace(String path) {
+        return from("spark:trace:" + path);
+    }
+
+    public RouteDefinition trace(String path, String accept) {
+        return from("spark:trace:" + path + "?accept=" + accept);
+    }
+
+    public RouteDefinition connect(String path) {
+        return from("spark:connect:" + path);
+    }
+
+    public RouteDefinition connect(String path, String accept) {
+        return from("spark:connect:" + path + "?accept=" + accept);
+    }
+
+    public RouteDefinition options(String path) {
+        return from("spark:options:" + path);
+    }
+
+    public RouteDefinition options(String path, String accept) {
+        return from("spark:options:" + path + "?accept=" + accept);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6f83cbeb/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderPostTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderPostTest.java b/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderPostTest.java
new file mode 100644
index 0000000..b455e1c
--- /dev/null
+++ b/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderPostTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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;
+
+public class CamelSparkRouteBuilderPostTest extends BaseSparkTest {
+
+    @Test
+    public void testSparkPost() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+        String out = template.requestBody("http://0.0.0.0:" + getPort() + "/hello", "I was here", String.class);
+        assertEquals("Bye I was here", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new SparkRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                post("hello")
+                    .to("mock:foo")
+                    .transform().simple("Bye ${body}");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6f83cbeb/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderTest.java b/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderTest.java
new file mode 100644
index 0000000..e670b062
--- /dev/null
+++ b/components/camel-spark/src/test/java/org/apache/camel/component/spark/CamelSparkRouteBuilderTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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;
+
+public class CamelSparkRouteBuilderTest extends BaseSparkTest {
+
+    @Test
+    public void testSparkGet() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+        String out = template.requestBody("http://0.0.0.0:" + getPort() + "/hello", null, String.class);
+        assertEquals("Bye World", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new SparkRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                get("hello")
+                    .to("mock:foo")
+                    .transform().constant("Bye World");
+            }
+        };
+    }
+}