use of org.apache.camel.Attachment in project camel by apache.
the class MimeMultipartDataFormat method marshal.
@Override
public void marshal(Exchange exchange, Object graph, OutputStream stream) throws NoTypeConversionAvailableException, MessagingException, IOException {
if (multipartWithoutAttachment || headersInline || exchange.getIn().hasAttachments()) {
ContentType contentType = getContentType(exchange);
// remove the Content-Type header. This will be wrong afterwards...
exchange.getOut().removeHeader(Exchange.CONTENT_TYPE);
byte[] bodyContent = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, graph);
Session session = Session.getInstance(System.getProperties());
MimeMessage mm = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart(multipartSubType);
BodyPart part = new MimeBodyPart();
writeBodyPart(bodyContent, part, contentType);
mp.addBodyPart(part);
for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) {
String attachmentFilename = entry.getKey();
Attachment attachment = entry.getValue();
part = new MimeBodyPart();
part.setDataHandler(attachment.getDataHandler());
part.setFileName(MimeUtility.encodeText(attachmentFilename, "UTF-8", null));
String ct = attachment.getDataHandler().getContentType();
contentType = new ContentType(ct);
part.setHeader(CONTENT_TYPE, ct);
if (!contentType.match("text/*") && binaryContent) {
part.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
}
// Set headers to the attachment
for (String headerName : attachment.getHeaderNames()) {
List<String> values = attachment.getHeaderAsList(headerName);
for (String value : values) {
part.setHeader(headerName, value);
}
}
mp.addBodyPart(part);
exchange.getOut().removeAttachment(attachmentFilename);
}
mm.setContent(mp);
// a String
if (headersInline && includeHeaders != null) {
for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
if (includeHeaders.matcher(entry.getKey()).matches()) {
String headerStr = ExchangeHelper.convertToType(exchange, String.class, entry.getValue());
if (headerStr != null) {
mm.setHeader(entry.getKey(), headerStr);
}
}
}
}
mm.saveChanges();
Enumeration<?> hl = mm.getAllHeaders();
List<String> headers = new ArrayList<String>();
if (!headersInline) {
while (hl.hasMoreElements()) {
Object ho = hl.nextElement();
if (ho instanceof Header) {
Header h = (Header) ho;
exchange.getOut().setHeader(h.getName(), h.getValue());
headers.add(h.getName());
}
}
mm.saveChanges();
}
mm.writeTo(stream, headers.toArray(new String[0]));
} else {
// keep the original data
InputStream is = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);
IOHelper.copyAndCloseInput(is, stream);
}
}
use of org.apache.camel.Attachment in project camel by apache.
the class SplitAttachmentsExpression method evaluate.
@Override
public Object evaluate(Exchange exchange) {
// must use getAttachments to ensure attachments is initial populated
if (exchange.getIn().getAttachments().isEmpty()) {
return null;
}
try {
List<Message> answer = new ArrayList<Message>();
Message inMessage = exchange.getIn();
for (Map.Entry<String, Attachment> entry : inMessage.getAttachmentObjects().entrySet()) {
Message attachmentMessage;
if (extractAttachments) {
attachmentMessage = extractAttachment(inMessage, entry.getKey());
} else {
attachmentMessage = splitAttachment(inMessage, entry.getKey(), entry.getValue());
}
if (attachmentMessage != null) {
answer.add(attachmentMessage);
}
}
return answer;
} catch (Exception e) {
throw new RuntimeCamelException("Unable to split attachments from MimeMultipart message", e);
}
}
Aggregations