Search in sources :

Example 1 with MessageDataSource

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

Example 2 with MessageDataSource

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

Example 3 with MessageDataSource

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

Example 4 with MessageDataSource

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

Example 5 with MessageDataSource

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

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