Search in sources :

Example 21 with FileDataSource

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));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Message(org.apache.camel.Message) Producer(org.apache.camel.Producer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) Test(org.junit.Test)

Example 22 with FileDataSource

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();
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Message(org.apache.camel.Message) Producer(org.apache.camel.Producer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) Test(org.junit.Test)

Example 23 with FileDataSource

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();
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Message(org.apache.camel.Message) Producer(org.apache.camel.Producer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) Test(org.junit.Test)

Example 24 with FileDataSource

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));
}
Also used : Producer(org.apache.camel.Producer) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) Test(org.junit.Test)

Example 25 with FileDataSource

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);
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) URLDataSource(javax.activation.URLDataSource) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) URISyntaxException(java.net.URISyntaxException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource)

Aggregations

FileDataSource (javax.activation.FileDataSource)41 DataHandler (javax.activation.DataHandler)33 Exchange (org.apache.camel.Exchange)18 Message (org.apache.camel.Message)15 Test (org.junit.Test)15 File (java.io.File)12 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)12 MimeBodyPart (javax.mail.internet.MimeBodyPart)11 MimeMultipart (javax.mail.internet.MimeMultipart)7 Producer (org.apache.camel.Producer)7 DataSource (javax.activation.DataSource)6 BodyPart (javax.mail.BodyPart)6 Multipart (javax.mail.Multipart)6 Endpoint (org.apache.camel.Endpoint)6 Processor (org.apache.camel.Processor)6 MessagingException (javax.mail.MessagingException)5 InternetAddress (javax.mail.internet.InternetAddress)5 MimeMessage (javax.mail.internet.MimeMessage)5 List (java.util.List)4 DefaultAttachment (org.apache.camel.impl.DefaultAttachment)4