Search in sources :

Example 1 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project ballerina by ballerina-lang.

the class MultipartEncoderTest method testNestedPartsInOutResponse.

@Test(description = "Retrieve body parts from the Request and send it across Response")
public void testNestedPartsInOutResponse() {
    String path = "/multipart/nested_parts_in_outresponse";
    HTTPTestRequest inRequestMsg = Util.createNestedPartRequest(path);
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
    Assert.assertNotNull(response, "Response message not found");
    InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
    try {
        List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts(inRequestMsg.getHeader(HttpHeaderNames.CONTENT_TYPE.toString()), inputStream);
        Assert.assertEquals(mimeParts.size(), 2);
        List<MIMEPart> childParts = MultipartDecoder.decodeBodyParts(mimeParts.get(1).getContentType(), mimeParts.get(1).readOnce());
        Assert.assertEquals(childParts.size(), 2);
    } catch (MimeTypeParseException e) {
        log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 2 with MIMEPart

use of org.jvnet.mimepull.MIMEPart 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 3 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project ballerina by ballerina-lang.

the class MultipartEncoderTest method testMultipartsInOutResponse.

@Test(description = "Test whether the encoded body parts can be sent through Response, with a given boundary")
public void testMultipartsInOutResponse() {
    String path = "/multipart/encode_out_response";
    HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_GET);
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
    Assert.assertNotNull(response, "Response message not found");
    InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
    try {
        List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" + "e3a0b9ad7b4e7cdb", 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 : BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 4 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project ballerina by ballerina-lang.

the class MultipartDecoder method populateBallerinaParts.

/**
 * Populate ballerina body parts from the given mime parts and set it to top level entity.
 *
 * @param context   Represent ballerina context
 * @param entity    Represent top level entity that the body parts needs to be attached to
 * @param mimeParts List of decoded mime parts
 */
private static void populateBallerinaParts(Context context, BStruct entity, List<MIMEPart> mimeParts) {
    ArrayList<BStruct> bodyParts = new ArrayList<>();
    for (final MIMEPart mimePart : mimeParts) {
        BStruct partStruct = ConnectorUtils.createAndGetStruct(context, PROTOCOL_PACKAGE_MIME, ENTITY);
        BStruct mediaType = ConnectorUtils.createAndGetStruct(context, PROTOCOL_PACKAGE_MIME, MEDIA_TYPE);
        populateBodyPart(context, mimePart, partStruct, mediaType);
        bodyParts.add(partStruct);
    }
    EntityBodyHandler.setPartsToTopLevelEntity(entity, bodyParts);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ArrayList(java.util.ArrayList) MIMEPart(org.jvnet.mimepull.MIMEPart)

Example 5 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project Payara by payara.

the class MultipartProprietaryReader method readFrom.

@Override
public ParamsWithPayload readFrom(final InputStream is, final String contentType) throws IOException {
    RestPayloadImpl.Inbound payload = null;
    ActionReport actionReport = null;
    ParameterMap parameters = null;
    Properties mtProps = parseHeaderParams(contentType);
    final String boundary = mtProps.getProperty("boundary");
    if (!StringUtils.ok(boundary)) {
        throw new IOException("ContentType does not define boundary");
    }
    final MIMEMessage mimeMessage = new MIMEMessage(is, boundary, new MIMEConfig());
    // Parse
    for (MIMEPart mimePart : mimeMessage.getAttachments()) {
        String cd = getFirst(mimePart.getHeader("Content-Disposition"));
        if (!StringUtils.ok(cd)) {
            cd = "file";
        }
        cd = cd.trim();
        Properties cdParams = parseHeaderParams(cd);
        // 3 types of content disposition
        if (cd.startsWith("form-data")) {
            // COMMAND PARAMETER
            if (!StringUtils.ok(cdParams.getProperty("name"))) {
                throw new IOException("Form-data Content-Disposition does not contains name parameter.");
            }
            if (parameters == null) {
                parameters = new ParameterMap();
            }
            parameters.add(cdParams.getProperty("name"), stream2String(mimePart.readOnce()));
        } else if (mimePart.getContentType() != null && mimePart.getContentType().startsWith("application/json")) {
            // ACTION REPORT
            actionReport = actionReportReader.readFrom(mimePart.readOnce(), "application/json");
        } else {
            // PAYLOAD
            String name = "noname";
            if (cdParams.containsKey("name")) {
                name = cdParams.getProperty("name");
            } else if (cdParams.containsKey("filename")) {
                name = cdParams.getProperty("filename");
            }
            if (payload == null) {
                payload = new RestPayloadImpl.Inbound();
            }
            String ct = mimePart.getContentType();
            if (!StringUtils.ok(ct) || ct.trim().startsWith("text/plain")) {
                payload.add(name, stream2String(mimePart.readOnce()), mimePart.getAllHeaders());
            } else {
                payload.add(name, mimePart.read(), ct, mimePart.getAllHeaders());
            }
        }
    }
    // Result
    return new ParamsWithPayload(payload, parameters, actionReport);
}
Also used : MIMEConfig(org.jvnet.mimepull.MIMEConfig) MIMEMessage(org.jvnet.mimepull.MIMEMessage) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) ParamsWithPayload(com.sun.enterprise.admin.remote.ParamsWithPayload) ActionReport(org.glassfish.api.ActionReport) Properties(java.util.Properties) RestPayloadImpl(com.sun.enterprise.admin.remote.RestPayloadImpl) MIMEPart(org.jvnet.mimepull.MIMEPart)

Aggregations

MIMEPart (org.jvnet.mimepull.MIMEPart)9 BStruct (org.ballerinalang.model.values.BStruct)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 MimeTypeParseException (javax.activation.MimeTypeParseException)5 Test (org.testng.annotations.Test)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 MultipartDataSource (org.ballerinalang.mime.util.MultipartDataSource)3 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)2 MIMEMessage (org.jvnet.mimepull.MIMEMessage)2 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)2 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)2 ParamsWithPayload (com.sun.enterprise.admin.remote.ParamsWithPayload)1 RestPayloadImpl (com.sun.enterprise.admin.remote.RestPayloadImpl)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1