use of org.mule.runtime.api.streaming.object.CursorIteratorProvider in project mule by mulesoft.
the class StreamingUtilsTestCase method consumeRepeatableIteratorPayload.
@Test
@Description("Test that repeatable iterator is consumed into a list")
public void consumeRepeatableIteratorPayload() throws Exception {
CursorIteratorProvider payload = asCursorProvider(TEST_LIST);
CoreEvent event = consumeRepeatablePayload(getEventBuilder().message(Message.of(payload)).build());
assertConsumedObjectStream(payload, event);
}
use of org.mule.runtime.api.streaming.object.CursorIteratorProvider in project mule by mulesoft.
the class CursorManager method manage.
/**
* Becomes aware of the given {@code provider} and returns a replacement provider which is managed by the runtime, allowing for
* automatic resource handling
*
* @param provider the provider to be tracked
* @param creatorEvent the event that created the provider
* @return a {@link CursorContext}
*/
public CursorProvider manage(CursorProvider provider, CoreEvent creatorEvent) {
final BaseEventContext ownerContext = ((BaseEventContext) creatorEvent.getContext()).getRootContext();
registerEventContext(ownerContext);
registry.getUnchecked(ownerContext.getId()).addProvider(provider);
final CursorContext context = new CursorContext(provider, ownerContext);
if (provider instanceof CursorStreamProvider) {
return new ManagedCursorStreamProvider(context, this);
} else if (provider instanceof CursorIteratorProvider) {
return new ManagedCursorIteratorProvider(context, this);
}
throw new MuleRuntimeException(createStaticMessage("Unknown cursor provider type: " + context.getClass().getName()));
}
use of org.mule.runtime.api.streaming.object.CursorIteratorProvider 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.streaming.object.CursorIteratorProvider in project mule by mulesoft.
the class EventToMessageSequenceSplittingStrategy method split.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public MessageSequence<?> split(CoreEvent event) {
if (expressionSplitterStrategy.hasDefaultExpression()) {
Message msg = event.getMessage();
Object payload = msg.getPayload().getValue();
if (payload instanceof MessageSequence<?>) {
return ((MessageSequence<?>) payload);
}
if (payload instanceof Iterator<?>) {
return new IteratorMessageSequence(((Iterator<Object>) payload));
}
if (payload instanceof Collection) {
return new CollectionMessageSequence(copyCollection((Collection) payload));
} else if (payload instanceof CursorIteratorProvider) {
return new IteratorMessageSequence(((CursorIteratorProvider) payload).openCursor());
} else if (payload instanceof Iterable<?>) {
return new IteratorMessageSequence(((Iterable<Object>) payload).iterator());
} else if (payload instanceof Object[]) {
return new ArrayMessageSequence((Object[]) payload);
} else if (payload instanceof NodeList) {
return new NodeListMessageSequence((NodeList) payload);
} else if (payload instanceof Map<?, ?>) {
List<Object> list = new LinkedList<>();
Set<Map.Entry<?, ?>> set = ((Map) payload).entrySet();
for (Map.Entry<?, ?> entry : set) {
list.add(new ImmutableEntry<>(entry));
}
return new CollectionMessageSequence(list);
}
}
try {
Iterator<TypedValue<?>> valueIterator = expressionSplitterStrategy.split(event);
if (hasMelExpression(expressionSplitterStrategy.getExpression())) {
List<Object> iteratorCollection = new ArrayList<>();
valueIterator.forEachRemaining(iteratorCollection::add);
return new CollectionMessageSequence<>(iteratorCollection);
}
return new IteratorMessageSequence(valueIterator);
} catch (ExpressionRuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalArgumentException(format("Could not split result of expression %s. The provided value is not instance of %s java " + "type or it's not a collection in any other format", expressionSplitterStrategy.getExpression(), new Class[] { Iterable.class, Iterator.class, MessageSequence.class, Collection.class }), e);
}
}
use of org.mule.runtime.api.streaming.object.CursorIteratorProvider in project mule by mulesoft.
the class DefaultStreamingHelperTestCase method resolveIteratorProvider.
@Test
public void resolveIteratorProvider() {
CursorIteratorProvider streamProvider = (CursorIteratorProvider) streamingHelper.resolveCursorProvider(valueList.iterator());
CursorIterator cursor = streamProvider.openCursor();
valueList.forEach(value -> {
assertThat(cursor.hasNext(), is(true));
assertThat(value, equalTo(cursor.next()));
});
assertThat(cursor.hasNext(), is(false));
}
Aggregations