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