use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class EntityBodyHandler method writeByteChannelToOutputStream.
/**
* Write byte channel stream directly into outputstream without converting it to a data source.
*
* @param entityStruct Represent a ballerina entity
* @param messageOutputStream Represent the outputstream that the message should be written to
* @throws IOException When an error occurs while writing inputstream to outputstream
*/
public static void writeByteChannelToOutputStream(BStruct entityStruct, OutputStream messageOutputStream) throws IOException {
Channel byteChannel = EntityBodyHandler.getByteChannel(entityStruct);
if (byteChannel != null) {
MimeUtil.writeInputToOutputStream(byteChannel.getInputStream(), messageOutputStream);
byteChannel.close();
}
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class EntityBodyHandler method constructStringDataSource.
/**
* Construct StringDataSource from the underneath byte channel which is associated with the entity struct.
*
* @param entityStruct Represent an entity struct
* @return StringDataSource which represent the entity body which is kept in memory
*/
public static StringDataSource constructStringDataSource(BStruct entityStruct) {
try {
Channel byteChannel = getByteChannel(entityStruct);
if (byteChannel == null) {
throw new BallerinaIOException("Payload is null");
}
String textContent = StringUtils.getStringFromInputStream(byteChannel.getInputStream());
byteChannel.close();
return new StringDataSource(textContent);
} catch (IOException e) {
throw new BallerinaIOException("Error occurred while closing the channel", e);
}
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class EntityBodyHandler method constructBlobDataSource.
/**
* Construct BlobDataSource from the underneath byte channel which is associated with the entity struct.
*
* @param entityStruct Represent an entity struct
* @return BlobDataSource Data source for binary data which is kept in memory
* @throws IOException In case an error occurred while creating blob data source
*/
public static BlobDataSource constructBlobDataSource(BStruct entityStruct) throws IOException {
Channel byteChannel = getByteChannel(entityStruct);
if (byteChannel == null) {
return null;
}
byte[] byteData = MimeUtil.getByteArray(byteChannel.getInputStream());
byteChannel.close();
return new BlobDataSource(byteData);
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class GetBodyParts method execute.
@Override
public void execute(Context context) {
BRefValueArray partsArray = new BRefValueArray();
try {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
// Get the body parts from entity's multipart data field, if they've been already been decoded
partsArray = EntityBodyHandler.getBodyPartArray(entityStruct);
if (partsArray == null || partsArray.size() < 1) {
Channel byteChannel = EntityBodyHandler.getByteChannel(entityStruct);
if (byteChannel != null) {
EntityBodyHandler.decodeEntityBody(context, entityStruct, byteChannel);
// Check the body part availability for the second time, since the parts will be by this
// time populated from bytechannel
partsArray = EntityBodyHandler.getBodyPartArray(entityStruct);
// Set byte channel that belongs to parent entity to null, once the message body parts have
// been decoded
entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, null);
}
}
} catch (Throwable e) {
context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while extracting body parts from entity: " + e.getMessage()));
}
context.setReturnValues(partsArray);
}
Aggregations