use of org.ballerinalang.runtime.message.StringDataSource 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.runtime.message.StringDataSource in project ballerina by ballerina-lang.
the class SetText method execute.
@Override
public void execute(Context context) {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
String textContent = context.getStringArgument(FIRST_PARAMETER_INDEX);
EntityBodyHandler.addMessageDataSource(entityStruct, new StringDataSource(textContent));
context.setReturnValues();
}
use of org.ballerinalang.runtime.message.StringDataSource in project ballerina by ballerina-lang.
the class GetText method execute.
@Override
public void execute(Context context) {
BString result;
try {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
MessageDataSource dataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (dataSource != null) {
result = new BString(dataSource.getMessageAsString());
} else {
StringDataSource stringDataSource = EntityBodyHandler.constructStringDataSource(entityStruct);
result = new BString(stringDataSource.getMessageAsString());
EntityBodyHandler.addMessageDataSource(entityStruct, stringDataSource);
// Set byte channel to null, once the message data source has been constructed
entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, null);
}
} catch (Throwable e) {
context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while retrieving text data from entity : " + e.getMessage()));
return;
}
context.setReturnValues(result);
}
Aggregations