Search in sources :

Example 6 with MessageDataSource

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");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 7 with MessageDataSource

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();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource)

Example 8 with MessageDataSource

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]));
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BStruct(org.ballerinalang.model.values.BStruct) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource) BBlob(org.ballerinalang.model.values.BBlob)

Example 9 with MessageDataSource

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);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource) StringDataSource(org.ballerinalang.runtime.message.StringDataSource)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)9 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)9 BBlob (org.ballerinalang.model.values.BBlob)2 HttpResponseFuture (org.wso2.transport.http.netty.contract.HttpResponseFuture)2 BJSON (org.ballerinalang.model.values.BJSON)1 BString (org.ballerinalang.model.values.BString)1 BValue (org.ballerinalang.model.values.BValue)1 BXML (org.ballerinalang.model.values.BXML)1 BlobDataSource (org.ballerinalang.runtime.message.BlobDataSource)1 StringDataSource (org.ballerinalang.runtime.message.StringDataSource)1 Test (org.testng.annotations.Test)1