Search in sources :

Example 76 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class AbstractForkJoinStrategyFactory method mergeVariables.

private Consumer<List<CoreEvent>> mergeVariables(CoreEvent original, CoreEvent.Builder result) {
    return list -> {
        Map<String, TypedValue> routeVars = new HashMap<>();
        list.forEach(event -> event.getVariables().forEach((key, value) -> {
            // Only merge variables that have been added or mutated in routes
            if (!value.equals(original.getVariables().get(key))) {
                if (!routeVars.containsKey(key)) {
                    // A new variable that hasn't already been set by another route is added as a simple entry.
                    routeVars.put(key, value);
                } else {
                    // If a variable already exists from before route, or was set in a previous route, then it's added to a list of 1.
                    if (!(routeVars.get(key).getValue() instanceof List)) {
                        List newList = new ArrayList();
                        newList.add(routeVars.get(key).getValue());
                        routeVars.put(key, new TypedValue(newList, DataType.builder().collectionType(List.class).itemType(routeVars.get(key).getDataType().getType()).build()));
                    }
                    List valueList = (List) routeVars.get(key).getValue();
                    valueList.add(value.getValue());
                    if (((CollectionDataType) routeVars.get(key).getDataType()).getItemDataType().isCompatibleWith(value.getDataType())) {
                        // If item types are compatible then data type is conserved
                        routeVars.put(key, new TypedValue(valueList, routeVars.get(key).getDataType()));
                    } else {
                        // Else Object item type is used.
                        routeVars.put(key, new TypedValue(valueList, DataType.builder().collectionType(List.class).build()));
                    }
                }
            }
        }));
        routeVars.forEach((s, typedValue) -> result.addVariable(s, typedValue.getValue(), typedValue.getDataType()));
    };
}
Also used : Optional.empty(java.util.Optional.empty) CompositeRoutingException(org.mule.runtime.core.privileged.routing.CompositeRoutingException) Message(org.mule.runtime.api.message.Message) Flux.from(reactor.core.publisher.Flux.from) Mono.defer(reactor.core.publisher.Mono.defer) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Function(java.util.function.Function) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scheduler(org.mule.runtime.api.scheduler.Scheduler) GroupCorrelation(org.mule.runtime.core.api.message.GroupCorrelation) Map(java.util.Map) RoutingResult(org.mule.runtime.core.privileged.routing.RoutingResult) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Schedulers(reactor.core.scheduler.Schedulers) Mono.error(reactor.core.publisher.Mono.error) Error(org.mule.runtime.api.message.Error) Exceptions.checkedConsumer(org.mule.runtime.core.api.rx.Exceptions.checkedConsumer) DataType(org.mule.runtime.api.metadata.DataType) Exceptions.propagate(reactor.core.Exceptions.propagate) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ForkJoinStrategy(org.mule.runtime.core.internal.routing.ForkJoinStrategy) ForkJoinStrategyFactory(org.mule.runtime.core.internal.routing.ForkJoinStrategyFactory) Publisher(org.reactivestreams.Publisher) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) Mono(reactor.core.publisher.Mono) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) ErrorBuilder(org.mule.runtime.core.internal.message.ErrorBuilder) Consumer(java.util.function.Consumer) TypedValue(org.mule.runtime.api.metadata.TypedValue) List(java.util.List) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) RoutingPair.of(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair.of) Duration.ofMillis(java.time.Duration.ofMillis) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 77 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class Foreach method splitAndProcess.

private Publisher<CoreEvent> splitAndProcess(CoreEvent request) {
    AtomicInteger count = new AtomicInteger();
    final AtomicReference<CoreEvent> currentEvent = new AtomicReference<>(request);
    // Split into sequence of TypedValue
    return fromIterable(() -> splitRequest(request)).onErrorMap(throwable -> new MessagingException(request, throwable, Foreach.this)).transform(p -> batchSize > 1 ? from(p).buffer(batchSize).map(list -> new TypedValue<>(list, fromObject(list))) : p).flatMapSequential(typedValue -> {
        EventContext parentContext = currentEvent.get().getContext();
        BaseEventContext childContext = newChildContext(currentEvent.get(), ofNullable(getLocation()));
        Builder partEventBuilder = builder(childContext, currentEvent.get());
        if (typedValue.getValue() instanceof EventBuilderConfigurer) {
            // Support EventBuilderConfigurer currently used by Batch Module
            EventBuilderConfigurer configurer = (EventBuilderConfigurer) typedValue.getValue();
            configurer.configure(partEventBuilder);
            childContext.onResponse((e, t) -> {
                configurer.eventCompleted();
            });
        } else if (typedValue.getValue() instanceof Message) {
            // If value is a Message then use it directly conserving attributes and properties.
            partEventBuilder.message((Message) typedValue.getValue());
        } else {
            // Otherwise create a new message
            partEventBuilder.message(Message.builder().payload(typedValue).build());
        }
        return Mono.from(just(partEventBuilder.addVariable(counterVariableName, count.incrementAndGet()).build()).transform(nestedChain).doOnNext(completeSuccessIfNeeded(childContext, true)).switchIfEmpty(Mono.from(childContext.getResponsePublisher())).map(result -> builder(parentContext, result).build()).doOnNext(result -> currentEvent.set(CoreEvent.builder(result).build())).doOnError(MessagingException.class, me -> me.setProcessedEvent(builder(parentContext, me.getEvent()).build())).doOnSuccess(result -> {
            if (result == null) {
                childContext.success();
            }
        }));
    }, // Force sequential execution of the chain for each element
    1).switchIfEmpty(defer(() -> {
        if (count.get() == 0) {
            logger.warn("Split expression returned no results. If this is not expected please check your expression");
            return just(request);
        } else {
            return empty();
        }
    })).takeLast(1).map(s -> CoreEvent.builder(currentEvent.get()).message(request.getMessage()).build()).errorStrategyStop();
}
Also used : MessageProcessors.newChain(org.mule.runtime.core.privileged.processor.MessageProcessors.newChain) Message(org.mule.runtime.api.message.Message) Flux.from(reactor.core.publisher.Flux.from) Mono.defer(reactor.core.publisher.Mono.defer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject) Processor(org.mule.runtime.core.api.processor.Processor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) Iterators(com.google.common.collect.Iterators) Collections.singletonList(java.util.Collections.singletonList) Scope(org.mule.runtime.core.privileged.processor.Scope) MuleException(org.mule.runtime.api.exception.MuleException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProcessors.newChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.newChildContext) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) Map(java.util.Map) Mono.just(reactor.core.publisher.Mono.just) AbstractMessageProcessorOwner(org.mule.runtime.core.api.processor.AbstractMessageProcessorOwner) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) EventBuilderConfigurer(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer) Mono.empty(reactor.core.publisher.Mono.empty) DEFAULT_SPLIT_EXPRESSION(org.mule.runtime.core.internal.routing.ExpressionSplittingStrategy.DEFAULT_SPLIT_EXPRESSION) EventContext(org.mule.runtime.api.event.EventContext) Iterator(java.util.Iterator) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Flux.fromIterable(reactor.core.publisher.Flux.fromIterable) Optional.ofNullable(java.util.Optional.ofNullable) MessageProcessors.completeSuccessIfNeeded(org.mule.runtime.core.privileged.processor.MessageProcessors.completeSuccessIfNeeded) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) EventBuilderConfigurerList(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerList) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) TypedValue(org.mule.runtime.api.metadata.TypedValue) List(java.util.List) Optional(java.util.Optional) EventBuilderConfigurerIterator(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerIterator) MessageProcessors.getProcessingStrategy(org.mule.runtime.core.privileged.processor.MessageProcessors.getProcessingStrategy) EventContext(org.mule.runtime.api.event.EventContext) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) EventBuilderConfigurer(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer) Message(org.mule.runtime.api.message.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) AtomicReference(java.util.concurrent.atomic.AtomicReference) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 78 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class DefaultMuleSession method getProperties.

public Map<String, Object> getProperties() {
    Map<String, Object> result = new HashMap<>();
    for (String key : properties.keySet()) {
        TypedValue typedValue = properties.get(key);
        result.put(key, typedValue.getValue());
    }
    return result;
}
Also used : CaseInsensitiveHashMap(org.mule.runtime.core.api.util.CaseInsensitiveHashMap) HashMap(java.util.HashMap) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 79 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class SoapOperationExecutor method toSoapAttachments.

private Map<String, SoapAttachment> toSoapAttachments(Map<String, TypedValue<?>> attachments) throws MessageTransformerException, TransformerException {
    Map<String, SoapAttachment> soapAttachmentMap = new HashMap<>();
    for (Map.Entry<String, TypedValue<?>> attachment : attachments.entrySet()) {
        SoapAttachment soapAttachment = new SoapAttachment(toInputStream(attachment.getValue()), attachment.getValue().getDataType().getMediaType());
        soapAttachmentMap.put(attachment.getKey(), soapAttachment);
    }
    return soapAttachmentMap;
}
Also used : SoapAttachment(org.mule.runtime.extension.api.soap.SoapAttachment) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 80 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class FunctionExecutionTestCase method echoFromManager.

@Test
public void echoFromManager() {
    TypedValue result = expressionManager.evaluate("Fn::defaultPrimitives()");
    assertThat(result.getValue(), is("SUCCESS"));
}
Also used : TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Aggregations

TypedValue (org.mule.runtime.api.metadata.TypedValue)97 Test (org.junit.Test)74 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)47 DataType (org.mule.runtime.api.metadata.DataType)17 Message (org.mule.runtime.api.message.Message)16 Description (io.qameta.allure.Description)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 List (java.util.List)11 SmallTest (org.mule.tck.size.SmallTest)10 BindingContext (org.mule.runtime.api.el.BindingContext)9 Map (java.util.Map)8 Optional (java.util.Optional)8 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)5 ArrayList (java.util.ArrayList)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MuleException (org.mule.runtime.api.exception.MuleException)4 Error (org.mule.runtime.api.message.Error)4 ErrorType (org.mule.runtime.api.message.ErrorType)4