use of org.jvnet.mimepull.MIMEMessage in project ballerina by ballerina-lang.
the class MultipartDecoder method decodeBodyParts.
/**
* Decode multiparts from a given input stream.
*
* @param contentType Content-Type of the top level message
* @param inputStream Represent input stream coming from the request/response
* @return A list of mime parts
* @throws MimeTypeParseException When an inputstream cannot be decoded properly
*/
public static List<MIMEPart> decodeBodyParts(String contentType, InputStream inputStream) throws MimeTypeParseException {
MimeType mimeType = new MimeType(contentType);
final MIMEMessage mimeMessage = new MIMEMessage(inputStream, mimeType.getParameter(BOUNDARY), getMimeConfig());
return mimeMessage.getAttachments();
}
use of org.jvnet.mimepull.MIMEMessage 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);
}
use of org.jvnet.mimepull.MIMEMessage in project jersey by jersey.
the class MultiPartReaderClientSide method readMultiPart.
protected MultiPart readMultiPart(final Class<MultiPart> type, final Type genericType, final Annotation[] annotations, MediaType mediaType, final MultivaluedMap<String, String> headers, final InputStream stream) throws IOException, MIMEParsingException {
mediaType = unquoteMediaTypeParameters(mediaType, "boundary");
final MIMEMessage mimeMessage = new MIMEMessage(stream, mediaType.getParameters().get("boundary"), mimeConfig);
final boolean formData = MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE);
final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart();
final MessageBodyWorkers workers = messageBodyWorkers.get();
multiPart.setMessageBodyWorkers(workers);
final MultivaluedMap<String, String> multiPartHeaders = multiPart.getHeaders();
for (final Map.Entry<String, List<String>> entry : headers.entrySet()) {
final List<String> values = entry.getValue();
for (final String value : values) {
multiPartHeaders.add(entry.getKey(), value);
}
}
final boolean fileNameFix;
if (!formData) {
multiPart.setMediaType(mediaType);
fileNameFix = false;
} else {
// see if the User-Agent header corresponds to some version of MS Internet Explorer
// if so, need to set fileNameFix to true to handle issue http://java.net/jira/browse/JERSEY-759
final String userAgent = headers.getFirst(HttpHeaders.USER_AGENT);
fileNameFix = userAgent != null && userAgent.contains(" MSIE ");
}
for (final MIMEPart mimePart : getMimeParts(mimeMessage)) {
final BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart();
// Configure providers.
bodyPart.setMessageBodyWorkers(workers);
// Copy headers.
for (final Header header : mimePart.getAllHeaders()) {
bodyPart.getHeaders().add(header.getName(), header.getValue());
}
try {
final String contentType = bodyPart.getHeaders().getFirst("Content-Type");
if (contentType != null) {
bodyPart.setMediaType(MediaType.valueOf(contentType));
}
bodyPart.getContentDisposition();
} catch (final IllegalArgumentException ex) {
throw new BadRequestException(ex);
}
// Copy data into a BodyPartEntity structure.
bodyPart.setEntity(new BodyPartEntity(mimePart));
// Add this BodyPart to our MultiPart.
multiPart.getBodyParts().add(bodyPart);
}
return multiPart;
}
Aggregations