Search in sources :

Example 6 with PolicyPointcutParameters

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

the class PolicyPointcutParametersManagerTestCase method createOperationParametersWhenEmptyFactoryAndEmptySourceParameters.

@Test
public void createOperationParametersWhenEmptyFactoryAndEmptySourceParameters() {
    Map<String, Object> operationParameters = new HashMap<>();
    PolicyPointcutParameters parameters = parametersManager.createOperationPointcutParameters(component, event, operationParameters);
    assertThat(parameters.getComponent(), is(component));
    assertThat(parameters.getSourceParameters(), empty());
}
Also used : HashMap(java.util.HashMap) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Test(org.junit.Test)

Example 7 with PolicyPointcutParameters

use of org.mule.runtime.policy.api.PolicyPointcutParameters 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)

Example 8 with PolicyPointcutParameters

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

the class PolicyPointcutParametersManager method createSourcePointcutParameters.

/**
 * Creates {@link PolicyPointcutParameters} for a specific source. The created parameters is also stored so it can be used in
 * case a matching policy also defines an operation part.
 *
 * @param source the source component to which policies will be applied
 * @param event the event which will execute the source policies
 * @return the created {@link PolicyPointcutParameters}
 */
public PolicyPointcutParameters createSourcePointcutParameters(Component source, CoreEvent event) {
    ComponentIdentifier sourceIdentifier = source.getLocation().getComponentIdentifier().getIdentifier();
    PolicyPointcutParameters sourcePointcutParameters = createPointcutParameters(source, SourcePolicyPointcutParametersFactory.class, sourcePointcutFactories, factory -> factory.supportsSourceIdentifier(sourceIdentifier), factory -> factory.createPolicyPointcutParameters(source, event.getMessage().getAttributes())).orElse(new PolicyPointcutParameters(source));
    String correlationId = event.getContext().getCorrelationId();
    sourceParametersMap.put(correlationId, sourcePointcutParameters);
    ((BaseEventContext) event.getContext()).getRootContext().onTerminated((e, t) -> sourceParametersMap.remove(correlationId));
    return sourcePointcutParameters;
}
Also used : Optional.empty(java.util.Optional.empty) SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Predicate(java.util.function.Predicate) Optional.of(java.util.Optional.of) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collection(java.util.Collection) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Function(java.util.function.Function) String.format(java.lang.String.format) Component(org.mule.runtime.api.component.Component) Map(java.util.Map) OperationPolicyPointcutParametersParameters(org.mule.runtime.policy.api.OperationPolicyPointcutParametersParameters) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Optional(java.util.Optional) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters)

Example 9 with PolicyPointcutParameters

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

the class PolicyPointcutParametersManagerTestCase method createSourceParametersWhenOneFactorySupportsIdentifierAndOneNot.

@Test
public void createSourceParametersWhenOneFactorySupportsIdentifierAndOneNot() {
    SourcePolicyPointcutParametersFactory factory1 = mockSourceFactory(true);
    SourcePolicyPointcutParametersFactory factory2 = mockSourceFactory(false);
    sourcePointcutFactories.add(factory1);
    sourcePointcutFactories.add(factory2);
    PolicyPointcutParameters parameters = parametersManager.createSourcePointcutParameters(component, event);
    assertThat(parameters.getComponent(), is(component));
    assertThat(parameters.getSourceParameters(), empty());
    verify(factory1).supportsSourceIdentifier(identifier);
    verify(factory1).createPolicyPointcutParameters(component, event.getMessage().getAttributes());
    verify(factory2).supportsSourceIdentifier(identifier);
    verify(factory2, never()).createPolicyPointcutParameters(component, event.getMessage().getAttributes());
    verify(eventContext).onTerminated(any());
}
Also used : SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Test(org.junit.Test)

Example 10 with PolicyPointcutParameters

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

the class PolicyPointcutParametersManagerTestCase method createOperationParametersWhenEmptyFactory.

@Test
public void createOperationParametersWhenEmptyFactory() {
    Map<String, Object> operationParameters = new HashMap<>();
    sourcePointcutFactories.add(mockSourceFactory(true));
    PolicyPointcutParameters sourceParameters = parametersManager.createSourcePointcutParameters(component, event);
    PolicyPointcutParameters parameters = parametersManager.createOperationPointcutParameters(component, event, operationParameters);
    assertThat(parameters.getComponent(), is(component));
    assertThat(parameters.getSourceParameters(), is(of(sourceParameters)));
}
Also used : HashMap(java.util.HashMap) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Test(org.junit.Test)

Aggregations

PolicyPointcutParameters (org.mule.runtime.policy.api.PolicyPointcutParameters)17 Test (org.junit.Test)11 OperationPolicyPointcutParametersFactory (org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory)10 SourcePolicyPointcutParametersFactory (org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory)8 HashMap (java.util.HashMap)7 Collection (java.util.Collection)4 Map (java.util.Map)4 Optional (java.util.Optional)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Component (org.mule.runtime.api.component.Component)4 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)4 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)4 String.format (java.lang.String.format)2 Collections.emptyList (java.util.Collections.emptyList)2 List (java.util.List)2 Optional.empty (java.util.Optional.empty)2 Optional.of (java.util.Optional.of)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2 Inject (javax.inject.Inject)2