You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/10/03 21:33:54 UTC

[GitHub] lburgazzoli closed pull request #152: runtime: improve rest dsl for groovy and kotlin runtimes

lburgazzoli closed pull request #152: runtime: improve rest dsl for groovy and kotlin runtimes
URL: https://github.com/apache/camel-k/pull/152
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/runtime/examples/camel-caffeine.groovy b/runtime/examples/camel-caffeine.groovy
index d1aada7..84da8fc 100644
--- a/runtime/examples/camel-caffeine.groovy
+++ b/runtime/examples/camel-caffeine.groovy
@@ -6,38 +6,40 @@
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.Caffeine;
 
-  Cache cache = Caffeine.newBuilder().recordStats().build();
+Cache cache = Caffeine.newBuilder().recordStats().build();
 
-  registry {
-      bind 'caffeineCache', cache
-  }
+context {
+    registry {
+        bind 'caffeineCache', cache
+    }
+}
 
-  from('timer:tick')
-      .setBody().constant('Hello')
-      .process {
-        it.in.headers['CamelCaffeineAction'] = 'PUT'
-        it.in.headers['CamelCaffeineKey'] = 1
-      }
-      .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
-      .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}')
-      .setBody().constant(null)
-      .process {
-        it.in.headers['CamelCaffeineAction'] = 'GET'
-        it.in.headers['CamelCaffeineKey'] = 1
-      }
-      .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
-      .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}')
-      .setBody().constant(null)
-      .process {
-        it.in.headers['CamelCaffeineAction'] = 'INVALIDATE'
-        it.in.headers['CamelCaffeineKey'] = 1
-      }
-      .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
-      .log('Invalidating entry with key ${header.CamelCaffeineKey}')
-      .setBody().constant(null)
-      .process {
-        it.in.headers['CamelCaffeineAction'] = 'GET'
-        it.in.headers['CamelCaffeineKey'] = 1
-      }
-      .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
-      .log('The Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} has result? ${header.CamelCaffeineActionHasResult}');
+from('timer:tick')
+  .setBody().constant('Hello')
+  .process {
+    it.in.headers['CamelCaffeineAction'] = 'PUT'
+    it.in.headers['CamelCaffeineKey'] = 1
+  }
+  .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
+  .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}')
+  .setBody().constant(null)
+  .process {
+    it.in.headers['CamelCaffeineAction'] = 'GET'
+    it.in.headers['CamelCaffeineKey'] = 1
+  }
+  .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
+  .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}')
+  .setBody().constant(null)
+  .process {
+    it.in.headers['CamelCaffeineAction'] = 'INVALIDATE'
+    it.in.headers['CamelCaffeineKey'] = 1
+  }
+  .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
+  .log('Invalidating entry with key ${header.CamelCaffeineKey}')
+  .setBody().constant(null)
+  .process {
+    it.in.headers['CamelCaffeineAction'] = 'GET'
+    it.in.headers['CamelCaffeineKey'] = 1
+  }
+  .toF('caffeine-cache://%s?cache=#caffeineCache', 'test')
+  .log('The Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} has result? ${header.CamelCaffeineActionHasResult}');
diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy
index 5fccd30..5b07bd5 100644
--- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy
@@ -24,8 +24,6 @@ import org.apache.camel.builder.RouteBuilder
 import org.apache.camel.k.jvm.RuntimeRegistry
 import org.apache.camel.k.jvm.dsl.Components
 import org.apache.camel.model.RouteDefinition
-import org.apache.camel.model.rest.RestConfigurationDefinition
-import org.apache.camel.model.rest.RestDefinition
 
 class IntegrationConfiguration {
     private final RuntimeRegistry registry
@@ -47,34 +45,14 @@ class IntegrationConfiguration {
         callable.call()
     }
 
-    RouteDefinition from(String endpoint) {
-        return builder.from(endpoint)
-    }
-
-    RestDefinition rest() {
-        return builder.rest()
-    }
-
     def rest(Closure<?> callable) {
         callable.resolveStrategy = Closure.DELEGATE_FIRST
-        callable.delegate = builder.rest()
+        callable.delegate = new RestConfiguration(builder)
         callable.call()
     }
 
-    RestConfigurationDefinition restConfiguration() {
-        return builder.restConfiguration()
-    }
-
-    def restConfiguration(Closure<?> callable) {
-        callable.resolveStrategy = Closure.DELEGATE_FIRST
-        callable.delegate = builder.restConfiguration()
-        callable.call()
-    }
-
-    def restConfiguration(String component, Closure<?> callable) {
-        callable.resolveStrategy = Closure.DELEGATE_FIRST
-        callable.delegate = builder.restConfiguration(component)
-        callable.call()
+    RouteDefinition from(String endpoint) {
+        return builder.from(endpoint)
     }
 
     def processor(Closure<?> callable) {
@@ -87,7 +65,6 @@ class IntegrationConfiguration {
         }
     }
 
-
     def predicate(Closure<?> callable) {
         return new Predicate() {
             @Override
diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RestConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RestConfiguration.groovy
new file mode 100644
index 0000000..d0b2fcf
--- /dev/null
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RestConfiguration.groovy
@@ -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.k.groovy.dsl
+
+import org.apache.camel.builder.RouteBuilder
+
+class RestConfiguration {
+    private final RouteBuilder builder
+
+    RestConfiguration(RouteBuilder builder) {
+        this.builder = builder
+    }
+
+    def configuration(Closure<?> callable) {
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
+        callable.delegate = builder.restConfiguration()
+        callable.call()
+    }
+
+    def configuration(String component, Closure<?> callable) {
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
+        callable.delegate = builder.restConfiguration(component)
+        callable.call()
+    }
+
+    def path(String path, Closure<?> callable) {
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
+        callable.delegate = builder.rest(path)
+        callable.call()
+    }
+}
diff --git a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy b/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy
index 3572e95..4e2fdfe 100644
--- a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy
+++ b/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy
@@ -46,6 +46,8 @@ class IntegrationTest extends Specification {
         runtime.camelContext.restConfiguration.port == 9192
         runtime.camelContext.getRestConfiguration('undertow', false).host == 'my-undertow-host'
         runtime.camelContext.getRestConfiguration('undertow', false).port == 9193
+        runtime.camelContext.restDefinitions.size() == 1
+        runtime.camelContext.restDefinitions[0].path == '/my/path'
     }
 
     def "load integration with bindings"()  {
diff --git a/runtime/groovy/src/test/resources/routes-with-rest.groovy b/runtime/groovy/src/test/resources/routes-with-rest.groovy
index f0187e4..6df9367 100644
--- a/runtime/groovy/src/test/resources/routes-with-rest.groovy
+++ b/runtime/groovy/src/test/resources/routes-with-rest.groovy
@@ -1,12 +1,18 @@
 
-restConfiguration {
-    host 'my-host'
-    port '9192'
-}
+rest {
+    configuration {
+        host 'my-host'
+        port '9192'
+    }
+
+    configuration('undertow') {
+        host 'my-undertow-host'
+        port '9193'
+    }
+
+    path('/my/path') {
 
-restConfiguration('undertow') {
-    host 'my-undertow-host'
-    port '9193'
+    }
 }
 
 from('timer:tick')
diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt
index 804664b..62e8369 100644
--- a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt
+++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt
@@ -49,18 +49,8 @@ class KotlinRoutesLoader : RoutesLoader {
                     val pre = """
                         val builder = bindings["builder"] as org.apache.camel.builder.RouteBuilder
 
-                        fun rest(block: org.apache.camel.model.rest.RestDefinition.() -> Unit) {
-                            val delegate = builder.rest()
-                            delegate.block()
-                        }
-
-                        fun restConfiguration(block: org.apache.camel.model.rest.RestConfigurationDefinition.() -> Unit) {
-                            val delegate = builder.restConfiguration()
-                            delegate.block()
-                        }
-
-                        fun restConfiguration(component: String, block: org.apache.camel.model.rest.RestConfigurationDefinition.() -> Unit) {
-                            val delegate = builder.restConfiguration(component)
+                        fun rest(block: org.apache.camel.k.kotlin.dsl.RestConfiguration.() -> Unit) {
+                            val delegate = org.apache.camel.k.kotlin.dsl.RestConfiguration(builder)
                             delegate.block()
                         }
 
diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RestConfiguration.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RestConfiguration.kt
new file mode 100644
index 0000000..9f9f490
--- /dev/null
+++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RestConfiguration.kt
@@ -0,0 +1,39 @@
+/**
+ * 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.k.kotlin.dsl
+
+import org.apache.camel.builder.RouteBuilder
+import org.apache.camel.model.rest.RestConfigurationDefinition
+import org.apache.camel.model.rest.RestDefinition
+
+class RestConfiguration(val builder: RouteBuilder) {
+
+    fun configuration(block: RestConfigurationDefinition.() -> Unit) {
+        val delegate = builder.restConfiguration()
+        delegate.block()
+    }
+
+    fun configuration(component: String, block: RestConfigurationDefinition.() -> Unit) {
+        val delegate = builder.restConfiguration(component)
+        delegate.block()
+    }
+
+    fun path(path: String, block: RestDefinition.() -> Unit) {
+        val delegate = builder.rest(path)
+        delegate.block()
+    }
+}
\ No newline at end of file
diff --git a/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt b/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt
index 4e56d34..d35bc61 100644
--- a/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt
+++ b/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt
@@ -29,6 +29,8 @@ class IntegrationTest {
         assertThat(runtime.camelContext.restConfiguration.port).isEqualTo(9192)
         assertThat(runtime.camelContext.getRestConfiguration("undertow", false).host).isEqualTo("my-undertow-host")
         assertThat(runtime.camelContext.getRestConfiguration("undertow", false).port).isEqualTo(9193)
+        assertThat(runtime.camelContext.restDefinitions.size).isEqualTo(1)
+        assertThat(runtime.camelContext.restDefinitions[0].path).isEqualTo("/my/path")
     }
 
     @Test
diff --git a/runtime/kotlin/src/test/resources/routes-with-rest.kts b/runtime/kotlin/src/test/resources/routes-with-rest.kts
index 29a748f..c94ccb5 100644
--- a/runtime/kotlin/src/test/resources/routes-with-rest.kts
+++ b/runtime/kotlin/src/test/resources/routes-with-rest.kts
@@ -1,12 +1,17 @@
 
-restConfiguration {
-    host = "my-host"
-    port = "9192"
-}
+rest {
+    configuration {
+        host = "my-host"
+        port = "9192"
+    }
 
-restConfiguration("undertow") {
-    host = "my-undertow-host"
-    port = "9193"
+    configuration("undertow") {
+        host = "my-undertow-host"
+        port = "9193"
+    }
+
+    path("/my/path") {
+    }
 }
 
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services