camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nferr...@apache.org
Subject [camel-k] branch master updated: Add bindings for rest dsl in groovy/js routes #117
Date Mon, 24 Sep 2018 21:29:26 GMT
This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new e1312ed  Add bindings for rest dsl in groovy/js routes #117
e1312ed is described below

commit e1312edf6c9c9c413683892dfb6a673cf7926625
Author: lburgazzoli <lburgazzoli@gmail.com>
AuthorDate: Mon Sep 24 21:53:09 2018 +0200

    Add bindings for rest dsl in groovy/js routes #117
---
 runtime/examples/routes-rest.js                    | 48 ++++++++++++++++++++++
 .../java/org/apache/camel/k/jvm/RoutesLoaders.java | 36 ++++++++++++++--
 2 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/runtime/examples/routes-rest.js b/runtime/examples/routes-rest.js
new file mode 100644
index 0000000..47b9c24
--- /dev/null
+++ b/runtime/examples/routes-rest.js
@@ -0,0 +1,48 @@
+//
+// To run this integrations use:
+//
+//     kamel run --name=withrest --dependency=camel:undertow runtime/examples/routes-rest.js
+//
+
+// ****************
+//
+// Setup
+//
+// ****************
+
+l = components.get('log')
+l.exchangeFormatter = function(e) {
+    return "log - body=" + e.in.body + ", headers=" + e.in.headers
+}
+
+c = restConfiguration()
+c.component = 'undertow'
+c.port = 8081
+
+// ****************
+//
+// Functions
+//
+// ****************
+
+function proc(e) {
+    e.getIn().setHeader('RandomValue', Math.floor((Math.random() * 100) + 1))
+}
+
+// ****************
+//
+// Route
+//
+// ****************
+
+rest()
+    .path('/say/hello')
+    .get().route()
+        .transform().constant("Hello World");
+
+from('timer:js?period=1s')
+    .routeId('js')
+    .setBody()
+        .constant('Hello Camel K')
+    .process(proc)
+    .to('log:info')
\ No newline at end of file
diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java
index cc07cc2..5b27f99 100644
--- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java
+++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java
@@ -23,10 +23,12 @@ import java.nio.charset.StandardCharsets;
 import java.util.Collections;
 import java.util.List;
 import java.util.function.Function;
+import java.util.function.Supplier;
 import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.SimpleBindings;
+import javax.xml.bind.UnmarshalException;
 
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
@@ -35,12 +37,16 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.model.rest.RestConfigurationDefinition;
+import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ResourceHelper;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.codehaus.groovy.control.CompilerConfiguration;
 import org.joor.Reflect;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.apache.camel.k.jvm.Constants.SCHEME_CLASSPATH;
 import static org.apache.camel.k.jvm.Constants.SCHEME_FILE;
@@ -116,6 +122,8 @@ public enum RoutesLoaders implements RoutesLoader {
                     bindings.put("context", context);
                     bindings.put("components", new Components(context));
                     bindings.put("from", (Function<String, RouteDefinition>) uri ->
from(uri));
+                    bindings.put("rest", (Supplier<RestDefinition>) () -> rest());
+                    bindings.put("restConfiguration", (Supplier<RestConfigurationDefinition>)
() -> restConfiguration());
 
                     try (InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context,
resource)) {
                         engine.eval(new InputStreamReader(is), bindings);
@@ -165,15 +173,29 @@ public enum RoutesLoaders implements RoutesLoader {
                 @Override
                 public void configure() throws Exception {
                     try (InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getContext(),
resource)) {
-                        setRouteCollection(
-                            getContext().loadRoutesDefinition(is)
-                        );
+                        try {
+                            setRouteCollection(
+                                getContext().loadRoutesDefinition(is)
+                            );
+                        } catch (UnmarshalException e) {
+                            LOGGER.debug("Unable to load RoutesDefinition: {}", e.getMessage());
+                        }
+
+                        try {
+                            setRestCollection(
+                                getContext().loadRestsDefinition(is)
+                            );
+                        } catch (UnmarshalException e) {
+                            LOGGER.debug("Unbale to load RestsDefinition: {}", e.getMessage());
+                        }
                     }
                 }
             };
         }
     };
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(RoutesLoaders.class);
+
     public static RoutesLoader loaderFor(String resource, String languageName) {
         if (!resource.startsWith(SCHEME_CLASSPATH) && !resource.startsWith(SCHEME_FILE))
{
             throw new IllegalArgumentException("No valid resource format, expected scheme:path,
found " + resource);
@@ -242,5 +264,13 @@ public enum RoutesLoaders implements RoutesLoader {
         public RouteDefinition from(String endpoint) {
             return builder.from(endpoint);
         }
+
+        public RestDefinition rest() {
+            return builder.rest();
+        }
+
+        public RestConfigurationDefinition restConfiguration() {
+            return builder.restConfiguration();
+        }
     }
 }


Mime
View raw message