Search in sources :

Example 1 with SourcePolicyParametersTransformer

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));
}
Also used : Optional.empty(java.util.Optional.empty) Logger(org.slf4j.Logger) SourcePolicyParametersTransformer(org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer) INFO_ALREADY_LOGGED_KEY(org.mule.runtime.api.exception.MuleException.INFO_ALREADY_LOGGED_KEY) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) HashMap(java.util.HashMap) Either.left(org.mule.runtime.core.api.functional.Either.left) Processor(org.mule.runtime.core.api.processor.Processor) Supplier(java.util.function.Supplier) Either.right(org.mule.runtime.core.api.functional.Either.right) Either(org.mule.runtime.core.api.functional.Either) List(java.util.List) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) Policy(org.mule.runtime.core.api.policy.Policy) MessageProcessors(org.mule.runtime.core.privileged.processor.MessageProcessors) Map(java.util.Map) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Mono.from(reactor.core.publisher.Mono.from) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Optional(java.util.Optional) Message(org.mule.runtime.api.message.Message) MessagingException(org.mule.runtime.core.internal.exception.MessagingException)

Example 2 with SourcePolicyParametersTransformer

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));
}
Also used : Matchers.same(org.mockito.Matchers.same) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) Processor(org.mule.runtime.core.api.processor.Processor) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Arrays.asList(java.util.Arrays.asList) Is.is(org.hamcrest.core.Is.is) ExpectedException.none(org.junit.rules.ExpectedException.none) Mono.from(reactor.core.publisher.Mono.from) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) Mono.empty(reactor.core.publisher.Mono.empty) Mono.error(reactor.core.publisher.Mono.error) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) SourcePolicyParametersTransformer(org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer) Collections.emptyList(java.util.Collections.emptyList) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) Either(org.mule.runtime.core.api.functional.Either) Rule(org.junit.Rule) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) Policy(org.mule.runtime.core.api.policy.Policy) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) Optional(java.util.Optional) DefaultComponentLocation.fromSingleComponent(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test)

Aggregations

Optional (java.util.Optional)2 Message (org.mule.runtime.api.message.Message)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 Either (org.mule.runtime.core.api.functional.Either)2 Policy (org.mule.runtime.core.api.policy.Policy)2 SourcePolicyParametersTransformer (org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer)2 Processor (org.mule.runtime.core.api.processor.Processor)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 Publisher (org.reactivestreams.Publisher)2 Mono.from (reactor.core.publisher.Mono.from)2 Mono.just (reactor.core.publisher.Mono.just)2 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional.empty (java.util.Optional.empty)1 Optional.of (java.util.Optional.of)1 Supplier (java.util.function.Supplier)1 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)1