diff --git a/runtime/examples/routes-rest.js b/runtime/examples/routes-rest.js new file mode 100644 index 0000000000..47b9c24f75 --- /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 cc07cc251f..5b27f99f84 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.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.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 void configure() throws Exception { bindings.put("context", context); bindings.put("components", new Components(context)); bindings.put("from", (Function) uri -> from(uri)); + bindings.put("rest", (Supplier) () -> rest()); + bindings.put("restConfiguration", (Supplier) () -> restConfiguration()); try (InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, resource)) { engine.eval(new InputStreamReader(is), bindings); @@ -165,15 +173,29 @@ public RouteBuilder load(String resource) throws Exception { @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 ScriptingDsl(RouteBuilder builder) { public RouteDefinition from(String endpoint) { return builder.from(endpoint); } + + public RestDefinition rest() { + return builder.rest(); + } + + public RestConfigurationDefinition restConfiguration() { + return builder.restConfiguration(); + } } }