Search in sources :

Example 1 with BlobDataSource

use of org.ballerinalang.runtime.message.BlobDataSource in project ballerina by ballerina-lang.

the class MultipartEncoderTest method validateBodyPartContent.

/**
 * Validate that the decoded body part content matches with the encoded content.
 *
 * @param mimeParts List of decoded body parts
 * @param bodyPart  Ballerina body part
 * @throws IOException When an exception occurs during binary data decoding
 */
private void validateBodyPartContent(List<MIMEPart> mimeParts, BStruct bodyPart) throws IOException {
    EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(0));
    BJSON jsonData = EntityBodyHandler.constructJsonDataSource(bodyPart);
    Assert.assertNotNull(jsonData);
    Assert.assertEquals(jsonData.getMessageAsString(), "{\"" + "bodyPart" + "\":\"" + "jsonPart" + "\"}");
    EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(1));
    BXML xmlData = EntityBodyHandler.constructXmlDataSource(bodyPart);
    Assert.assertNotNull(xmlData);
    Assert.assertEquals(xmlData.getMessageAsString(), "<name>Ballerina xml file part</name>");
    EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(2));
    StringDataSource textData = EntityBodyHandler.constructStringDataSource(bodyPart);
    Assert.assertNotNull(textData);
    Assert.assertEquals(textData.getMessageAsString(), "Ballerina text body part");
    EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(3));
    BlobDataSource blobDataSource = EntityBodyHandler.constructBlobDataSource(bodyPart);
    Assert.assertNotNull(blobDataSource);
    Assert.assertEquals(blobDataSource.getMessageAsString(), "Ballerina binary file part");
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BXML(org.ballerinalang.model.values.BXML) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with BlobDataSource

use of org.ballerinalang.runtime.message.BlobDataSource in project ballerina by ballerina-lang.

the class HttpDispatcher method populateAndGetEntityBody.

private static BValue populateAndGetEntityBody(HttpResource httpResource, BStruct inRequest, BStruct inRequestEntity, BType entityBodyType) throws IOException {
    HttpUtil.populateEntityBody(null, inRequest, inRequestEntity, true);
    switch(entityBodyType.getTag()) {
        case TypeTags.STRING_TAG:
            StringDataSource stringDataSource = EntityBodyHandler.constructStringDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, stringDataSource);
            return stringDataSource != null ? new BString(stringDataSource.getMessageAsString()) : null;
        case TypeTags.JSON_TAG:
            BJSON bjson = EntityBodyHandler.constructJsonDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, bjson);
            return bjson;
        case TypeTags.XML_TAG:
            BXML bxml = EntityBodyHandler.constructXmlDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, bxml);
            return bxml;
        case TypeTags.BLOB_TAG:
            BlobDataSource blobDataSource = EntityBodyHandler.constructBlobDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, blobDataSource);
            return new BBlob(blobDataSource != null ? blobDataSource.getValue() : new byte[0]);
        case TypeTags.STRUCT_TAG:
            bjson = EntityBodyHandler.constructJsonDataSource(inRequestEntity);
            EntityBodyHandler.addMessageDataSource(inRequestEntity, bjson);
            try {
                return JSONUtils.convertJSONToStruct(bjson, (BStructType) entityBodyType);
            } catch (NullPointerException ex) {
                throw new BallerinaConnectorException("cannot convert payload to struct type: " + entityBodyType.getName());
            }
    }
    return null;
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BString(org.ballerinalang.model.values.BString) BXML(org.ballerinalang.model.values.BXML) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) BJSON(org.ballerinalang.model.values.BJSON) BBlob(org.ballerinalang.model.values.BBlob)

Example 3 with BlobDataSource

use of org.ballerinalang.runtime.message.BlobDataSource in project ballerina by ballerina-lang.

the class SetBlob method execute.

@Override
public void execute(Context context) {
    BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
    byte[] payload = context.getBlobArgument(FIRST_PARAMETER_INDEX);
    EntityBodyHandler.addMessageDataSource(entityStruct, new BlobDataSource(payload));
    context.setReturnValues();
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BStruct(org.ballerinalang.model.values.BStruct)

Example 4 with BlobDataSource

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

Example 5 with BlobDataSource

use of org.ballerinalang.runtime.message.BlobDataSource 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)

Aggregations

BlobDataSource (org.ballerinalang.runtime.message.BlobDataSource)5 BBlob (org.ballerinalang.model.values.BBlob)2 BJSON (org.ballerinalang.model.values.BJSON)2 BStruct (org.ballerinalang.model.values.BStruct)2 BXML (org.ballerinalang.model.values.BXML)2 StringDataSource (org.ballerinalang.runtime.message.StringDataSource)2 FileChannel (java.nio.channels.FileChannel)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 BString (org.ballerinalang.model.values.BString)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