Search in sources :

Example 1 with IntegrationRouteBuilder

use of io.syndesis.integration.runtime.IntegrationRouteBuilder in project syndesis by syndesisio.

the class SplitStepHandlerJsonTest method testTokenizeBodyStep.

@Test
public void testTokenizeBodyStep() throws Exception {
    final CamelContext context = new SpringCamelContext(applicationContext);
    try {
        final RouteBuilder routes = new IntegrationRouteBuilder("classpath:/syndesis/integration/SplitStepHandlerJsonTest.json", Collections.emptyList());
        // Set up the camel context
        context.addRoutes(routes);
        context.start();
        // Dump routes as XML for troubleshooting
        dumpRoutes(context);
        final ProducerTemplate template = context.createProducerTemplate();
        final MockEndpoint result = context.getEndpoint("mock:expression", MockEndpoint.class);
        final String[] expected = { "a", "b", "c" };
        final List<String> body = Arrays.asList(expected);
        result.expectedMessageCount(3);
        result.expectedBodiesReceived((Object[]) expected);
        template.sendBody("direct:expression", body);
        result.assertIsSatisfied();
    } finally {
        context.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) IntegrationRouteBuilder(io.syndesis.integration.runtime.IntegrationRouteBuilder) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) IntegrationRouteBuilder(io.syndesis.integration.runtime.IntegrationRouteBuilder) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with IntegrationRouteBuilder

use of io.syndesis.integration.runtime.IntegrationRouteBuilder in project syndesis by syndesisio.

the class ExtensionStepHandler method handle.

@SuppressWarnings("PMD")
@Override
public Optional<ProcessorDefinition> handle(io.syndesis.common.model.integration.Step step, ProcessorDefinition route, IntegrationRouteBuilder builder, String stepIndex) {
    ObjectHelper.notNull(route, "route");
    // Model
    final StepAction action = step.getActionAs(StepAction.class).get();
    // Camel
    final Map<String, String> properties = step.getConfiguredProperties();
    final CamelContext context = builder.getContext();
    if (action.getDescriptor().getKind() == StepAction.Kind.ENDPOINT) {
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            route.setHeader(entry.getKey(), builder.constant(entry.getValue()));
        }
        route = route.to(action.getDescriptor().getEntrypoint());
    } else if (action.getDescriptor().getKind() == StepAction.Kind.BEAN) {
        String method = null;
        String function = action.getDescriptor().getEntrypoint();
        String options = null;
        if (ObjectHelper.isEmpty(function)) {
            return Optional.empty();
        }
        int idx = function.indexOf("::");
        if (idx > 0 && !function.endsWith("::")) {
            method = function.substring(idx + 2);
            function = function.substring(0, idx);
        }
        if (ObjectHelper.isNotEmpty(properties)) {
            options = properties.entrySet().stream().filter(entry -> ObjectHelper.isNotEmpty(entry.getKey())).filter(entry -> ObjectHelper.isNotEmpty(entry.getValue())).map(entry -> "bean." + entry.getKey() + "=" + StringHelpers.sanitizeForURI(entry.getValue())).collect(Collectors.joining("&"));
        }
        String uri = "class:" + function;
        if (method != null) {
            uri += "?method=" + method;
            if (options != null) {
                uri += "&" + options;
            }
        } else if (options != null) {
            uri += "?" + options;
        }
        route = route.to(uri);
    } else if (action.getDescriptor().getKind() == StepAction.Kind.STEP) {
        final String target = action.getDescriptor().getEntrypoint();
        final TypeConverter converter = context.getTypeConverter();
        if (!ObjectHelper.isEmpty(target)) {
            try {
                final Class<Step> clazz = context.getClassResolver().resolveMandatoryClass(target, Step.class);
                final Step stepExtension = context.getInjector().newInstance(clazz);
                final Map<String, Object> props = new HashMap<>(properties);
                try {
                    IntrospectionSupport.setProperties(context, converter, stepExtension, props);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
                // Set the camel context if the step extension object implements
                // CamelContextAware, this is a shortcut to retrieve it from
                // the handler method.
                ObjectHelper.trySetCamelContext(stepExtension, context);
                return stepExtension.configure(context, route, props);
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException(e);
            }
        }
    }
    return Optional.of(route);
}
Also used : CamelContext(org.apache.camel.CamelContext) Step(io.syndesis.extension.api.Step) CamelContext(org.apache.camel.CamelContext) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) IntegrationRouteBuilder(io.syndesis.integration.runtime.IntegrationRouteBuilder) StepAction(io.syndesis.common.model.action.StepAction) IntegrationStepHandler(io.syndesis.integration.runtime.IntegrationStepHandler) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) TypeConverter(org.apache.camel.TypeConverter) IntrospectionSupport(org.apache.camel.util.IntrospectionSupport) Map(java.util.Map) StringHelpers(io.syndesis.integration.runtime.util.StringHelpers) Optional(java.util.Optional) ObjectHelper(org.apache.camel.util.ObjectHelper) StepKind(io.syndesis.common.model.integration.StepKind) HashMap(java.util.HashMap) Step(io.syndesis.extension.api.Step) TypeConverter(org.apache.camel.TypeConverter) StepAction(io.syndesis.common.model.action.StepAction) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IntegrationRouteBuilder (io.syndesis.integration.runtime.IntegrationRouteBuilder)2 CamelContext (org.apache.camel.CamelContext)2 StepAction (io.syndesis.common.model.action.StepAction)1 StepKind (io.syndesis.common.model.integration.StepKind)1 Step (io.syndesis.extension.api.Step)1 IntegrationStepHandler (io.syndesis.integration.runtime.IntegrationStepHandler)1 StringHelpers (io.syndesis.integration.runtime.util.StringHelpers)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 TypeConverter (org.apache.camel.TypeConverter)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)1 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)1 IntrospectionSupport (org.apache.camel.util.IntrospectionSupport)1 ObjectHelper (org.apache.camel.util.ObjectHelper)1 Test (org.junit.Test)1