Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttachmentAttacher processor was missing in mail component #70

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.assimbly.mail.component.mail;

import org.apache.axiom.attachments.ByteArrayDataSource;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.attachment.AttachmentMessage;
import org.apache.commons.io.IOUtils;
import org.assimbly.util.helper.MimeTypeHelper;

import javax.activation.DataHandler;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AttachmentAttacher implements Processor {

@Override
public void process(Exchange exchange) throws Exception {
AttachmentMessage in = exchange.getIn(AttachmentMessage.class);

String fileName = in.getHeader(Exchange.FILE_NAME, String.class);
String mimeType = in.getHeader(Exchange.CONTENT_TYPE, String.class);
InputStream is = in.getBody(InputStream.class);

if (mimeType == null)
mimeType = MimeTypeHelper.detectMimeType(is).toString();

if (fileName == null)
in.setHeader(Exchange.FILE_NAME,
fileName = new SimpleDateFormat("'Dovetail'-yyyy-MM-dd-HH-mm-ss-SSS")
.format(new Date()) + MimeTypeHelper.findFileExtension(mimeType));

String emailBody = in.getHeader("EmailBody", String.class);

if (emailBody == null)
emailBody = "";

in.addAttachment(fileName, new DataHandler(new ByteArrayDataSource(IOUtils.toByteArray(is), mimeType)));

in.setHeader(Exchange.CONTENT_TYPE, "text/plain");
in.setBody(emailBody);
}
}