Search in sources :

Example 1 with CursorStreamProvider

use of org.mule.runtime.api.streaming.bytes.CursorStreamProvider in project mule by mulesoft.

the class StreamingUtilsTestCase method consumeJsonRepeatableInputStreamPayload.

@Test
@Description("Test that repeatable stream in the payload is consumed into another fully in memory stream provider while maintaining " + "the original media type")
public void consumeJsonRepeatableInputStreamPayload() throws Exception {
    CursorStreamProvider payload = asCursorProvider(TEST_PAYLOAD);
    CoreEvent event = consumeRepeatablePayload(getEventBuilder().message(Message.builder().payload(TypedValue.of(payload)).mediaType(APPLICATION_JSON).build()).build());
    assertConsumedRepeatableInputStream(payload, event);
    assertThat(event.getMessage().getPayload().getDataType().getMediaType(), is(APPLICATION_JSON));
}
Also used : InMemoryCursorStreamProvider(org.mule.runtime.core.api.streaming.bytes.InMemoryCursorStreamProvider) CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) ByteArrayCursorStreamProvider(org.mule.runtime.core.internal.streaming.bytes.ByteArrayCursorStreamProvider) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Description(io.qameta.allure.Description) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 2 with CursorStreamProvider

use of org.mule.runtime.api.streaming.bytes.CursorStreamProvider 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()));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ManagedCursorStreamProvider(org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider) CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) ManagedCursorIteratorProvider(org.mule.runtime.core.internal.streaming.object.ManagedCursorIteratorProvider) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) ManagedCursorStreamProvider(org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ManagedCursorIteratorProvider(org.mule.runtime.core.internal.streaming.object.ManagedCursorIteratorProvider)

Example 3 with CursorStreamProvider

use of org.mule.runtime.api.streaming.bytes.CursorStreamProvider 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 4 with CursorStreamProvider

use of org.mule.runtime.api.streaming.bytes.CursorStreamProvider in project mule by mulesoft.

the class CustomJavaSerializationProtocol method doSerialize.

/**
 * {@inheritDoc}
 */
@Override
protected byte[] doSerialize(Object object) throws Exception {
    // TODO: MULE-11939
    if (object instanceof CursorStreamProvider) {
        try (CursorStream cursor = ((CursorStreamProvider) object).openCursor()) {
            object = toByteArray(cursor);
        }
    }
    validateForSerialization(object);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(512);
    try (ObjectOutputStream out = new ArtifactClassLoaderObjectOutputStream(classLoaderRepository, outputStream)) {
        out.writeObject(object);
    } catch (IOException ex) {
        throw new SerializationException("Cannot serialize object", ex);
    }
    return outputStream.toByteArray();
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) SerializationException(org.mule.runtime.api.serialization.SerializationException) ArtifactClassLoaderObjectOutputStream(org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ArtifactClassLoaderObjectOutputStream(org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream)

Example 5 with CursorStreamProvider

use of org.mule.runtime.api.streaming.bytes.CursorStreamProvider in project mule by mulesoft.

the class InputStreamToByteArrayTestCase method transformCursorStreamProvider.

@Test
public void transformCursorStreamProvider() throws Exception {
    InputStream inputStream = new ByteArrayInputStream(DONKEY.getBytes());
    CursorStreamProvider provider = new InMemoryCursorStreamProvider(inputStream, InMemoryCursorStreamConfig.getDefault(), new SimpleByteBufferManager());
    assertThat(transformer.transform(provider), equalTo(DONKEY.getBytes()));
}
Also used : InMemoryCursorStreamProvider(org.mule.runtime.core.api.streaming.bytes.InMemoryCursorStreamProvider) CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) SimpleByteBufferManager(org.mule.tck.core.streaming.SimpleByteBufferManager) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InMemoryCursorStreamProvider(org.mule.runtime.core.api.streaming.bytes.InMemoryCursorStreamProvider) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

CursorStreamProvider (org.mule.runtime.api.streaming.bytes.CursorStreamProvider)12 Test (org.junit.Test)7 InputStream (java.io.InputStream)5 InMemoryCursorStreamProvider (org.mule.runtime.core.api.streaming.bytes.InMemoryCursorStreamProvider)4 SmallTest (org.mule.tck.size.SmallTest)4 Description (io.qameta.allure.Description)3 ByteArrayCursorStreamProvider (org.mule.runtime.core.internal.streaming.bytes.ByteArrayCursorStreamProvider)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 DataType (org.mule.runtime.api.metadata.DataType)2 SerializationException (org.mule.runtime.api.serialization.SerializationException)2 CursorStream (org.mule.runtime.api.streaming.bytes.CursorStream)2 CursorIteratorProvider (org.mule.runtime.api.streaming.object.CursorIteratorProvider)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 SimpleByteBufferManager (org.mule.tck.core.streaming.SimpleByteBufferManager)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1