Search in sources :

Example 6 with CursorStreamProvider

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

the class IOUtils method ifInputStream.

public static <T> T ifInputStream(Object value, CheckedFunction<InputStream, T> function) throws NotAnInputStreamException {
    boolean shouldCloseStream = false;
    InputStream stream = null;
    if (value instanceof CursorStreamProvider) {
        stream = ((CursorStreamProvider) value).openCursor();
        shouldCloseStream = true;
    } else if (value instanceof InputStream) {
        stream = (InputStream) value;
    } else {
        throw new NotAnInputStreamException(stream);
    }
    try {
        return function.apply(stream);
    } finally {
        if (shouldCloseStream) {
            closeQuietly(stream);
        }
    }
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Example 7 with CursorStreamProvider

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

the class GZipUncompressTransformer method doTransform.

@Override
public Object doTransform(Object src, Charset outputEncoding) throws TransformerException {
    try {
        if (src instanceof CursorStreamProvider) {
            return getStrategy().uncompressInputStream(((CursorStreamProvider) src).openCursor());
        }
        if (src instanceof InputStream) {
            return getStrategy().uncompressInputStream((InputStream) src);
        } else {
            byte[] buffer = getStrategy().uncompressByteArray((byte[]) src);
            DataType returnDataType = getReturnDataType();
            // If a return type has been specified, then deserialize the uncompressed byte array.
            if (DataType.STRING.isCompatibleWith(returnDataType)) {
                return new String(buffer, outputEncoding);
            } else if (!DataType.OBJECT.isCompatibleWith(returnDataType) && !DataType.BYTE_ARRAY.isCompatibleWith(returnDataType)) {
                try {
                    return muleContext.getObjectSerializer().getExternalProtocol().deserialize(buffer);
                } catch (SerializationException e) {
                    throw new TransformerException(this, e);
                }
            } else {
                // First try to deserialize the byte array. If it can be deserialized, then it was originally serialized.
                try {
                    return muleContext.getObjectSerializer().getExternalProtocol().deserialize(buffer);
                } catch (SerializationException e) {
                    // If it fails, ignore it. We assume it was not serialized in the first place and return the buffer as it is.
                    return buffer;
                }
            }
        }
    } catch (IOException e) {
        throw new TransformerException(I18nMessageFactory.createStaticMessage("Failed to uncompress message."), this, e);
    }
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) SerializationException(org.mule.runtime.api.serialization.SerializationException) InputStream(java.io.InputStream) DataType(org.mule.runtime.api.metadata.DataType) IOException(java.io.IOException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 8 with CursorStreamProvider

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

the class PetStoreSerializableParameterTestCase method cursorStreamProviderParameter.

@Test
public void cursorStreamProviderParameter() throws Exception {
    InputStream inputStream = new ByteArrayInputStream(DONKEY.getBytes());
    CursorStreamProvider provider = new InMemoryCursorStreamProvider(inputStream, InMemoryCursorStreamConfig.getDefault(), new SimpleByteBufferManager());
    Message message = flowRunner("dynamicSerializableParameter").withVariable("animal", provider).run().getMessage();
    assertThat(message.getPayload().getValue(), is(DONKEY));
}
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) Message(org.mule.runtime.api.message.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InMemoryCursorStreamProvider(org.mule.runtime.core.api.streaming.bytes.InMemoryCursorStreamProvider) Test(org.junit.Test)

Example 9 with CursorStreamProvider

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

the class BytesStreamingExtensionTestCase method streamProviderSerialization.

@Test
@Description("A stream provider is serialized as a byte[]")
public void streamProviderSerialization() throws Exception {
    CursorStreamProvider provider = (CursorStreamProvider) flowRunner("toStream").keepStreamsOpen().withPayload(data).run().getMessage().getPayload().getValue();
    byte[] bytes = muleContext.getObjectSerializer().getInternalProtocol().serialize(provider);
    bytes = muleContext.getObjectSerializer().getInternalProtocol().deserialize(bytes);
    assertThat(new String(bytes, Charset.defaultCharset()), equalTo(data));
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 10 with CursorStreamProvider

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

the class ReconnectionWithStreamingTestCase method cursorComingFromProviderIsResetOnReconnection.

@Test
public void cursorComingFromProviderIsResetOnReconnection() throws Exception {
    CursorStream cursorStream = createMockCursor();
    CursorStreamProvider provider = mock(CursorStreamProvider.class);
    when(provider.openCursor()).thenReturn(cursorStream);
    assertReconnection(cursorStream, provider);
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream) 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