use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class AbstractHTTPAction method serializeDataSource.
private void serializeDataSource(Context context, OutputStream messageOutputStream) throws IOException {
BStruct requestStruct = ((BStruct) context.getRefArgument(1));
BStruct entityStruct = MimeUtil.extractEntity(requestStruct);
if (entityStruct != null) {
MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (messageDataSource != null) {
messageDataSource.serializeData(messageOutputStream);
HttpUtil.closeMessageOutputStream(messageOutputStream);
} else {
// When the entity body is a byte channel
try {
EntityBodyHandler.writeByteChannelToOutputStream(entityStruct, messageOutputStream);
} finally {
HttpUtil.closeMessageOutputStream(messageOutputStream);
}
}
}
}
use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class ConnectionAction method sendOutboundResponseRobust.
private BValue[] sendOutboundResponseRobust(Context context, HTTPCarbonMessage requestMessage, BStruct outboundResponseStruct, HTTPCarbonMessage responseMessage) {
String contentType = HttpUtil.getContentTypeFromTransportMessage(responseMessage);
String boundaryString = null;
if (HeaderUtil.isMultipart(contentType)) {
boundaryString = HttpUtil.addBoundaryIfNotExist(responseMessage, contentType);
}
HttpResponseFuture outboundRespStatusFuture = HttpUtil.sendOutboundResponse(requestMessage, responseMessage);
BStruct entityStruct = MimeUtil.extractEntity(outboundResponseStruct);
if (entityStruct != null) {
if (boundaryString != null) {
serializeMultiparts(responseMessage, boundaryString, outboundRespStatusFuture, entityStruct);
} else {
MessageDataSource outboundMessageSource = EntityBodyHandler.getMessageDataSource(entityStruct);
serializeMsgDataSource(responseMessage, outboundMessageSource, outboundRespStatusFuture, entityStruct);
}
}
return handleResponseStatus(context, outboundRespStatusFuture);
}
use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class PushPromisedResponse method pushResponseRobust.
private BValue[] pushResponseRobust(Context context, HTTPCarbonMessage requestMessage, BStruct outboundResponseStruct, HTTPCarbonMessage responseMessage, Http2PushPromise http2PushPromise) {
BStruct entityStruct = MimeUtil.extractEntity(outboundResponseStruct);
HttpResponseFuture outboundRespStatusFuture = HttpUtil.pushResponse(requestMessage, responseMessage, http2PushPromise);
if (entityStruct != null) {
MessageDataSource outboundMessageSource = EntityBodyHandler.getMessageDataSource(entityStruct);
serializeMsgDataSource(responseMessage, outboundMessageSource, outboundRespStatusFuture, entityStruct);
}
return handleResponseStatus(context, outboundRespStatusFuture);
}
use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class GetXml method execute.
@Override
public void execute(Context context) {
BXML result;
try {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
MessageDataSource dataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (dataSource != null) {
if (dataSource instanceof BXML) {
result = (BXML) dataSource;
} else {
// else, build the XML from the string representation of the payload.
result = XMLUtils.parse(dataSource.getMessageAsString());
}
} else {
result = EntityBodyHandler.constructXmlDataSource(entityStruct);
EntityBodyHandler.addMessageDataSource(entityStruct, result);
// Set byte channel to null, once the message data source has been constructed
entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, null);
}
context.setReturnValues(result);
} catch (Throwable e) {
context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while retrieving xml data from entity : " + e.getMessage()));
}
}
use of org.ballerinalang.runtime.message.MessageDataSource in project ballerina by ballerina-lang.
the class GetJson method execute.
@Override
public void execute(Context context) {
BJSON result;
try {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
MessageDataSource dataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (dataSource != null) {
if (dataSource instanceof BJSON) {
result = (BJSON) dataSource;
} else {
// else, build the JSON from the string representation of the payload.
result = new BJSON(dataSource.getMessageAsString());
}
} else {
result = EntityBodyHandler.constructJsonDataSource(entityStruct);
EntityBodyHandler.addMessageDataSource(entityStruct, result);
// Set byte channel to null, once the message data source has been constructed
entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, null);
}
context.setReturnValues(result);
} catch (Throwable e) {
context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while extracting json data from entity: " + e.getMessage()));
}
}
Aggregations