use of org.apache.camel.Attachment in project camel by apache.
the class MailBinding method addAttachmentsToMultipart.
protected void addAttachmentsToMultipart(MimeMultipart multipart, String partDisposition, AttachmentsContentTransferEncodingResolver encodingResolver, Exchange exchange) throws MessagingException {
LOG.trace("Adding attachments +++ start +++");
int i = 0;
for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) {
String attachmentFilename = entry.getKey();
Attachment attachment = entry.getValue();
if (LOG.isTraceEnabled()) {
LOG.trace("Attachment #{}: Disposition: {}", i, partDisposition);
LOG.trace("Attachment #{}: DataHandler: {}", i, attachment.getDataHandler());
LOG.trace("Attachment #{}: FileName: {}", i, attachmentFilename);
}
if (attachment != null) {
if (shouldAddAttachment(exchange, attachmentFilename, attachment.getDataHandler())) {
// Create another body part
BodyPart messageBodyPart = new MimeBodyPart();
// Set the data handler to the attachment
messageBodyPart.setDataHandler(attachment.getDataHandler());
// Set headers to the attachment
for (String headerName : attachment.getHeaderNames()) {
List<String> values = attachment.getHeaderAsList(headerName);
for (String value : values) {
messageBodyPart.setHeader(headerName, value);
}
}
if (attachmentFilename.toLowerCase().startsWith("cid:")) {
// add a Content-ID header to the attachment
// must use angle brackets according to RFC: http://www.ietf.org/rfc/rfc2392.txt
messageBodyPart.addHeader("Content-ID", "<" + attachmentFilename.substring(4) + ">");
// Set the filename without the cid
messageBodyPart.setFileName(attachmentFilename.substring(4));
} else {
// Set the filename
messageBodyPart.setFileName(attachmentFilename);
}
LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
if (contentTypeResolver != null) {
String contentType = contentTypeResolver.resolveContentType(attachmentFilename);
LOG.trace("Attachment #" + i + ": Using content type resolver: " + contentTypeResolver + " resolved content type as: " + contentType);
if (contentType != null) {
String value = contentType + "; name=" + attachmentFilename;
messageBodyPart.setHeader("Content-Type", value);
LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
}
}
// set Content-Transfer-Encoding using resolver if possible
resolveContentTransferEncoding(encodingResolver, i, messageBodyPart);
// Set Disposition
messageBodyPart.setDisposition(partDisposition);
// Add part to multipart
multipart.addBodyPart(messageBodyPart);
} else {
LOG.trace("shouldAddAttachment: false");
}
} else {
LOG.warn("Cannot add attachment: " + attachmentFilename + " as DataHandler is null");
}
i++;
}
LOG.trace("Adding attachments +++ done +++");
}
use of org.apache.camel.Attachment in project camel by apache.
the class MailAttachmentTest method testSendAndReceiveMailWithAttachments.
@Test
public void testSendAndReceiveMailWithAttachments() throws Exception {
// clear mailbox
Mailbox.clearAll();
// START SNIPPET: e1
// create an exchange with a normal body and attachment to be produced as email
Endpoint endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret");
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("Hello World");
DefaultAttachment att = new DefaultAttachment(new FileDataSource("src/test/data/logo.jpeg"));
att.addHeader("Content-Description", "some sample content");
in.addAttachmentObject("logo.jpeg", att);
// create a producer that can produce the exchange (= send the mail)
Producer producer = endpoint.createProducer();
// start the producer
producer.start();
// and let it go (processes the exchange by sending the email)
producer.process(exchange);
// END SNIPPET: e1
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(2000);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
Exchange out = mock.assertExchangeReceived(0);
mock.assertIsSatisfied();
// plain text
assertEquals("Hello World", out.getIn().getBody(String.class));
// attachment
Map<String, Attachment> attachments = out.getIn().getAttachmentObjects();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
Attachment attachment = out.getIn().getAttachmentObject("logo.jpeg");
DataHandler handler = attachment.getDataHandler();
assertNotNull("The logo should be there", handler);
// content type should match
boolean match1 = "image/jpeg; name=logo.jpeg".equals(handler.getContentType());
boolean match2 = "application/octet-stream; name=logo.jpeg".equals(handler.getContentType());
assertTrue("Should match 1 or 2", match1 || match2);
assertEquals("Handler name should be the file name", "logo.jpeg", handler.getName());
assertEquals("some sample content", attachment.getHeader("content-description"));
producer.stop();
}
use of org.apache.camel.Attachment in project camel by apache.
the class SplitAttachmentsExpression method splitAttachment.
private Message splitAttachment(Message inMessage, String attachmentName, Attachment attachmentHandler) {
final Message copy = inMessage.copy();
Map<String, Attachment> attachments = copy.getAttachmentObjects();
attachments.clear();
attachments.put(attachmentName, attachmentHandler);
copy.setHeader(HEADER_NAME, attachmentName);
return copy;
}
use of org.apache.camel.Attachment in project camel by apache.
the class NestedMimeMessageConsumeTest method testNestedMultipart.
@Test
public void testNestedMultipart() throws Exception {
Mailbox.clearAll();
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedMinimumMessageCount(1);
prepareMailbox("james3");
resultEndpoint.assertIsSatisfied();
Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
String text = exchange.getIn().getBody(String.class);
assertThat(text, containsString("Test with bold face, pictures and attachments"));
assertEquals("text/plain; charset=us-ascii", exchange.getIn().getHeader("Content-Type"));
Set<String> attachmentNames = exchange.getIn().getAttachmentNames();
assertNotNull("attachments got lost", attachmentNames);
assertEquals(2, attachmentNames.size());
for (String s : attachmentNames) {
Attachment att = exchange.getIn().getAttachmentObject(s);
DataHandler dh = att.getDataHandler();
Object content = dh.getContent();
assertNotNull("Content should not be empty", content);
assertThat(dh.getName(), anyOf(equalTo("image001.png"), equalTo("test.txt")));
}
}
use of org.apache.camel.Attachment in project camel by apache.
the class MimeMultipartDataFormatTest method unmarshalRelated.
@Test
public void unmarshalRelated() throws IOException {
in.setBody(new File("src/test/resources/multipart-related.txt"));
Attachment dh = unmarshalAndCheckAttachmentName("950120.aaCB@XIson.com");
assertNotNull(dh);
assertEquals("The fixed length records", dh.getHeader("Content-Description"));
assertEquals("header value1,header value2", dh.getHeader("X-Additional-Header"));
assertEquals(2, dh.getHeaderAsList("X-Additional-Header").size());
}
Aggregations