MIMEMessage mm = new MIMEMessage(stream,
mediaType.getParameters().get("boundary"),
mimeConfig);
boolean formData = false;
MultiPart multiPart = null;
if (MediaTypes.typeEquals(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) {
multiPart = new FormDataMultiPart();
formData = true;
} else {
multiPart = new MultiPart();
}
multiPart.setProviders(providers);
MultivaluedMap<String,String> mpHeaders = multiPart.getHeaders();
for (Map.Entry<String,List<String>> entry : headers.entrySet()) {
List<String> values = entry.getValue();
for (String value : values) {
mpHeaders.add(entry.getKey(), value);
}
}
if (!formData) {
multiPart.setMediaType(mediaType);
}
for (MIMEPart mp : mm.getAttachments()) {
BodyPart bodyPart = null;
if (formData) {
bodyPart = new FormDataBodyPart();
} else {
bodyPart = new BodyPart();
}
// Configure providers
bodyPart.setProviders(providers);
// Copy headers
for (Header h : mp.getAllHeaders()) {
bodyPart.getHeaders().add(h.getName(), h.getValue());
}
try {
String contentType = bodyPart.getHeaders().getFirst("Content-Type");
if (contentType != null)
bodyPart.setMediaType(MediaType.valueOf(contentType));
bodyPart.getContentDisposition();
} catch (IllegalArgumentException ex) {
throw new WebApplicationException(ex, Status.BAD_REQUEST);
}
// Copy data into a BodyPartEntity structure
bodyPart.setEntity(new BodyPartEntity(mp));
// Add this BodyPart to our MultiPart
multiPart.getBodyParts().add(bodyPart);
}
return multiPart;
}