Search in sources :

Example 16 with DataHandler

use of javax.activation.DataHandler 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());
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataHandler(javax.activation.DataHandler)

Example 17 with DataHandler

use of javax.activation.DataHandler 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)

Example 18 with DataHandler

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

the class MimeMultipartDataFormatTest method roundtripWithBinaryAttachmentsAndBinaryContent.

@Test
public void roundtripWithBinaryAttachmentsAndBinaryContent() throws IOException {
    String attContentType = "application/binary";
    byte[] attText = { 0, 1, 2, 3, 4, 5, 6, 7 };
    String attFileName = "Attachment File Name";
    in.setBody("Body text");
    DataSource ds = new ByteArrayDataSource(attText, attContentType);
    in.addAttachment(attFileName, new DataHandler(ds));
    Exchange result = template.send("direct:roundtripbinarycontent", exchange);
    Message out = result.getOut();
    assertEquals("Body text", out.getBody(String.class));
    assertTrue(out.hasAttachments());
    assertEquals(1, out.getAttachmentNames().size());
    assertThat(out.getAttachmentNames(), hasItem(attFileName));
    DataHandler dh = out.getAttachment(attFileName);
    assertNotNull(dh);
    assertEquals(attContentType, dh.getContentType());
    InputStream is = dh.getInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOHelper.copyAndCloseInput(is, os);
    assertArrayEquals(attText, os.toByteArray());
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.camel.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 19 with DataHandler

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

the class MimeMultipartDataFormatTest method roundtripWithBinaryAttachments.

@Test
public void roundtripWithBinaryAttachments() throws IOException {
    String attContentType = "application/binary";
    byte[] attText = { 0, 1, 2, 3, 4, 5, 6, 7 };
    String attFileName = "Attachment File Name";
    in.setBody("Body text");
    DataSource ds = new ByteArrayDataSource(attText, attContentType);
    in.addAttachment(attFileName, new DataHandler(ds));
    Exchange result = template.send("direct:roundtrip", exchange);
    Message out = result.getOut();
    assertEquals("Body text", out.getBody(String.class));
    assertTrue(out.hasAttachments());
    assertEquals(1, out.getAttachmentNames().size());
    assertThat(out.getAttachmentNames(), hasItem(attFileName));
    DataHandler dh = out.getAttachment(attFileName);
    assertNotNull(dh);
    assertEquals(attContentType, dh.getContentType());
    InputStream is = dh.getInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOHelper.copyAndCloseInput(is, os);
    assertArrayEquals(attText, os.toByteArray());
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.camel.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 20 with DataHandler

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

the class MimeMultipartDataFormatTest method roundtripWithTextAttachmentsAndSpecialCharacters.

@Test
@Ignore("Fails on CI servers and some platforms - maybe due locale or something")
public void roundtripWithTextAttachmentsAndSpecialCharacters() throws IOException {
    String attContentType = "text/plain";
    String attText = "Attachment Text with special characters: ©";
    String attFileName = "Attachment File Name with special characters: ©";
    in.setBody("Body text with special characters: ©");
    in.setHeader(Exchange.CONTENT_TYPE, "text/plain");
    in.setHeader(Exchange.CONTENT_ENCODING, "UTF8");
    addAttachment(attContentType, attText, attFileName);
    Exchange result = template.send("direct:roundtrip", exchange);
    Message out = result.getOut();
    assertEquals("Body text with special characters: ©", out.getBody(String.class));
    assertThat(out.getHeader(Exchange.CONTENT_TYPE, String.class), startsWith("text/plain"));
    assertEquals("UTF8", out.getHeader(Exchange.CONTENT_ENCODING));
    assertTrue(out.hasAttachments());
    assertEquals(1, out.getAttachmentNames().size());
    assertThat(out.getAttachmentNames(), hasItem(attFileName));
    DataHandler dh = out.getAttachment(attFileName);
    assertNotNull(dh);
    assertEquals(attContentType, dh.getContentType());
    InputStream is = dh.getInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOHelper.copyAndCloseInput(is, os);
    assertEquals(attText, new String(os.toByteArray()));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.camel.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DataHandler (javax.activation.DataHandler)180 Exchange (org.apache.camel.Exchange)39 MimeBodyPart (javax.mail.internet.MimeBodyPart)38 FileDataSource (javax.activation.FileDataSource)33 Test (org.junit.Test)33 DataSource (javax.activation.DataSource)32 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)25 MessagingException (javax.mail.MessagingException)25 MimeMultipart (javax.mail.internet.MimeMultipart)25 MimeMessage (javax.mail.internet.MimeMessage)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)17 Processor (org.apache.camel.Processor)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 File (java.io.File)13 PipedInputStream (java.io.PipedInputStream)13