use of org.ballerinalang.mime.util.MultipartDataSource in project ballerina by ballerina-lang.
the class MultipartEncoderTest method testNestedParts.
@Test(description = "Test whether the nested body parts within a multipart entity can be properly encoded")
public void testNestedParts() {
BStruct nestedMultipartEntity = Util.getNestedMultipartEntity(result);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
MultipartDataSource multipartDataSource = new MultipartDataSource(nestedMultipartEntity, multipartDataBoundary);
multipartDataSource.serializeData(outputStream);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" + multipartDataBoundary, inputStream);
Assert.assertEquals(mimeParts.size(), 4);
for (MIMEPart mimePart : mimeParts) {
testNestedPartContent(mimePart);
}
} catch (MimeTypeParseException | IOException e) {
log.error("Error occurred while testing encoded nested parts", e.getMessage());
}
}
use of org.ballerinalang.mime.util.MultipartDataSource in project ballerina by ballerina-lang.
the class AbstractHTTPAction method serializeMultipartDataSource.
/**
* Encode body parts with the given boundary and send it across the wire.
*
* @param boundaryString Boundary string of multipart entity
* @param entityStruct Represent ballerina entity struct
* @param messageOutputStream Output stream to which the payload is written
*/
private void serializeMultipartDataSource(OutputStream messageOutputStream, String boundaryString, BStruct entityStruct) {
MultipartDataSource multipartDataSource = new MultipartDataSource(entityStruct, boundaryString);
multipartDataSource.serializeData(messageOutputStream);
HttpUtil.closeMessageOutputStream(messageOutputStream);
}
use of org.ballerinalang.mime.util.MultipartDataSource in project ballerina by ballerina-lang.
the class ConnectionAction method serializeMultiparts.
/**
* Serlaize multipart entity body. If an array of body parts exist, encode body parts else serialize body content
* if it exist as a byte channel.
*
* @param responseMessage Response message that needs to be sent out.
* @param boundaryString Boundary string that should be used in encoding body parts
* @param outboundRespStatusFuture Represent the future events and results of connectors
* @param entityStruct Represent the entity that holds the actual body
*/
private void serializeMultiparts(HTTPCarbonMessage responseMessage, String boundaryString, HttpResponseFuture outboundRespStatusFuture, BStruct entityStruct) {
BRefValueArray bodyParts = EntityBodyHandler.getBodyPartArray(entityStruct);
if (bodyParts != null && bodyParts.size() > 0) {
MultipartDataSource multipartDataSource = new MultipartDataSource(entityStruct, boundaryString);
serializeMsgDataSource(responseMessage, multipartDataSource, outboundRespStatusFuture, entityStruct);
} else {
OutputStream messageOutputStream = getOutputStream(responseMessage, outboundRespStatusFuture);
try {
EntityBodyHandler.writeByteChannelToOutputStream(entityStruct, messageOutputStream);
} catch (IOException e) {
throw new BallerinaException("Error occurred while serializing byte channel content : " + e.getMessage());
} finally {
HttpUtil.closeMessageOutputStream(messageOutputStream);
}
}
}
use of org.ballerinalang.mime.util.MultipartDataSource in project ballerina by ballerina-lang.
the class MultipartEncoderTest method testMultipartWriterForNewSubTypes.
@Test(description = "Test whether the body parts get correctly encoded for any new multipart sub type")
public void testMultipartWriterForNewSubTypes() {
BStruct multipartEntity = Util.getMultipartEntity(result);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
MultipartDataSource multipartDataSource = new MultipartDataSource(multipartEntity, multipartDataBoundary);
multipartDataSource.serializeData(outputStream);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/new-sub-type; boundary=" + multipartDataBoundary, inputStream);
Assert.assertEquals(mimeParts.size(), 4);
BStruct bodyPart = Util.getEntityStruct(result);
validateBodyPartContent(mimeParts, bodyPart);
} catch (MimeTypeParseException e) {
log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
} catch (IOException e) {
log.error("Error occurred while decoding binary part", e.getMessage());
}
}
use of org.ballerinalang.mime.util.MultipartDataSource in project ballerina by ballerina-lang.
the class MultipartEncoderTest method testMultipartWriterForMixed.
@Test(description = "Test whether the body parts get correctly encoded for multipart/mixed")
public void testMultipartWriterForMixed() {
BStruct multipartEntity = Util.getMultipartEntity(result);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
MultipartDataSource multipartDataSource = new MultipartDataSource(multipartEntity, multipartDataBoundary);
multipartDataSource.serializeData(outputStream);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" + multipartDataBoundary, inputStream);
Assert.assertEquals(mimeParts.size(), 4);
BStruct bodyPart = Util.getEntityStruct(result);
validateBodyPartContent(mimeParts, bodyPart);
} catch (MimeTypeParseException e) {
log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
} catch (IOException e) {
log.error("Error occurred while decoding binary part", e.getMessage());
}
}
Aggregations