Search in sources :

Example 1 with CollectionDataType

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

the class StreamingUtils method consumeRepeatablePayload.

/**
 * If the {@code event} has a repeatable payload (instance of {@link CursorProvider}), then this method returns a new
 * event which payload has an equivalent, already consumed structure. This functionality makes sense for cases like
 * caching in which the contents of the stream need to survive the completion of the event that generated it.
 * <p>
 * If the payload is a {@link CursorStreamProvider}, then it will be consumed into a {@link ByteArrayCursorStreamProvider}
 * so that the contents are fully in memory while still keeping repeatable byte streaming semantics.
 * <p>
 * If the payload is a {@link CursorIteratorProvider}, then the contents will be consumed into a {@link List}.
 * <p>
 * In any other case, the same input event is returned
 *
 * @param event an event which might have a repeatable payload
 * @return a {@link CoreEvent}
 * @since 4.1
 */
public static CoreEvent consumeRepeatablePayload(CoreEvent event) {
    TypedValue payload = event.getMessage().getPayload();
    final Object originalPayload = payload.getValue();
    if (originalPayload == null) {
        return event;
    }
    DataType originalDataType = payload.getDataType();
    TypedValue replacedPayload = null;
    if (originalPayload instanceof CursorStreamProvider) {
        Object consumedPayload = asCursorProvider(toByteArray((CursorStreamProvider) originalPayload));
        replacedPayload = new TypedValue(consumedPayload, DataType.builder(originalDataType).type(consumedPayload.getClass()).build());
    } else if (originalPayload instanceof CursorIteratorProvider) {
        List consumed = new LinkedList<>();
        ((CursorIteratorProvider) originalPayload).openCursor().forEachRemaining(consumed::add);
        DataType newDataType;
        if (originalDataType instanceof CollectionDataType) {
            final CollectionDataType collectionDataType = (CollectionDataType) originalDataType;
            newDataType = DataType.builder(originalDataType).collectionType(consumed.getClass()).itemType(collectionDataType.getItemDataType().getType()).itemMediaType(collectionDataType.getItemDataType().getMediaType()).build();
        } else {
            newDataType = DataType.builder(originalDataType).type(consumed.getClass()).build();
        }
        replacedPayload = new TypedValue(consumed, newDataType);
    }
    if (replacedPayload != null) {
        event = CoreEvent.builder(event).message(Message.builder(event.getMessage()).payload(replacedPayload).build()).build();
    }
    return event;
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) ByteArrayCursorStreamProvider(org.mule.runtime.core.internal.streaming.bytes.ByteArrayCursorStreamProvider) ListCursorIteratorProvider(org.mule.runtime.core.internal.streaming.object.ListCursorIteratorProvider) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) List(java.util.List) LinkedList(java.util.LinkedList) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 2 with CollectionDataType

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

the class OperationMessageProcessorTestCase method operationReturnsResultCollectionWithCorrectDataType.

@Test
public void operationReturnsResultCollectionWithCorrectDataType() throws Exception {
    Object payload = new ArrayList<>();
    setUpOperationReturning(Result.builder().output(payload).build(), new TypeToken<List<Integer>>() {
    }.getType());
    Message message = messageProcessor.process(event).getMessage();
    assertThat(message, is(notNullValue()));
    assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
    DataType dataType = message.getPayload().getDataType();
    assertThat(dataType, instanceOf(CollectionDataType.class));
    assertThat(((CollectionDataType) dataType).getItemDataType(), like(Integer.class, ANY.withCharset(null)));
}
Also used : Message(org.mule.runtime.api.message.Message) TypeToken(com.google.common.reflect.TypeToken) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) ArrayList(java.util.ArrayList) MapDataType(org.mule.runtime.api.metadata.MapDataType) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with CollectionDataType

use of org.mule.runtime.api.metadata.CollectionDataType 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 4 with CollectionDataType

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

the class OperationMessageProcessorTestCase method operationReturnsCollectionWithCorrectDataType.

@Test
public void operationReturnsCollectionWithCorrectDataType() throws Exception {
    Object payload = new ArrayList<>();
    setUpOperationReturning(payload, new TypeToken<List<Integer>>() {
    }.getType());
    Message message = messageProcessor.process(event).getMessage();
    assertThat(message, is(notNullValue()));
    assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
    DataType dataType = message.getPayload().getDataType();
    assertThat(dataType, instanceOf(CollectionDataType.class));
    assertThat(((CollectionDataType) dataType).getItemDataType(), like(Integer.class, ANY.withCharset(null)));
}
Also used : Message(org.mule.runtime.api.message.Message) TypeToken(com.google.common.reflect.TypeToken) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) ArrayList(java.util.ArrayList) MapDataType(org.mule.runtime.api.metadata.MapDataType) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 5 with CollectionDataType

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

the class IntrospectionUtilsTestCase method getDataTypeFromList.

@Test
public void getDataTypeFromList() {
    Class<List> listClass = List.class;
    Class<Integer> integerClass = Integer.class;
    ArrayTypeBuilder arrayTypeBuilder = BaseTypeBuilder.create(JAVA).arrayType().with(new ClassInformationAnnotation(listClass));
    arrayTypeBuilder.of().numberType().integer();
    CollectionDataType dataType = (CollectionDataType) toDataType(arrayTypeBuilder.build());
    assertThat(dataType.getType(), is(equalTo(listClass)));
    assertThat(dataType.getItemDataType().getType(), is(equalTo(integerClass)));
}
Also used : CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) List(java.util.List) ArrayList(java.util.ArrayList) ArrayTypeBuilder(org.mule.metadata.api.builder.ArrayTypeBuilder) ClassInformationAnnotation(org.mule.metadata.java.api.annotation.ClassInformationAnnotation) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

CollectionDataType (org.mule.runtime.api.metadata.CollectionDataType)6 DataType (org.mule.runtime.api.metadata.DataType)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 SmallTest (org.mule.tck.size.SmallTest)4 List (java.util.List)3 Message (org.mule.runtime.api.message.Message)3 TypeToken (com.google.common.reflect.TypeToken)2 MapDataType (org.mule.runtime.api.metadata.MapDataType)2 TypedValue (org.mule.runtime.api.metadata.TypedValue)2 CursorIteratorProvider (org.mule.runtime.api.streaming.object.CursorIteratorProvider)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 Description (io.qameta.allure.Description)1 Duration.ofMillis (java.time.Duration.ofMillis)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1