use of javax.activation.FileDataSource in project camel by apache.
the class MailAttachmentRedeliveryTest method testSendAndReceiveMailWithAttachmentsRedelivery.
@Test
public void testSendAndReceiveMailWithAttachmentsRedelivery() throws Exception {
// clear mailbox
Mailbox.clearAll();
// 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");
in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
// 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);
// 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, DataHandler> attachments = out.getIn().getAttachments();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
DataHandler handler = out.getIn().getAttachment("logo.jpeg");
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());
producer.stop();
assertEquals(3, names.size());
assertEquals("logo.jpeg", names.get(0));
assertEquals("logo.jpeg", names.get(1));
assertEquals("logo.jpeg", names.get(2));
}
use of javax.activation.FileDataSource in project camel by apache.
the class MailAttachmentsUmlautIssueTest method testSendAndReceiveMailWithAttachments.
@Test
public void testSendAndReceiveMailWithAttachments() throws Exception {
// clear mailbox
Mailbox.clearAll();
// 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");
// unicode 00DC is german umlaut
String name = "logo2Ü";
// use existing logo.jpeg file, but lets name it with the umlaut
in.addAttachment(name, new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
// 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);
// 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, DataHandler> attachments = out.getIn().getAttachments();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
DataHandler handler = out.getIn().getAttachment(name);
assertNotNull("The " + name + " should be there", handler);
String nameURLEncoded = URLEncoder.encode(name, Charset.defaultCharset().name());
assertTrue("Handler content type should end with URL-encoded name", handler.getContentType().endsWith(nameURLEncoded));
assertEquals("Handler name should be the file name", name, handler.getName());
producer.stop();
}
use of javax.activation.FileDataSource in project camel by apache.
the class MailContentTypeResolverTest method testCustomContentTypeResolver.
@Test
public void testCustomContentTypeResolver() throws Exception {
// clear mailbox
Mailbox.clearAll();
// 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");
in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
// 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);
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(4000);
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, DataHandler> attachments = out.getIn().getAttachments();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
DataHandler handler = out.getIn().getAttachment("logo.jpeg");
assertNotNull("The logo should be there", handler);
// as we use a custom content type resolver the content type should then be fixed and correct
// 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);
producer.stop();
}
use of javax.activation.FileDataSource in project camel by apache.
the class MailSplitAttachmentsTest method testExtractAttachments.
@Test
public void testExtractAttachments() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:split");
mock.expectedMessageCount(2);
// set the expression to extract the attachments as byte[]s
splitAttachmentsExpression.setExtractAttachments(true);
Producer producer = endpoint.createProducer();
producer.start();
producer.process(exchange);
Thread.sleep(2000);
mock.assertIsSatisfied();
Message first = mock.getReceivedExchanges().get(0).getIn();
Message second = mock.getReceivedExchanges().get(1).getIn();
// check it's no longer an attachment, but is the message body
assertEquals(0, first.getAttachments().size());
assertEquals(0, second.getAttachments().size());
assertEquals("logo.jpeg", first.getHeader("CamelSplitAttachmentId"));
assertEquals("license.txt", second.getHeader("CamelSplitAttachmentId"));
byte[] expected1 = IOUtils.toByteArray(new FileDataSource("src/test/data/logo.jpeg").getInputStream());
byte[] expected2 = IOUtils.toByteArray(new FileDataSource("src/main/resources/META-INF/LICENSE.txt").getInputStream());
assertArrayEquals(expected1, first.getBody(byte[].class));
assertArrayEquals(expected2, second.getBody(byte[].class));
}
use of javax.activation.FileDataSource in project camel by apache.
the class MimeMessageConsumeTest method populateMimeMessageBody.
/**
* Lets encode a multipart mime message
*/
protected void populateMimeMessageBody(MimeMessage message) throws MessagingException {
MimeBodyPart plainPart = new MimeBodyPart();
plainPart.setText(body);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText("<html><body>" + body + "</body></html>");
Multipart alt = new MimeMultipart("alternative");
alt.addBodyPart(plainPart);
alt.addBodyPart(htmlPart);
Multipart mixed = new MimeMultipart("mixed");
MimeBodyPart wrap = new MimeBodyPart();
wrap.setContent(alt);
mixed.addBodyPart(wrap);
mixed.addBodyPart(plainPart);
mixed.addBodyPart(htmlPart);
DataSource ds;
try {
File f = new File(getClass().getResource("/log4j2.properties").toURI());
ds = new FileDataSource(f);
} catch (URISyntaxException ex) {
ds = new URLDataSource(getClass().getResource("/log4j2.properties"));
}
DataHandler dh = new DataHandler(ds);
BodyPart attachmentBodyPart;
// Create another body part
attachmentBodyPart = new MimeBodyPart();
// Set the data handler to the attachment
attachmentBodyPart.setDataHandler(dh);
// Set the filename
attachmentBodyPart.setFileName(dh.getName());
// Set Disposition
attachmentBodyPart.setDisposition(Part.ATTACHMENT);
mixed.addBodyPart(plainPart);
mixed.addBodyPart(htmlPart);
// Add attachmentBodyPart to multipart
mixed.addBodyPart(attachmentBodyPart);
message.setContent(mixed);
}
Aggregations