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