Search in sources :

Example 1 with Either

use of org.mule.runtime.core.api.functional.Either 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)

Example 2 with Either

use of org.mule.runtime.core.api.functional.Either in project mule by mulesoft.

the class ModuleFlowProcessingPhase method runPhase.

@Override
public void runPhase(final ModuleFlowProcessingPhaseTemplate template, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    try {
        final MessageSource messageSource = messageProcessContext.getMessageSource();
        final FlowConstruct flowConstruct = (FlowConstruct) componentLocator.find(messageSource.getRootContainerLocation()).get();
        final ComponentLocation sourceLocation = messageSource.getLocation();
        final Consumer<Either<MessagingException, CoreEvent>> terminateConsumer = getTerminateConsumer(messageSource, template);
        final CompletableFuture<Void> responseCompletion = new CompletableFuture<>();
        final CoreEvent templateEvent = createEvent(template, sourceLocation, responseCompletion, flowConstruct);
        try {
            FlowProcessor flowExecutionProcessor = new FlowProcessor(template, flowConstruct.getExceptionListener(), templateEvent);
            flowExecutionProcessor.setAnnotations(flowConstruct.getAnnotations());
            final SourcePolicy policy = policyManager.createSourcePolicyInstance(messageSource, templateEvent, flowExecutionProcessor, template);
            final PhaseContext phaseContext = new PhaseContext(template, messageProcessContext, phaseResultNotifier, terminateConsumer);
            just(templateEvent).doOnNext(onMessageReceived(template, messageProcessContext, flowConstruct)).flatMap(request -> from(policy.process(request))).flatMap(policyResult -> policyResult.reduce(policyFailure(phaseContext, flowConstruct, messageSource), policySuccess(phaseContext, flowConstruct, messageSource))).doOnSuccess(aVoid -> phaseResultNotifier.phaseSuccessfully()).doOnError(onFailure(flowConstruct, messageSource, phaseResultNotifier, terminateConsumer)).doAfterTerminate(() -> responseCompletion.complete(null)).subscribe();
        } catch (Exception e) {
            from(template.sendFailureResponseToClient(new MessagingExceptionResolver(messageProcessContext.getMessageSource()).resolve(new MessagingException(templateEvent, e), muleContext), template.getFailedExecutionResponseParametersFunction().apply(templateEvent))).doOnTerminate(() -> phaseResultNotifier.phaseFailure(e)).subscribe();
        }
    } catch (Exception t) {
        phaseResultNotifier.phaseFailure(t);
    }
}
Also used : Message(org.mule.runtime.api.message.Message) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) FunctionalUtils.safely(org.mule.runtime.core.internal.util.FunctionalUtils.safely) Map(java.util.Map) SOURCE_ERROR_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_GENERATE) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) SOURCE_ERROR_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_SEND) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) Collection(java.util.Collection) NullExceptionHandler(org.mule.runtime.core.api.exception.NullExceptionHandler) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) Message.of(org.mule.runtime.api.message.Message.of) Either.left(org.mule.runtime.core.api.functional.Either.left) ErrorBuilder.builder(org.mule.runtime.core.internal.message.ErrorBuilder.builder) Either.right(org.mule.runtime.core.api.functional.Either.right) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) MESSAGE_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RESPONSE) MessageUtils.toMessage(org.mule.runtime.core.internal.util.message.MessageUtils.toMessage) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) SOURCE_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_SEND) SourcePolicySuccessResult(org.mule.runtime.core.internal.policy.SourcePolicySuccessResult) MessageSource(org.mule.runtime.core.api.source.MessageSource) MessageProcessContext(org.mule.runtime.core.privileged.execution.MessageProcessContext) CompletableFuture(java.util.concurrent.CompletableFuture) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) MessageUtils.toMessageCollection(org.mule.runtime.core.internal.util.message.MessageUtils.toMessageCollection) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) MuleException(org.mule.runtime.api.exception.MuleException) SourcePolicyFailureResult(org.mule.runtime.core.internal.policy.SourcePolicyFailureResult) Mono.from(reactor.core.publisher.Mono.from) SOURCE_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_GENERATE) Mono.error(reactor.core.publisher.Mono.error) Mono.fromCallable(reactor.core.publisher.Mono.fromCallable) MESSAGE_RECEIVED(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RECEIVED) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) MESSAGE_ERROR_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_ERROR_RESPONSE) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Result(org.mule.runtime.extension.api.runtime.operation.Result) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Mono.when(reactor.core.publisher.Mono.when) TypedValue(org.mule.runtime.api.metadata.TypedValue) Consumer(java.util.function.Consumer) Either(org.mule.runtime.core.api.functional.Either) MessageProcessTemplate(org.mule.runtime.core.privileged.execution.MessageProcessTemplate) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) Builder(org.mule.runtime.core.internal.message.InternalEvent.Builder) ANY(org.mule.runtime.api.metadata.MediaType.ANY) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) MessageSource(org.mule.runtime.core.api.source.MessageSource) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Either(org.mule.runtime.core.api.functional.Either) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver)

Example 3 with Either

use of org.mule.runtime.core.api.functional.Either in project mule by mulesoft.

the class DefaultPolicyManager method createSourcePolicyInstance.

@Override
public SourcePolicy createSourcePolicyInstance(Component source, CoreEvent sourceEvent, Processor flowExecutionProcessor, MessageSourceResponseParametersProcessor messageSourceResponseParametersProcessor) {
    PolicyPointcutParameters sourcePointcutParameters = policyPointcutParametersManager.createSourcePointcutParameters(source, sourceEvent);
    List<Policy> parameterizedPolicies = policyProvider.findSourceParameterizedPolicies(sourcePointcutParameters);
    if (parameterizedPolicies.isEmpty()) {
        return event -> from(process(event, flowExecutionProcessor)).defaultIfEmpty(CoreEvent.builder(sourceEvent).message(of(null)).build()).<Either<SourcePolicyFailureResult, SourcePolicySuccessResult>>map(flowExecutionResult -> right(new SourcePolicySuccessResult(flowExecutionResult, () -> messageSourceResponseParametersProcessor.getSuccessfulExecutionResponseParametersFunction().apply(flowExecutionResult), messageSourceResponseParametersProcessor))).onErrorResume(Exception.class, e -> {
            MessagingException messagingException = e instanceof MessagingException ? (MessagingException) e : new MessagingException(event, e, (Component) flowExecutionProcessor);
            return just(Either.left(new SourcePolicyFailureResult(messagingException, () -> messageSourceResponseParametersProcessor.getFailedExecutionResponseParametersFunction().apply(messagingException.getEvent()))));
        });
    }
    return new CompositeSourcePolicy(parameterizedPolicies, lookupSourceParametersTransformer(source.getLocation().getComponentIdentifier().getIdentifier()), sourcePolicyProcessorFactory, flowExecutionProcessor, messageSourceResponseParametersProcessor);
}
Also used : Policy(org.mule.runtime.core.api.policy.Policy) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Processor(org.mule.runtime.core.api.processor.Processor) StreamingManager(org.mule.runtime.core.api.streaming.StreamingManager) Inject(javax.inject.Inject) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleContext(org.mule.runtime.core.api.MuleContext) MessageProcessors.process(org.mule.runtime.core.privileged.processor.MessageProcessors.process) Component(org.mule.runtime.api.component.Component) Map(java.util.Map) Mono.from(reactor.core.publisher.Mono.from) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) PolicyStateHandler(org.mule.runtime.core.api.policy.PolicyStateHandler) SourcePolicyParametersTransformer(org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer) Collections.emptyList(java.util.Collections.emptyList) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Message.of(org.mule.runtime.api.message.Message.of) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) OperationPolicyParametersTransformer(org.mule.runtime.core.api.policy.OperationPolicyParametersTransformer) Either.right(org.mule.runtime.core.api.functional.Either.right) Either(org.mule.runtime.core.api.functional.Either) PolicyProvider(org.mule.runtime.core.api.policy.PolicyProvider) List(java.util.List) Policy(org.mule.runtime.core.api.policy.Policy) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Optional(java.util.Optional) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Component(org.mule.runtime.api.component.Component) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters)

Aggregations

Optional (java.util.Optional)3 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)3 Either (org.mule.runtime.core.api.functional.Either)3 Processor (org.mule.runtime.core.api.processor.Processor)3 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)3 Mono.from (reactor.core.publisher.Mono.from)3 Mono.just (reactor.core.publisher.Mono.just)3 Collection (java.util.Collection)2 Collections.emptyList (java.util.Collections.emptyList)2 Map (java.util.Map)2 Message (org.mule.runtime.api.message.Message)2 FlowConstruct (org.mule.runtime.core.api.construct.FlowConstruct)2 EventContextFactory.create (org.mule.runtime.core.api.event.EventContextFactory.create)2 Publisher (org.reactivestreams.Publisher)2 Mono (reactor.core.publisher.Mono)2 Mono.empty (reactor.core.publisher.Mono.empty)2 Mono.error (reactor.core.publisher.Mono.error)2 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Optional.of (java.util.Optional.of)1