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;
}
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)));
}
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()));
};
}
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)));
}
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)));
}
Aggregations