Search in sources :

Example 1 with BallerinaIOException

use of org.ballerinalang.nativeimpl.io.BallerinaIOException in project ballerina by ballerina-lang.

the class EntityBodyHandler method constructJsonDataSource.

/**
 * Construct JsonDataSource from the underneath byte channel which is associated with the entity struct.
 *
 * @param entityStruct Represent an entity struct
 * @return BJSON data source which is kept in memory
 */
public static BJSON constructJsonDataSource(BStruct entityStruct) {
    try {
        Channel byteChannel = getByteChannel(entityStruct);
        if (byteChannel == null) {
            return null;
        }
        BJSON jsonData = new BJSON(byteChannel.getInputStream());
        byteChannel.close();
        return jsonData;
    } catch (IOException e) {
        throw new BallerinaIOException("Error occurred while closing connection", 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) BJSON(org.ballerinalang.model.values.BJSON) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException)

Example 2 with BallerinaIOException

use of org.ballerinalang.nativeimpl.io.BallerinaIOException in project ballerina by ballerina-lang.

the class EntityBodyHandler method constructXmlDataSource.

/**
 * Construct XMl data source from the underneath byte channel which is associated with the entity struct.
 *
 * @param entityStruct Represent an entity struct
 * @return BXML data source which is kept in memory
 */
public static BXML constructXmlDataSource(BStruct entityStruct) {
    try {
        Channel byteChannel = getByteChannel(entityStruct);
        if (byteChannel == null) {
            throw new BallerinaIOException("Empty xml payload");
        }
        BXML xmlContent = XMLUtils.parse(byteChannel.getInputStream());
        byteChannel.close();
        return xmlContent;
    } 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) BXML(org.ballerinalang.model.values.BXML) IOException(java.io.IOException) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException) BallerinaIOException(org.ballerinalang.nativeimpl.io.BallerinaIOException)

Example 3 with BallerinaIOException

use of org.ballerinalang.nativeimpl.io.BallerinaIOException 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)

Aggregations

IOException (java.io.IOException)3 FileChannel (java.nio.channels.FileChannel)3 BallerinaIOException (org.ballerinalang.nativeimpl.io.BallerinaIOException)3 TempFileIOChannel (org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel)3 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)3 BJSON (org.ballerinalang.model.values.BJSON)1 BXML (org.ballerinalang.model.values.BXML)1 StringDataSource (org.ballerinalang.runtime.message.StringDataSource)1