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");
}
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;
}
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();
}
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);
}
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]));
}
Aggregations