Search in sources :

Example 1 with Attachment

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

the class MailBinding method addAttachmentsToMultipart.

protected void addAttachmentsToMultipart(MimeMultipart multipart, String partDisposition, AttachmentsContentTransferEncodingResolver encodingResolver, Exchange exchange) throws MessagingException {
    LOG.trace("Adding attachments +++ start +++");
    int i = 0;
    for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) {
        String attachmentFilename = entry.getKey();
        Attachment attachment = entry.getValue();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Attachment #{}: Disposition: {}", i, partDisposition);
            LOG.trace("Attachment #{}: DataHandler: {}", i, attachment.getDataHandler());
            LOG.trace("Attachment #{}: FileName: {}", i, attachmentFilename);
        }
        if (attachment != null) {
            if (shouldAddAttachment(exchange, attachmentFilename, attachment.getDataHandler())) {
                // Create another body part
                BodyPart messageBodyPart = new MimeBodyPart();
                // Set the data handler to the attachment
                messageBodyPart.setDataHandler(attachment.getDataHandler());
                // Set headers to the attachment
                for (String headerName : attachment.getHeaderNames()) {
                    List<String> values = attachment.getHeaderAsList(headerName);
                    for (String value : values) {
                        messageBodyPart.setHeader(headerName, value);
                    }
                }
                if (attachmentFilename.toLowerCase().startsWith("cid:")) {
                    // add a Content-ID header to the attachment
                    // must use angle brackets according to RFC: http://www.ietf.org/rfc/rfc2392.txt
                    messageBodyPart.addHeader("Content-ID", "<" + attachmentFilename.substring(4) + ">");
                    // Set the filename without the cid
                    messageBodyPart.setFileName(attachmentFilename.substring(4));
                } else {
                    // Set the filename
                    messageBodyPart.setFileName(attachmentFilename);
                }
                LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
                if (contentTypeResolver != null) {
                    String contentType = contentTypeResolver.resolveContentType(attachmentFilename);
                    LOG.trace("Attachment #" + i + ": Using content type resolver: " + contentTypeResolver + " resolved content type as: " + contentType);
                    if (contentType != null) {
                        String value = contentType + "; name=" + attachmentFilename;
                        messageBodyPart.setHeader("Content-Type", value);
                        LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
                    }
                }
                // set Content-Transfer-Encoding using resolver if possible
                resolveContentTransferEncoding(encodingResolver, i, messageBodyPart);
                // Set Disposition
                messageBodyPart.setDisposition(partDisposition);
                // Add part to multipart
                multipart.addBodyPart(messageBodyPart);
            } else {
                LOG.trace("shouldAddAttachment: false");
            }
        } else {
            LOG.warn("Cannot add attachment: " + attachmentFilename + " as DataHandler is null");
        }
        i++;
    }
    LOG.trace("Adding attachments +++ done +++");
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with Attachment

use of org.apache.camel.Attachment 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 Attachment

use of org.apache.camel.Attachment 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 Attachment

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

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

the class MimeMultipartDataFormatTest method unmarshalRelated.

@Test
public void unmarshalRelated() throws IOException {
    in.setBody(new File("src/test/resources/multipart-related.txt"));
    Attachment dh = unmarshalAndCheckAttachmentName("950120.aaCB@XIson.com");
    assertNotNull(dh);
    assertEquals("The fixed length records", dh.getHeader("Content-Description"));
    assertEquals("header value1,header value2", dh.getHeader("X-Additional-Header"));
    assertEquals(2, dh.getHeaderAsList("X-Additional-Header").size());
}
Also used : Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) File(java.io.File) Test(org.junit.Test)

Aggregations

Attachment (org.apache.camel.Attachment)12 DefaultAttachment (org.apache.camel.impl.DefaultAttachment)9 DataHandler (javax.activation.DataHandler)5 Exchange (org.apache.camel.Exchange)5 Message (org.apache.camel.Message)5 Test (org.junit.Test)4 Map (java.util.Map)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 StringContains.containsString (org.hamcrest.core.StringContains.containsString)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 DataSource (javax.activation.DataSource)2 FileDataSource (javax.activation.FileDataSource)2 BodyPart (javax.mail.BodyPart)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 Part (javax.servlet.http.Part)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2