use of org.apache.camel.Exchange in project camel by apache.
the class MailProducerTest method testProducerBodyIsMimeMessage.
@Test
public void testProducerBodyIsMimeMessage() throws Exception {
Mailbox.clearAll();
getMockEndpoint("mock:result").expectedMessageCount(1);
Address from = new InternetAddress("fromCamelTest@localhost");
Address to = new InternetAddress("recipient2@localhost");
Session session = Session.getDefaultInstance(System.getProperties());
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(from);
mimeMessage.addRecipient(RecipientType.TO, to);
mimeMessage.setSubject("This is the subject.");
mimeMessage.setText("This is the message");
template.sendBodyAndHeader("direct:start", mimeMessage, "To", "someone@localhost");
assertMockEndpointsSatisfied();
// need to check the message header
Exchange exchange = getMockEndpoint("mock:result").getExchanges().get(0);
assertNotNull("The message id should not be null", exchange.getIn().getHeader(MailConstants.MAIL_MESSAGE_ID));
Mailbox box = Mailbox.get("someone@localhost");
assertEquals(0, box.size());
// Check if the mimeMessagea has override body and headers
Mailbox box2 = Mailbox.get("recipient2@localhost");
assertEquals(1, box2.size());
}
use of org.apache.camel.Exchange in project camel by apache.
the class MailRouteTest method testMailSubjectWithUnicode.
@Test
public void testMailSubjectWithUnicode() throws Exception {
Mailbox.clearAll();
final String body = "Hello Camel Riders!";
final String subject = "My Camel ™";
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
// now we don't use the UTF-8 encoding
mock.expectedHeaderReceived("subject", "=?US-ASCII?Q?My_Camel_=3F?=");
mock.expectedBodiesReceived(body);
template.send("direct:a", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(body);
exchange.getIn().setHeader("subject", subject);
exchange.setProperty(Exchange.CHARSET_NAME, "US-ASCII");
}
});
mock.assertIsSatisfied();
assertFalse("Should not have attachements", mock.getExchanges().get(0).getIn().hasAttachments());
}
use of org.apache.camel.Exchange in project camel by apache.
the class MimeMessageConsumeTest method testSendAndReceiveMails.
@Test
public void testSendAndReceiveMails() throws Exception {
Mailbox.clearAll();
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedMinimumMessageCount(1);
Properties properties = new Properties();
properties.put("mail.smtp.host", "localhost");
Session session = Session.getInstance(properties, null);
MimeMessage message = new MimeMessage(session);
populateMimeMessageBody(message);
message.setRecipients(Message.RecipientType.TO, "james3@localhost");
Transport.send(message);
// lets test the receive worked
resultEndpoint.assertIsSatisfied();
Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
String text = exchange.getIn().getBody(String.class);
assertEquals("mail body", body, text);
assertNotNull("attachments got lost", exchange.getIn().getAttachments());
for (String s : exchange.getIn().getAttachmentNames()) {
DataHandler dh = exchange.getIn().getAttachment(s);
Object content = dh.getContent();
assertNotNull("Content should not be empty", content);
assertEquals("log4j2.properties", dh.getName());
}
}
use of org.apache.camel.Exchange in project camel by apache.
the class MimeMultipartAlternativeWithLongerFilenameTest method verifyTheRecivedEmail.
private void verifyTheRecivedEmail(String expectString) throws Exception {
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(1000);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.assertIsSatisfied();
Exchange out = mock.assertExchangeReceived(0);
ByteArrayOutputStream baos = new ByteArrayOutputStream(((MailMessage) out.getIn()).getMessage().getSize());
((MailMessage) out.getIn()).getMessage().writeTo(baos);
String dumpedMessage = baos.toString();
assertTrue("There should have the " + expectString, dumpedMessage.indexOf(expectString) > 0);
log.trace("multipart alternative: \n{}", dumpedMessage);
// plain text
assertEquals(alternativeBody, out.getIn().getBody(String.class));
// attachment
Map<String, DataHandler> attachments = out.getIn().getAttachments();
assertNotNull("Should not have null attachments", attachments);
assertEquals(1, attachments.size());
assertEquals("multipart body should have 2 parts", 2, out.getIn().getBody(MimeMultipart.class).getCount());
}
use of org.apache.camel.Exchange in project camel by apache.
the class MultipleDestinationConsumeTest method testSendAndReceiveMails.
@Test
public void testSendAndReceiveMails() throws Exception {
Mailbox.clearAll();
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedMinimumMessageCount(1);
MimeMessage message = new MimeMessage(mailSession);
message.setText(body);
message.setRecipients(Message.RecipientType.TO, new Address[] { new InternetAddress("james@localhost"), new InternetAddress("bar@localhost") });
Transport.send(message);
// lets test the receive worked
resultEndpoint.assertIsSatisfied(100000);
Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
org.apache.camel.Message in = exchange.getIn();
assertNotNull("Should have headers", in.getHeaders());
MailMessage msg = (MailMessage) exchange.getIn();
Message inMessage = msg != null ? msg.getMessage() : null;
assertNotNull("In message has no JavaMail message!", inMessage);
String text = in.getBody(String.class);
assertEquals("mail body", body, text);
// need to use iterator as some mail impl returns String[] and others a single String with comma as separator
// so we let Camel create an iterator so we can use the same code for the test
Object to = in.getHeader("TO");
Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(to));
int i = 0;
while (it.hasNext()) {
if (i == 0) {
assertEquals("james@localhost", it.next().trim());
} else {
assertEquals("bar@localhost", it.next().trim());
}
i++;
}
Enumeration<Header> iter = CastUtils.cast(inMessage.getAllHeaders());
while (iter.hasMoreElements()) {
Header header = iter.nextElement();
String[] value = message.getHeader(header.getName());
log.debug("Header: " + header.getName() + " has value: " + ObjectHelper.asString(value));
}
}
Aggregations