Search in sources :

Example 1 with Step

use of io.syndesis.extension.api.Step 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

StepAction (io.syndesis.common.model.action.StepAction)1 StepKind (io.syndesis.common.model.integration.StepKind)1 Step (io.syndesis.extension.api.Step)1 IntegrationRouteBuilder (io.syndesis.integration.runtime.IntegrationRouteBuilder)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 CamelContext (org.apache.camel.CamelContext)1 TypeConverter (org.apache.camel.TypeConverter)1 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)1 IntrospectionSupport (org.apache.camel.util.IntrospectionSupport)1 ObjectHelper (org.apache.camel.util.ObjectHelper)1