Search in sources :

Example 1 with DEFAULT_SPLIT_EXPRESSION

use of org.mule.runtime.core.internal.routing.ExpressionSplittingStrategy.DEFAULT_SPLIT_EXPRESSION in project mule by mulesoft.

the class Foreach method apply.

@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
    return from(publisher).doOnNext(event -> {
        if (expression.equals(DEFAULT_SPLIT_EXPRESSION) && Map.class.isAssignableFrom(event.getMessage().getPayload().getDataType().getType())) {
            throw new IllegalArgumentException(MAP_NOT_SUPPORTED_MESSAGE);
        }
    }).flatMap(originalEvent -> {
        // Keep reference to existing rootMessage/count variables in order to restore later to support foreach nesting.
        final Object previousCounterVar = originalEvent.getVariables().containsKey(counterVariableName) ? originalEvent.getVariables().get(counterVariableName).getValue() : null;
        final Object previousRootMessageVar = originalEvent.getVariables().containsKey(rootMessageVariableName) ? originalEvent.getVariables().get(rootMessageVariableName).getValue() : null;
        final CoreEvent requestEvent = builder(originalEvent).addVariable(rootMessageVariableName, originalEvent.getMessage()).build();
        return Mono.from(splitAndProcess(requestEvent)).map(result -> {
            final Builder responseBuilder = builder(result).message(originalEvent.getMessage());
            restoreVariables(previousCounterVar, previousRootMessageVar, responseBuilder);
            return responseBuilder.build();
        }).onErrorMap(MessagingException.class, me -> {
            // Restore variables in case of error also
            CoreEvent.Builder exceptionEventBuilder = builder(me.getEvent());
            restoreVariables(previousCounterVar, previousRootMessageVar, exceptionEventBuilder);
            me.setProcessedEvent(exceptionEventBuilder.build());
            return me;
        });
    });
}
Also used : MessageProcessors.newChain(org.mule.runtime.core.privileged.processor.MessageProcessors.newChain) Message(org.mule.runtime.api.message.Message) Flux.from(reactor.core.publisher.Flux.from) Mono.defer(reactor.core.publisher.Mono.defer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject) Processor(org.mule.runtime.core.api.processor.Processor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) Iterators(com.google.common.collect.Iterators) Collections.singletonList(java.util.Collections.singletonList) Scope(org.mule.runtime.core.privileged.processor.Scope) MuleException(org.mule.runtime.api.exception.MuleException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProcessors.newChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.newChildContext) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) Map(java.util.Map) Mono.just(reactor.core.publisher.Mono.just) AbstractMessageProcessorOwner(org.mule.runtime.core.api.processor.AbstractMessageProcessorOwner) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) EventBuilderConfigurer(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer) Mono.empty(reactor.core.publisher.Mono.empty) DEFAULT_SPLIT_EXPRESSION(org.mule.runtime.core.internal.routing.ExpressionSplittingStrategy.DEFAULT_SPLIT_EXPRESSION) EventContext(org.mule.runtime.api.event.EventContext) Iterator(java.util.Iterator) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Flux.fromIterable(reactor.core.publisher.Flux.fromIterable) Optional.ofNullable(java.util.Optional.ofNullable) MessageProcessors.completeSuccessIfNeeded(org.mule.runtime.core.privileged.processor.MessageProcessors.completeSuccessIfNeeded) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) EventBuilderConfigurerList(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerList) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) TypedValue(org.mule.runtime.api.metadata.TypedValue) List(java.util.List) Optional(java.util.Optional) EventBuilderConfigurerIterator(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerIterator) MessageProcessors.getProcessingStrategy(org.mule.runtime.core.privileged.processor.MessageProcessors.getProcessingStrategy) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject)

Aggregations

Iterators (com.google.common.collect.Iterators)1 Collections.singletonList (java.util.Collections.singletonList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 EventContext (org.mule.runtime.api.event.EventContext)1 MuleException (org.mule.runtime.api.exception.MuleException)1 Initialisable (org.mule.runtime.api.lifecycle.Initialisable)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 Message (org.mule.runtime.api.message.Message)1 DataType.fromObject (org.mule.runtime.api.metadata.DataType.fromObject)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 Builder (org.mule.runtime.core.api.event.CoreEvent.Builder)1 CoreEvent.builder (org.mule.runtime.core.api.event.CoreEvent.builder)1 AbstractMessageProcessorOwner (org.mule.runtime.core.api.processor.AbstractMessageProcessorOwner)1