Search in sources :

Example 1 with Policy

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

the class DefaultPolicyManager method createOperationPolicy.

@Override
public OperationPolicy createOperationPolicy(Component operation, CoreEvent event, Map<String, Object> operationParameters, OperationExecutionFunction operationExecutionFunction) {
    PolicyPointcutParameters operationPointcutParameters = policyPointcutParametersManager.createOperationPointcutParameters(operation, event, operationParameters);
    List<Policy> parameterizedPolicies = policyProvider.findOperationParameterizedPolicies(operationPointcutParameters);
    if (parameterizedPolicies.isEmpty()) {
        return (operationEvent) -> operationExecutionFunction.execute(operationParameters, operationEvent);
    }
    return new CompositeOperationPolicy(parameterizedPolicies, lookupOperationParametersTransformer(operation.getLocation().getComponentIdentifier().getIdentifier()), operationPolicyProcessorFactory, () -> operationParameters, operationExecutionFunction, streamingManager);
}
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) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters)

Example 2 with Policy

use of org.mule.runtime.core.api.policy.Policy 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 3 with Policy

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

the class MuleApplicationPolicyProvider method addPolicy.

@Override
public synchronized void addPolicy(PolicyTemplateDescriptor policyTemplateDescriptor, PolicyParametrization parametrization) throws PolicyRegistrationException {
    try {
        checkArgument(application != null, "application was not configured on the policy provider");
        Optional<RegisteredPolicyInstanceProvider> registeredPolicyInstanceProvider = registeredPolicyInstanceProviders.stream().filter(p -> p.getPolicyId().equals(parametrization.getId())).findFirst();
        if (registeredPolicyInstanceProvider.isPresent()) {
            throw new IllegalArgumentException(createPolicyAlreadyRegisteredError(parametrization.getId()));
        }
        Optional<RegisteredPolicyTemplate> registeredPolicyTemplate = registeredPolicyTemplates.stream().filter(p -> p.policyTemplate.getDescriptor().getBundleDescriptor().getArtifactId().equals(policyTemplateDescriptor.getBundleDescriptor().getArtifactId())).findAny();
        if (!registeredPolicyTemplate.isPresent()) {
            PolicyTemplate policyTemplate = policyTemplateFactory.createArtifact(application, policyTemplateDescriptor);
            registeredPolicyTemplate = of(new RegisteredPolicyTemplate(policyTemplate));
            registeredPolicyTemplates.add(registeredPolicyTemplate.get());
        }
        ApplicationPolicyInstance applicationPolicyInstance = policyInstanceProviderFactory.create(application, registeredPolicyTemplate.get().policyTemplate, parametrization);
        applicationPolicyInstance.initialise();
        registeredPolicyInstanceProviders.add(new RegisteredPolicyInstanceProvider(applicationPolicyInstance, parametrization.getId()));
        registeredPolicyInstanceProviders.sort(null);
        registeredPolicyTemplate.get().count++;
    } catch (Exception e) {
        throw new PolicyRegistrationException(createPolicyRegistrationError(parametrization.getId()), e);
    }
}
Also used : Disposable(org.mule.runtime.api.lifecycle.Disposable) PolicyTemplate(org.mule.runtime.deployment.model.api.policy.PolicyTemplate) Optional.of(java.util.Optional.of) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) PolicyParametrization(org.mule.runtime.core.api.policy.PolicyParametrization) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException) PolicyInstanceProviderFactory(org.mule.runtime.module.deployment.impl.internal.policy.PolicyInstanceProviderFactory) String.format(java.lang.String.format) ArrayList(java.util.ArrayList) PolicyProvider(org.mule.runtime.core.api.policy.PolicyProvider) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) List(java.util.List) Integer.compare(java.lang.Integer.compare) ApplicationPolicyInstance(org.mule.runtime.module.deployment.impl.internal.policy.ApplicationPolicyInstance) Policy(org.mule.runtime.core.api.policy.Policy) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Optional(java.util.Optional) Application(org.mule.runtime.deployment.model.api.application.Application) LinkedList(java.util.LinkedList) PolicyTemplateFactory(org.mule.runtime.module.deployment.impl.internal.policy.PolicyTemplateFactory) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException) ApplicationPolicyInstance(org.mule.runtime.module.deployment.impl.internal.policy.ApplicationPolicyInstance) PolicyTemplate(org.mule.runtime.deployment.model.api.policy.PolicyTemplate) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException)

Example 4 with Policy

use of org.mule.runtime.core.api.policy.Policy 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 5 with Policy

use of org.mule.runtime.core.api.policy.Policy 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)5 Policy (org.mule.runtime.core.api.policy.Policy)5 List (java.util.List)4 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)4 Either (org.mule.runtime.core.api.functional.Either)4 SourcePolicyParametersTransformer (org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer)4 Processor (org.mule.runtime.core.api.processor.Processor)4 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)4 Mono.from (reactor.core.publisher.Mono.from)4 Mono.just (reactor.core.publisher.Mono.just)4 Collections.emptyList (java.util.Collections.emptyList)3 Map (java.util.Map)3 Either.right (org.mule.runtime.core.api.functional.Either.right)3 PolicyProvider (org.mule.runtime.core.api.policy.PolicyProvider)3 PolicyPointcutParameters (org.mule.runtime.policy.api.PolicyPointcutParameters)3 Collection (java.util.Collection)2 Optional.of (java.util.Optional.of)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Inject (javax.inject.Inject)2 Message (org.mule.runtime.api.message.Message)2