Search in sources :

Example 76 with CoreEvent

use of org.mule.runtime.core.api.event.CoreEvent in project mule by mulesoft.

the class MVELExpressionLanguage method evaluate.

@Override
public TypedValue evaluate(String expression, CoreEvent event, CoreEvent.Builder eventBuilder, ComponentLocation componentLocation, BindingContext bindingContext) {
    expression = removeExpressionMarker(expression);
    Map<String, Object> bindingMap = bindingContext.identifiers().stream().collect(toMap(id -> id, id -> bindingContext.lookup(id).get().getValue()));
    final Object value = evaluateUntyped(expression, (PrivilegedEvent) event, (PrivilegedEvent.Builder) eventBuilder, componentLocation, bindingMap);
    if (value instanceof TypedValue) {
        return (TypedValue) value;
    } else {
        final Serializable compiledExpression = expressionExecutor.getCompiledExpression(expression);
        DataType dataType = event != null ? dataTypeResolver.resolve(value, (PrivilegedEvent) event, compiledExpression) : OBJECT;
        return new TypedValue(value, dataType);
    }
}
Also used : ExtendedExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.ExtendedExpressionLanguageAdaptor) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) CoreMessages(org.mule.runtime.core.api.config.i18n.CoreMessages) AbstractDataTypeBuilderFactory(org.mule.runtime.api.metadata.AbstractDataTypeBuilderFactory) ExpressionCompiler(org.mule.mvel2.compiler.ExpressionCompiler) BindingContext(org.mule.runtime.api.el.BindingContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) HashMap(java.util.HashMap) DEFAULT_EXPRESSION_POSTFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_POSTFIX) DataHandler(javax.activation.DataHandler) ValidationResult.success(org.mule.runtime.api.el.ValidationResult.success) CachedMapVariableResolverFactory(org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory) ParserConfiguration(org.mule.mvel2.ParserConfiguration) Inject(javax.inject.Inject) BigDecimal(java.math.BigDecimal) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Collectors.toMap(java.util.stream.Collectors.toMap) MuleContext(org.mule.runtime.core.api.MuleContext) Map(java.util.Map) MimeType(javax.activation.MimeType) Function(org.mule.mvel2.ast.Function) Collections.singletonMap(java.util.Collections.singletonMap) BigInteger(java.math.BigInteger) IOUtils(org.mule.runtime.core.api.util.IOUtils) StringUtils.replace(org.apache.commons.lang3.StringUtils.replace) ValidationResult.failure(org.mule.runtime.api.el.ValidationResult.failure) DEFAULT_EXPRESSION_PREFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_PREFIX) Iterator(java.util.Iterator) DataType(org.mule.runtime.api.metadata.DataType) PREFIX_EXPR_SEPARATOR(org.mule.runtime.core.internal.el.DefaultExpressionManager.PREFIX_EXPR_SEPARATOR) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ValidationResult(org.mule.runtime.api.el.ValidationResult) IOException(java.io.IOException) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) MvelEnricherDataTypePropagator(org.mule.runtime.core.internal.el.mvel.datatype.MvelEnricherDataTypePropagator) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Serializable(java.io.Serializable) TypedValue(org.mule.runtime.api.metadata.TypedValue) MEL_PREFIX(org.mule.runtime.core.internal.el.DefaultExpressionManager.MEL_PREFIX) VariableResolverFactory(org.mule.mvel2.integration.VariableResolverFactory) CoreMessages.objectIsNull(org.mule.runtime.core.api.config.i18n.CoreMessages.objectIsNull) CompileException(org.mule.mvel2.CompileException) Entry(java.util.Map.Entry) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompilerTools(org.mule.mvel2.util.CompilerTools) MvelDataTypeResolver(org.mule.runtime.core.internal.el.mvel.datatype.MvelDataTypeResolver) Pattern(java.util.regex.Pattern) Serializable(java.io.Serializable) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) DataType(org.mule.runtime.api.metadata.DataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 77 with CoreEvent

use of org.mule.runtime.core.api.event.CoreEvent 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 78 with CoreEvent

use of org.mule.runtime.core.api.event.CoreEvent 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 79 with CoreEvent

use of org.mule.runtime.core.api.event.CoreEvent 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 80 with CoreEvent

use of org.mule.runtime.core.api.event.CoreEvent in project mule by mulesoft.

the class PolicyPointcutParametersManager method createOperationPointcutParameters.

/**
 * Creates {@link PolicyPointcutParameters} for a specific operation. Stored parameters from the source are included in the
 * newly created parameters to be able to correlate parameters from both source and operation.
 *
 * @param operation the operation component to which policies will be applied
 * @param event the event which will execute the operation policies
 * @param operationParameters a map containing the parameters of the operation
 * @return the created {@link PolicyPointcutParameters}
 */
public PolicyPointcutParameters createOperationPointcutParameters(Component operation, CoreEvent event, Map<String, Object> operationParameters) {
    ComponentIdentifier operationIdentifier = operation.getLocation().getComponentIdentifier().getIdentifier();
    PolicyPointcutParameters sourceParameters = sourceParametersMap.get(event.getContext().getCorrelationId());
    OperationPolicyPointcutParametersParameters parameters = new OperationPolicyPointcutParametersParameters(operation, operationParameters, sourceParameters);
    Function<OperationPolicyPointcutParametersFactory, PolicyPointcutParameters> creationFunction = factory -> {
        try {
            return factory.createPolicyPointcutParameters(parameters);
        } catch (AbstractMethodError error) {
            return factory.createPolicyPointcutParameters(parameters.getOperation(), parameters.getOperationParameters());
        }
    };
    return createPointcutParameters(operation, OperationPolicyPointcutParametersFactory.class, operationPointcutFactories, factory -> factory.supportsOperationIdentifier(operationIdentifier), creationFunction).orElse(new PolicyPointcutParameters(operation, sourceParameters));
}
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) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) OperationPolicyPointcutParametersParameters(org.mule.runtime.policy.api.OperationPolicyPointcutParametersParameters) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters)

Aggregations

CoreEvent (org.mule.runtime.core.api.event.CoreEvent)485 Test (org.junit.Test)394 Message (org.mule.runtime.api.message.Message)103 SmallTest (org.mule.tck.size.SmallTest)100 TypedValue (org.mule.runtime.api.metadata.TypedValue)44 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)39 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)38 Description (io.qameta.allure.Description)37 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)37 List (java.util.List)36 MuleException (org.mule.runtime.api.exception.MuleException)33 Processor (org.mule.runtime.core.api.processor.Processor)33 DataType (org.mule.runtime.api.metadata.DataType)28 InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)28 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)27 ArrayList (java.util.ArrayList)26 Map (java.util.Map)26 Optional (java.util.Optional)25 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)23 InOrder (org.mockito.InOrder)22