camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [24/33] git commit: CAMEL-7354: migrate camel-spark-rest to the new Rest DSL
Date Sun, 20 Jul 2014 06:53:30 GMT
CAMEL-7354: migrate camel-spark-rest to the new Rest DSL


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

Branch: refs/heads/master
Commit: 6d40039c513627c9668fb12c3e63f07f2347d7ab
Parents: 81928f2
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Jul 16 15:25:23 2014 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Jul 19 09:38:52 2014 +0200

----------------------------------------------------------------------
 .../component/sparkrest/SparkComponent.java     | 11 +++
 .../component/sparkrest/SparkRouteBuilder.java  | 91 --------------------
 .../CamelSparkRouteBuilderPostTest.java         | 45 ----------
 .../sparkrest/CamelSparkRouteBuilderTest.java   | 45 ----------
 .../sparkrest/RestCamelSparkAcceptTest.java     | 34 ++++++++
 .../sparkrest/RestCamelSparkParamTest.java      | 35 ++++++++
 .../RestCamelSparkRequestBeanTest.java          | 35 ++++++++
 .../sparkrest/RestCamelSparkSplatTest.java      | 34 ++++++++
 8 files changed, 149 insertions(+), 181 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6d40039c/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
index df58a7c..d055ce0 100644
--- a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
+++ b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java
@@ -17,6 +17,8 @@
 package org.apache.camel.component.sparkrest;
 
 import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Consumer;
@@ -30,6 +32,8 @@ import spark.SparkBase;
 
 public class SparkComponent extends UriEndpointComponent implements RestConsumerFactory {
 
+    private final Pattern pattern = Pattern.compile("\\{(.*?)\\}");
+
     private int port = SparkBase.SPARK_DEFAULT_PORT;
     private SparkConfiguration sparkConfiguration = new SparkConfiguration();
     private SparkBinding sparkBinding = new DefaultSparkBinding();
@@ -100,6 +104,13 @@ public class SparkComponent extends UriEndpointComponent implements RestConsumer
     @Override
     public Consumer createConsumer(CamelContext camelContext, Processor processor,
                                    String verb, String path, String consumes, Map<String,
Object> parameters) throws Exception {
+
+        if (ObjectHelper.isNotEmpty(path)) {
+            // spark-rest uses :name syntax instead of {name} so we need to replace those
+            Matcher matcher = pattern.matcher(path);
+            path = matcher.replaceAll(":$1");
+        }
+
         // get the endpoint
         SparkEndpoint endpoint;
         if (consumes != null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/6d40039c/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkRouteBuilder.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkRouteBuilder.java
b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkRouteBuilder.java
deleted file mode 100644
index 2265da8..0000000
--- a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkRouteBuilder.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/**
- * 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.sparkrest;
-
-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-rest:get:" + path);
-    }
-
-    public RouteDefinition get(String path, String accept) {
-        return from("spark-rest:get:" + path + "?accept=" + accept);
-    }
-
-    public RouteDefinition post(String path) {
-        return from("spark-rest:post:" + path);
-    }
-
-    public RouteDefinition post(String path, String accept) {
-        return from("spark-rest:post:" + path + "?accept=" + accept);
-    }
-
-    public RouteDefinition put(String path) {
-        return from("spark-rest:post:" + path);
-    }
-
-    public RouteDefinition put(String path, String accept) {
-        return from("spark-rest:post:" + path + "?accept=" + accept);
-    }
-
-    public RouteDefinition delete(String path) {
-        return from("spark-rest:delete:" + path);
-    }
-
-    public RouteDefinition delete(String path, String accept) {
-        return from("spark-rest:delete:" + path + "?accept=" + accept);
-    }
-
-    public RouteDefinition head(String path) {
-        return from("spark-rest:head:" + path);
-    }
-
-    public RouteDefinition head(String path, String accept) {
-        return from("spark-rest:head:" + path + "?accept=" + accept);
-    }
-
-    public RouteDefinition trace(String path) {
-        return from("spark-rest:trace:" + path);
-    }
-
-    public RouteDefinition trace(String path, String accept) {
-        return from("spark-rest:trace:" + path + "?accept=" + accept);
-    }
-
-    public RouteDefinition connect(String path) {
-        return from("spark-rest:connect:" + path);
-    }
-
-    public RouteDefinition connect(String path, String accept) {
-        return from("spark-rest:connect:" + path + "?accept=" + accept);
-    }
-
-    public RouteDefinition options(String path) {
-        return from("spark-rest:options:" + path);
-    }
-
-    public RouteDefinition options(String path, String accept) {
-        return from("spark-rest:options:" + path + "?accept=" + accept);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/6d40039c/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderPostTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderPostTest.java
b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderPostTest.java
deleted file mode 100644
index 8d0ff4a..0000000
--- a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderPostTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * 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.sparkrest;
-
-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/6d40039c/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderTest.java
b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderTest.java
deleted file mode 100644
index 56c7301..0000000
--- a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CamelSparkRouteBuilderTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * 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.sparkrest;
-
-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");
-            }
-        };
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/6d40039c/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkAcceptTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkAcceptTest.java
b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkAcceptTest.java
new file mode 100644
index 0000000..70b7636
--- /dev/null
+++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkAcceptTest.java
@@ -0,0 +1,34 @@
+/**
+ * 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.sparkrest;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class RestCamelSparkAcceptTest extends CamelSparkAcceptTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                rest().path("/hello").get().consumes("application/json")
+                    .to("mock:foo")
+                    .transform().constant("{ \"reply\": \"Bye World\" }");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6d40039c/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkParamTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkParamTest.java
b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkParamTest.java
new file mode 100644
index 0000000..4133704
--- /dev/null
+++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkParamTest.java
@@ -0,0 +1,35 @@
+/**
+ * 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.sparkrest;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class RestCamelSparkParamTest extends CamelSparkParamTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                rest().path("/hello/{name}").get()
+                    .to("mock:foo")
+                    .transform().simple("Bye ${header.name}");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6d40039c/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkRequestBeanTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkRequestBeanTest.java
b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkRequestBeanTest.java
new file mode 100644
index 0000000..f881f53
--- /dev/null
+++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkRequestBeanTest.java
@@ -0,0 +1,35 @@
+/**
+ * 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.sparkrest;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class RestCamelSparkRequestBeanTest extends CamelSparkRequestBeanTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                rest().path("/hello/*/to/*").get()
+                    .to("mock:foo")
+                    .bean(RestCamelSparkRequestBeanTest.class, "doSomething");
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6d40039c/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkSplatTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkSplatTest.java
b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkSplatTest.java
new file mode 100644
index 0000000..fefb211
--- /dev/null
+++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkSplatTest.java
@@ -0,0 +1,34 @@
+/**
+ * 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.sparkrest;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class RestCamelSparkSplatTest extends CamelSparkSplatTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                rest().path("/hello/*/to/*").get()
+                    .to("mock:foo")
+                    .transform().simple("Bye big ${header.splat[1]} from ${header.splat[0]}");
+            }
+        };
+    }
+}


Mime
View raw message