use of org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer in project mule by mulesoft.
the class CompositeSourcePolicy method processNextOperation.
/**
* Executes the flow.
*
* If there's a {@link SourcePolicyParametersTransformer} provided then it will use it to convert the source response or source
* failure response from the parameters back to a {@link Message} that can be routed through the policy chain which later will
* be convert back to response or failure response parameters thus allowing the policy chain to modify the response.. That
* message will be the result of the next-operation of the policy.
*
* If no {@link SourcePolicyParametersTransformer} is provided, then the same response from the flow is going to be routed as
* response of the next-operation of the policy chain. In this case, the same response from the flow is going to be used to
* generate the response or failure response for the source so the policy chain is not going to be able to modify the response
* sent by the source.
*
* When the flow execution fails, it will create a {@link FlowExecutionException} instead of a regular
* {@link MessagingException} to signal that the failure was through the the flow exception and not the policy logic.
*/
@Override
protected Publisher<CoreEvent> processNextOperation(CoreEvent event) {
return just(event).flatMap(request -> from(processWithChildContext(request, flowExecutionProcessor, empty()))).map(flowExecutionResponse -> {
originalResponseParameters = getParametersProcessor().getSuccessfulExecutionResponseParametersFunction().apply(flowExecutionResponse);
Message message = getParametersTransformer().map(parametersTransformer -> parametersTransformer.fromSuccessResponseParametersToMessage(originalResponseParameters)).orElseGet(flowExecutionResponse::getMessage);
return CoreEvent.builder(event).message(message).build();
}).onErrorMap(MessagingException.class, messagingException -> {
originalFailureResponseParameters = getParametersProcessor().getFailedExecutionResponseParametersFunction().apply(messagingException.getEvent());
Message message = getParametersTransformer().map(parametersTransformer -> parametersTransformer.fromFailureResponseParametersToMessage(originalFailureResponseParameters)).orElse(messagingException.getEvent().getMessage());
MessagingException flowExecutionException = new FlowExecutionException(CoreEvent.builder(messagingException.getEvent()).message(message).build(), messagingException.getCause(), messagingException.getFailingComponent());
if (messagingException.getInfo().containsKey(INFO_ALREADY_LOGGED_KEY)) {
flowExecutionException.addInfo(INFO_ALREADY_LOGGED_KEY, messagingException.getInfo().get(INFO_ALREADY_LOGGED_KEY));
}
return flowExecutionException;
}).doOnError(e -> LOGGER.error(e.getMessage(), e));
}
use of org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer in project mule by mulesoft.
the class CompositeSourcePolicyTestCase method nextProcessorExecutionFailurePropagates.
@Test
public void nextProcessorExecutionFailurePropagates() throws Exception {
RuntimeException policyException = new RuntimeException("policy failure");
reset(flowExecutionProcessor);
when(flowExecutionProcessor.apply(any())).thenAnswer(invocation -> {
Mono<CoreEvent> mono = from(invocation.getArgumentAt(0, Publisher.class));
mono.doOnNext(event -> ((BaseEventContext) event.getContext()).error(new MessagingException(event, policyException))).subscribe();
return empty();
});
compositeSourcePolicy = new CompositeSourcePolicy(asList(firstPolicy, secondPolicy), sourcePolicyParametersTransformer, sourcePolicyProcessorFactory, flowExecutionProcessor, sourceParametersTransformer);
Either<SourcePolicyFailureResult, SourcePolicySuccessResult> sourcePolicyResult = from(compositeSourcePolicy.process(initialEvent)).block();
assertThat(sourcePolicyResult.isLeft(), is(true));
assertThat(sourcePolicyResult.getLeft().getMessagingException(), instanceOf(FlowExecutionException.class));
assertThat(sourcePolicyResult.getLeft().getMessagingException().getCause(), is(policyException));
}
Aggregations