Search in sources :

Example 1 with Message

use of org.apache.camel.Message 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 2 with Message

use of org.apache.camel.Message 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 3 with Message

use of org.apache.camel.Message 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;
}
Also used : Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) Attachment(org.apache.camel.Attachment)

Example 4 with Message

use of org.apache.camel.Message 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 5 with Message

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

the class MailSplitAttachmentsTest method testSplitAttachments.

@Test
public void testSplitAttachments() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:split");
    mock.expectedMessageCount(2);
    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();
    assertEquals(1, first.getAttachments().size());
    assertEquals(1, second.getAttachments().size());
    String file1 = first.getAttachments().keySet().iterator().next();
    String file2 = second.getAttachments().keySet().iterator().next();
    boolean logo = file1.equals("logo.jpeg") || file2.equals("logo.jpeg");
    boolean license = file1.equals("license.txt") || file2.equals("license.txt");
    assertTrue("Should have logo.jpeg file attachment", logo);
    assertTrue("Should have license.txt file attachment", license);
}
Also used : Producer(org.apache.camel.Producer) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

Message (org.apache.camel.Message)810 Exchange (org.apache.camel.Exchange)379 Test (org.junit.Test)262 Processor (org.apache.camel.Processor)120 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)109 DefaultMessage (org.apache.camel.impl.DefaultMessage)67 DefaultExchange (org.apache.camel.impl.DefaultExchange)50 Endpoint (org.apache.camel.Endpoint)46 HashMap (java.util.HashMap)41 InputStream (java.io.InputStream)40 Response (javax.ws.rs.core.Response)38 ArrayList (java.util.ArrayList)33 RouteBuilder (org.apache.camel.builder.RouteBuilder)33 Map (java.util.Map)27 Producer (org.apache.camel.Producer)27 Customer (org.apache.camel.component.cxf.jaxrs.testbean.Customer)25 ActionResponse (org.openstack4j.model.common.ActionResponse)25 IOException (java.io.IOException)24 List (java.util.List)23 CamelContext (org.apache.camel.CamelContext)23