Search in sources :

Example 21 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel 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 22 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class GetByteChannel method execute.

@Override
public void execute(Context context) {
    BStruct byteChannelStruct;
    try {
        BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
        byteChannelStruct = ConnectorUtils.createAndGetStruct(context, PROTOCOL_PACKAGE_IO, BYTE_CHANNEL_STRUCT);
        Channel byteChannel = EntityBodyHandler.getByteChannel(entityStruct);
        if (byteChannel != null) {
            byteChannelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, byteChannel);
            context.setReturnValues(byteChannelStruct);
        } else {
            if (EntityBodyHandler.getMessageDataSource(entityStruct) != null) {
                context.setReturnValues(MimeUtil.createEntityError(context, "Byte channel is not available but payload can be obtain either as xml, " + "json, string or blob type"));
            } else if (EntityBodyHandler.getBodyPartArray(entityStruct) != null && EntityBodyHandler.getBodyPartArray(entityStruct).size() != 0) {
                context.setReturnValues(MimeUtil.createEntityError(context, "Byte channel is not available since payload contains a set of body parts"));
            } else {
                context.setReturnValues(MimeUtil.createEntityError(context, "Byte channel is not available as payload"));
            }
        }
    } catch (Throwable e) {
        context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while constructing byte channel from entity body : " + e.getMessage()));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel)

Example 23 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class MimeUtilityFunctionTest method testTempFileDeletion.

@Test(description = "Once the temp file channel is closed, check whether the temp file gets deleted")
public void testTempFileDeletion() throws IOException {
    File file;
    try {
        file = File.createTempFile("testFile", ".tmp");
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write("File Content");
        bufferedWriter.close();
        Channel tempFileIOChannel = EntityBodyHandler.getByteChannelForTempFile(file.getAbsolutePath());
        Assert.assertTrue(file.exists());
        InputStream inputStream = tempFileIOChannel.getInputStream();
        Assert.assertNotNull(inputStream);
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        MimeUtil.writeInputToOutputStream(inputStream, result);
        Assert.assertEquals(result.toString("UTF-8"), "File Content");
        tempFileIOChannel.close();
        Assert.assertFalse(file.exists());
    } catch (IOException e) {
        log.error("Error occurred in testTempFileDeletion", e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Test(org.testng.annotations.Test)

Example 24 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class Util method encodeBodyPart.

/**
 * Encode a given body part and add it to multipart request encoder.
 *
 * @param nettyEncoder Helps encode multipart/form-data
 * @param httpRequest  Represent top level http request that should hold multiparts
 * @param bodyPart     Represent a ballerina body part
 * @throws HttpPostRequestEncoder.ErrorDataEncoderException when an error occurs while encoding
 */
private static void encodeBodyPart(HttpPostRequestEncoder nettyEncoder, HttpRequest httpRequest, BStruct bodyPart) throws HttpPostRequestEncoder.ErrorDataEncoderException {
    try {
        InterfaceHttpData encodedData;
        Channel byteChannel = EntityBodyHandler.getByteChannel(bodyPart);
        FileUploadContentHolder contentHolder = new FileUploadContentHolder();
        contentHolder.setRequest(httpRequest);
        contentHolder.setBodyPartName(getBodyPartName(bodyPart));
        contentHolder.setFileName(TEMP_FILE_NAME + TEMP_FILE_EXTENSION);
        contentHolder.setContentType(MimeUtil.getBaseType(bodyPart));
        contentHolder.setBodyPartFormat(org.ballerinalang.mime.util.Constants.BodyPartForm.INPUTSTREAM);
        String contentTransferHeaderValue = HeaderUtil.getHeaderValue(bodyPart, HttpHeaderNames.CONTENT_TRANSFER_ENCODING.toString());
        if (contentTransferHeaderValue != null) {
            contentHolder.setContentTransferEncoding(contentTransferHeaderValue);
        }
        if (byteChannel != null) {
            contentHolder.setContentStream(byteChannel.getInputStream());
            encodedData = getFileUpload(contentHolder);
            if (encodedData != null) {
                nettyEncoder.addBodyHttpData(encodedData);
            }
        }
    } catch (IOException e) {
        log.error("Error occurred while encoding body part in ", e.getMessage());
    }
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) EntityBodyChannel(org.ballerinalang.mime.util.EntityBodyChannel) BString(org.ballerinalang.model.values.BString) IOException(java.io.IOException)

Example 25 with Channel

use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method readBytes.

@Test(description = "Read into fixed byte[] using async io framework")
public void readBytes() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    byte[] content = new byte[2];
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] expected = { 49, 50 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = new byte[] { 51, 52 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = new byte[] { 53, 54 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    int expectedNumberOfBytes = 0;
    content = new byte[2];
    expected = new byte[] { 0, 0 };
    int numberOfBytesRead = IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(numberOfBytesRead, expectedNumberOfBytes);
    Assert.assertEquals(expected, content);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Aggregations

Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)49 ByteChannel (java.nio.channels.ByteChannel)33 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)31 Test (org.testng.annotations.Test)31 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)19 BStruct (org.ballerinalang.model.values.BStruct)12 IOException (java.io.IOException)7 FileChannel (java.nio.channels.FileChannel)7 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)7 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)6 TempFileIOChannel (org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel)5 InputStream (java.io.InputStream)4 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 BallerinaIOException (org.ballerinalang.nativeimpl.io.BallerinaIOException)3 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 InputStreamReader (java.io.InputStreamReader)2