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;
});
});
}
Aggregations