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