Search in sources :

Example 6 with StringDataSource

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);
    }
}
Also used : Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) TempFileIOChannel(org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException)

Example 7 with StringDataSource

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

Example 8 with StringDataSource

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

StringDataSource (org.ballerinalang.runtime.message.StringDataSource)8 BString (org.ballerinalang.model.values.BString)3 BStruct (org.ballerinalang.model.values.BStruct)3 Test (org.testng.annotations.Test)3 BJSON (org.ballerinalang.model.values.BJSON)2 BXML (org.ballerinalang.model.values.BXML)2 BlobDataSource (org.ballerinalang.runtime.message.BlobDataSource)2 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)2 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)2 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 BBlob (org.ballerinalang.model.values.BBlob)1 BValue (org.ballerinalang.model.values.BValue)1 BallerinaIOException (org.ballerinalang.nativeimpl.io.BallerinaIOException)1 TempFileIOChannel (org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel)1 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)1 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)1