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