Search in sources :

Example 1 with MultipartDataSource

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());
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 2 with MultipartDataSource

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);
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource)

Example 3 with MultipartDataSource

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);
        }
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) OutputStream(java.io.OutputStream) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) IOException(java.io.IOException) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 4 with MultipartDataSource

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());
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 5 with MultipartDataSource

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());
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Aggregations

MultipartDataSource (org.ballerinalang.mime.util.MultipartDataSource)5 IOException (java.io.IOException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 MimeTypeParseException (javax.activation.MimeTypeParseException)3 BStruct (org.ballerinalang.model.values.BStruct)3 MIMEPart (org.jvnet.mimepull.MIMEPart)3 Test (org.testng.annotations.Test)3 OutputStream (java.io.OutputStream)1 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1