use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class RequestNativeFunctionSuccessTest method testSetBinaryPayload.
@Test(description = "Test setBinaryPayload() function")
public void testSetBinaryPayload() {
BBlob value = new BBlob("Ballerina".getBytes());
BValue[] inputArg = { value };
BValue[] returnVals = BRunUtil.invoke(result, "testSetBinaryPayload", inputArg);
Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
Assert.assertTrue(returnVals[0] instanceof BStruct);
BStruct entity = (BStruct) ((BStruct) returnVals[0]).getNativeData(MESSAGE_ENTITY);
MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entity);
Assert.assertEquals(messageDataSource.getMessageAsString(), "Ballerina", "Payload is not set properly");
}
use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class SetByteChannel method execute.
@Override
public void execute(Context context) {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
BStruct byteChannel = (BStruct) context.getRefArgument(SECOND_PARAMETER_INDEX);
entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, byteChannel.getNativeData(IOConstants.BYTE_CHANNEL_NAME));
MessageDataSource dataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (dataSource != null) {
// Clear message data source when the user set a byte channel to entity
entityStruct.addNativeData(MESSAGE_DATA_SOURCE, null);
}
context.setReturnValues();
}
use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class GetBlob method execute.
@Override
public void execute(Context context) {
BlobDataSource result = null;
try {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (messageDataSource != null) {
result = (BlobDataSource) messageDataSource;
} else {
result = EntityBodyHandler.constructBlobDataSource(entityStruct);
EntityBodyHandler.addMessageDataSource(entityStruct, result);
// 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 extracting blob data from entity : " + e.getMessage()));
}
context.setReturnValues(new BBlob(result != null ? result.getValue() : new byte[0]));
}
use of org.ballerinalang.runtime.message.MessageDataSource 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