Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings for rest dsl in groovy/js routes #120

Merged
merged 1 commit into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions runtime/examples/routes-rest.js
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -116,6 +122,8 @@ public void configure() throws Exception {
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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
}