Search in sources :

Example 1 with PolicyStateId

use of org.mule.runtime.core.api.policy.PolicyStateId in project mule by mulesoft.

the class OperationPolicyProcessor method executePolicyChain.

private Mono<PrivilegedEvent> executePolicyChain(PrivilegedEvent operationEvent, PolicyStateId policyStateId, PrivilegedEvent policyEvent) {
    PolicyChain policyChain = policy.getPolicyChain();
    policyChain.onChainError(t -> manageError(policyStateId, operationEvent, (MessagingException) t));
    return just(policyEvent).doOnNext(event -> logPolicy(event.getContext().getCorrelationId(), policyStateId.getPolicyId(), getMessageAttributesAsString(event), "Before operation")).cast(CoreEvent.class).transform(policyChain).cast(PrivilegedEvent.class).doOnNext(policyChainResult -> policyStateHandler.updateState(policyStateId, policyChainResult)).map(policyChainResult -> policyEventConverter.createEvent(policyChainResult, operationEvent)).doOnNext(event -> logPolicy(event.getContext().getCorrelationId(), policyStateId.getPolicyId(), getMessageAttributesAsString(event), "After operation"));
}
Also used : PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) PolicyStateHandler(org.mule.runtime.core.api.policy.PolicyStateHandler) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Logger(org.slf4j.Logger) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) Message.of(org.mule.runtime.api.message.Message.of) Processor(org.mule.runtime.core.api.processor.Processor) PolicyChain(org.mule.runtime.core.api.policy.PolicyChain) MuleException(org.mule.runtime.api.exception.MuleException) Policy(org.mule.runtime.core.api.policy.Policy) 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) PolicyChain(org.mule.runtime.core.api.policy.PolicyChain) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent)

Example 2 with PolicyStateId

use of org.mule.runtime.core.api.policy.PolicyStateId in project mule by mulesoft.

the class OperationPolicyProcessor method apply.

@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
    return from(publisher).cast(PrivilegedEvent.class).flatMap(operationEvent -> {
        PolicyStateId policyStateId = new PolicyStateId(operationEvent.getContext().getCorrelationId(), policy.getPolicyId());
        Optional<CoreEvent> latestPolicyState = policyStateHandler.getLatestState(policyStateId);
        PrivilegedEvent variablesProviderEvent = (PrivilegedEvent) latestPolicyState.orElseGet(() -> PrivilegedEvent.builder(operationEvent.getContext()).message(of(null)).build());
        policyStateHandler.updateState(policyStateId, variablesProviderEvent);
        PrivilegedEvent policyEvent = policyEventConverter.createEvent(operationEvent, variablesProviderEvent);
        Processor operationCall = buildOperationExecutionWithPolicyFunction(nextProcessor, operationEvent, policyStateId);
        policyStateHandler.updateNextOperation(policyStateId.getExecutionIdentifier(), operationCall);
        return executePolicyChain(operationEvent, policyStateId, policyEvent);
    });
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Processor(org.mule.runtime.core.api.processor.Processor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId)

Example 3 with PolicyStateId

use of org.mule.runtime.core.api.policy.PolicyStateId in project mule by mulesoft.

the class PolicyNextActionMessageProcessor method apply.

@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
    return from(publisher).doOnNext(coreEvent -> logExecuteNextEvent("Before execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId())).flatMap(event -> {
        Processor nextOperation = policyStateHandler.retrieveNextOperation(event.getContext().getCorrelationId());
        if (nextOperation == null) {
            return error(new MuleRuntimeException(createStaticMessage("There's no next operation configured for event context id " + event.getContext().getCorrelationId())));
        }
        popBeforeNextFlowFlowStackElement().accept(event);
        notificationHelper.notification(BEFORE_NEXT).accept(event);
        return from(processWithChildContext(event, nextOperation, ofNullable(getLocation()))).doOnSuccessOrError(notificationHelper.successOrErrorNotification(AFTER_NEXT).andThen((ev, t) -> pushAfterNextFlowStackElement().accept(event))).onErrorResume(MessagingException.class, t -> {
            PolicyStateId policyStateId = new PolicyStateId(event.getContext().getCorrelationId(), muleContext.getConfiguration().getId());
            policyStateHandler.getLatestState(policyStateId).ifPresent(latestStateEvent -> t.setProcessedEvent(policyEventConverter.createEvent((PrivilegedEvent) t.getEvent(), (PrivilegedEvent) latestStateEvent)));
            // Given we've used child context to ensure AFTER_NEXT notifications are fired at exactly the right time we need
            // to propagate the error to parent context manually.
            ((BaseEventContext) event.getContext()).error(resolveMessagingException(t.getFailingComponent(), muleContext).apply(t));
            return empty();
        }).doOnNext(coreEvent -> logExecuteNextEvent("After execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId()));
    });
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Message(org.mule.runtime.api.message.Message) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) BEFORE_NEXT(org.mule.runtime.api.notification.PolicyNotification.BEFORE_NEXT) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) Component(org.mule.runtime.api.component.Component) Mono.from(reactor.core.publisher.Mono.from) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) Mono.error(reactor.core.publisher.Mono.error) AFTER_NEXT(org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT) EventContext(org.mule.runtime.api.event.EventContext) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) PolicyStateHandler(org.mule.runtime.core.api.policy.PolicyStateHandler) Logger(org.slf4j.Logger) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Consumer(java.util.function.Consumer) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) DefaultFlowCallStack(org.mule.runtime.core.internal.context.notification.DefaultFlowCallStack) FlowStackElement(org.mule.runtime.core.api.context.notification.FlowStackElement) Processor(org.mule.runtime.core.api.processor.Processor) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 4 with PolicyStateId

use of org.mule.runtime.core.api.policy.PolicyStateId in project mule by mulesoft.

the class DefaultPolicyStateHandlerTestCase method destroyState.

@Test
public void destroyState() {
    PolicyStateId policyStateExecutionId = new PolicyStateId(TEST_EXECUTION_ID, TEST_POLICY_ID);
    defaultPolicyStateHandler.destroyState(policyStateExecutionId.getExecutionIdentifier());
    assertThat(defaultPolicyStateHandler.getLatestState(policyStateExecutionId).isPresent(), is(false));
    assertThat(defaultPolicyStateHandler.retrieveNextOperation(policyStateExecutionId.getExecutionIdentifier()), nullValue());
}
Also used : PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) Test(org.junit.Test)

Example 5 with PolicyStateId

use of org.mule.runtime.core.api.policy.PolicyStateId in project mule by mulesoft.

the class DefaultPolicyStateHandlerTestCase method destroyStateWhenEventIsCompleted.

@Test
public void destroyStateWhenEventIsCompleted() {
    PolicyStateId policyStateExecutionId = new PolicyStateId(TEST_EXECUTION_ID, TEST_POLICY_ID);
    Reference<BiConsumer> subscriberReference = new Reference<>();
    BaseEventContext rootEventContext = eventTestExecutionId.getContext().getRootContext();
    Mockito.doAnswer(invocationOnMock -> {
        subscriberReference.set((BiConsumer) invocationOnMock.getArguments()[0]);
        return null;
    }).when(rootEventContext).onTerminated(any(BiConsumer.class));
    defaultPolicyStateHandler.updateState(policyStateExecutionId, eventTestExecutionId);
    subscriberReference.get().accept(null, null);
    assertThat(defaultPolicyStateHandler.getLatestState(policyStateExecutionId).isPresent(), is(false));
    assertThat(defaultPolicyStateHandler.policyStateIdsByExecutionIdentifier.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
    assertThat(defaultPolicyStateHandler.nextOperationMap.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
    assertThat(defaultPolicyStateHandler.stateMap.containsKey(policyStateExecutionId), is(false));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Reference(org.mule.runtime.api.util.Reference) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) BiConsumer(java.util.function.BiConsumer) Test(org.junit.Test)

Aggregations

PolicyStateId (org.mule.runtime.core.api.policy.PolicyStateId)7 Test (org.junit.Test)4 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)3 Processor (org.mule.runtime.core.api.processor.Processor)3 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)3 MuleException (org.mule.runtime.api.exception.MuleException)2 PolicyStateHandler (org.mule.runtime.core.api.policy.PolicyStateHandler)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)2 MessageProcessors.processToApply (org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply)2 Publisher (org.reactivestreams.Publisher)2 Logger (org.slf4j.Logger)2 LoggerFactory.getLogger (org.slf4j.LoggerFactory.getLogger)2 Mono.from (reactor.core.publisher.Mono.from)2 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Inject (javax.inject.Inject)1