Search in sources :

Example 6 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class BeanWithAttachmentAnnotationTest method testBeanWithOldAnnotationAndExchangeTest.

public void testBeanWithOldAnnotationAndExchangeTest() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("attachment");
    template.send("direct:inOld", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            Message m = exchange.getIn();
            m.addAttachmentObject("attachment", new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java")));
        }
    });
    mock.assertIsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DefaultAttachment(org.apache.camel.impl.DefaultAttachment)

Example 7 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class MailAttachmentDuplicateNamesTest method testSendAndRecieveMailWithAttachmentsWithDuplicateNames.

@Test
public void testSendAndRecieveMailWithAttachmentsWithDuplicateNames() 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");
    in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
    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);
    // 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, 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);
    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 8 with FileDataSource

use of javax.activation.FileDataSource 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();
}
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) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) DataHandler(javax.activation.DataHandler) Test(org.junit.Test)

Example 9 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class MailSplitAttachmentsTest method setup.

@Before
public void setup() {
    // create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
    endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret");
    exchange = endpoint.createExchange();
    Message in = exchange.getIn();
    in.setBody("Hello World");
    in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
    in.addAttachment("license.txt", new DataHandler(new FileDataSource("src/main/resources/META-INF/LICENSE.txt")));
}
Also used : Message(org.apache.camel.Message) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) Before(org.junit.Before)

Example 10 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class MailHtmlAttachmentTest method testSendAndRecieveMailWithAttachments.

@Test
public void testSendAndRecieveMailWithAttachments() 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&contentType=text/html");
    // 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("<html><body><h1>Hello</h1>World</body></html>");
    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);
    // 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("<html><body><h1>Hello</h1>World</body></html>", 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);
    byte[] bytes = context.getTypeConverter().convertTo(byte[].class, handler.getInputStream());
    assertNotNull("content should be there", bytes);
    assertTrue("logo should be more than 1000 bytes", bytes.length > 1000);
    // 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);
    // save logo for visual inspection
    template.sendBodyAndHeader("file://target", bytes, Exchange.FILE_NAME, "maillogo.jpg");
    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)

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