Search in sources :

Example 21 with Exchange

use of org.apache.camel.Exchange 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 22 with Exchange

use of org.apache.camel.Exchange 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 23 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class MailConvertersTest method testMultipartToString.

@Test
public void testMultipartToString() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.send("direct:a", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World");
            exchange.getIn().setHeader(MailConstants.MAIL_ALTERNATIVE_BODY, "Alternative World");
        }
    });
    assertMockEndpointsSatisfied();
    Message mailMessage = mock.getReceivedExchanges().get(0).getIn().getBody(MailMessage.class).getMessage();
    assertNotNull(mailMessage);
    Object content = mailMessage.getContent();
    Multipart mp = assertIsInstanceOf(Multipart.class, content);
    String s = MailConverters.toString(mp);
    assertEquals("Alternative World", s);
}
Also used : Exchange(org.apache.camel.Exchange) Multipart(javax.mail.Multipart) Processor(org.apache.camel.Processor) Message(javax.mail.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 24 with Exchange

use of org.apache.camel.Exchange 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")));
    }
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Attachment(org.apache.camel.Attachment) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) Test(org.junit.Test)

Example 25 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class RawMailMessageTest method testGetRawJavaMailMessage.

@Test
public void testGetRawJavaMailMessage() throws Exception {
    Mailbox.clearAll();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("To", "davsclaus@apache.org");
    map.put("From", "jstrachan@apache.org");
    map.put("Subject", "Camel rocks");
    String body = "Hello Claus.\nYes it does.\n\nRegards James.";
    getMockEndpoint("mock:mail").expectedMessageCount(1);
    template.sendBodyAndHeaders("smtp://davsclaus@apache.org", body, map);
    assertMockEndpointsSatisfied();
    Exchange exchange = getMockEndpoint("mock:mail").getReceivedExchanges().get(0);
    // START SNIPPET: e1
    // get access to the raw javax.mail.Message as shown below
    Message javaMailMessage = exchange.getIn(MailMessage.class).getMessage();
    assertNotNull(javaMailMessage);
    assertEquals("Camel rocks", javaMailMessage.getSubject());
// END SNIPPET: e1
}
Also used : Exchange(org.apache.camel.Exchange) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Exchange (org.apache.camel.Exchange)3446 Test (org.junit.Test)1735 Processor (org.apache.camel.Processor)1405 RouteBuilder (org.apache.camel.builder.RouteBuilder)666 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)640 DefaultExchange (org.apache.camel.impl.DefaultExchange)473 Message (org.apache.camel.Message)379 Endpoint (org.apache.camel.Endpoint)235 HashMap (java.util.HashMap)190 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)155 Producer (org.apache.camel.Producer)150 File (java.io.File)120 ArrayList (java.util.ArrayList)117 CamelContext (org.apache.camel.CamelContext)117 List (java.util.List)99 Map (java.util.Map)96 ProducerTemplate (org.apache.camel.ProducerTemplate)94 IOException (java.io.IOException)92 Tx (org.nhindirect.common.tx.model.Tx)83 Predicate (org.apache.camel.Predicate)78