Search in sources :

Example 11 with Attachment

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

the class MimeMultipartDataFormat method marshal.

@Override
public void marshal(Exchange exchange, Object graph, OutputStream stream) throws NoTypeConversionAvailableException, MessagingException, IOException {
    if (multipartWithoutAttachment || headersInline || exchange.getIn().hasAttachments()) {
        ContentType contentType = getContentType(exchange);
        // remove the Content-Type header. This will be wrong afterwards...
        exchange.getOut().removeHeader(Exchange.CONTENT_TYPE);
        byte[] bodyContent = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, graph);
        Session session = Session.getInstance(System.getProperties());
        MimeMessage mm = new MimeMessage(session);
        MimeMultipart mp = new MimeMultipart(multipartSubType);
        BodyPart part = new MimeBodyPart();
        writeBodyPart(bodyContent, part, contentType);
        mp.addBodyPart(part);
        for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) {
            String attachmentFilename = entry.getKey();
            Attachment attachment = entry.getValue();
            part = new MimeBodyPart();
            part.setDataHandler(attachment.getDataHandler());
            part.setFileName(MimeUtility.encodeText(attachmentFilename, "UTF-8", null));
            String ct = attachment.getDataHandler().getContentType();
            contentType = new ContentType(ct);
            part.setHeader(CONTENT_TYPE, ct);
            if (!contentType.match("text/*") && binaryContent) {
                part.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
            }
            // Set headers to the attachment
            for (String headerName : attachment.getHeaderNames()) {
                List<String> values = attachment.getHeaderAsList(headerName);
                for (String value : values) {
                    part.setHeader(headerName, value);
                }
            }
            mp.addBodyPart(part);
            exchange.getOut().removeAttachment(attachmentFilename);
        }
        mm.setContent(mp);
        // a String
        if (headersInline && includeHeaders != null) {
            for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
                if (includeHeaders.matcher(entry.getKey()).matches()) {
                    String headerStr = ExchangeHelper.convertToType(exchange, String.class, entry.getValue());
                    if (headerStr != null) {
                        mm.setHeader(entry.getKey(), headerStr);
                    }
                }
            }
        }
        mm.saveChanges();
        Enumeration<?> hl = mm.getAllHeaders();
        List<String> headers = new ArrayList<String>();
        if (!headersInline) {
            while (hl.hasMoreElements()) {
                Object ho = hl.nextElement();
                if (ho instanceof Header) {
                    Header h = (Header) ho;
                    exchange.getOut().setHeader(h.getName(), h.getValue());
                    headers.add(h.getName());
                }
            }
            mm.saveChanges();
        }
        mm.writeTo(stream, headers.toArray(new String[0]));
    } else {
        // keep the original data
        InputStream is = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);
        IOHelper.copyAndCloseInput(is, stream);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) ContentType(javax.mail.internet.ContentType) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Map(java.util.Map) Session(javax.mail.Session)

Example 12 with Attachment

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

the class SplitAttachmentsExpression method evaluate.

@Override
public Object evaluate(Exchange exchange) {
    // must use getAttachments to ensure attachments is initial populated
    if (exchange.getIn().getAttachments().isEmpty()) {
        return null;
    }
    try {
        List<Message> answer = new ArrayList<Message>();
        Message inMessage = exchange.getIn();
        for (Map.Entry<String, Attachment> entry : inMessage.getAttachmentObjects().entrySet()) {
            Message attachmentMessage;
            if (extractAttachments) {
                attachmentMessage = extractAttachment(inMessage, entry.getKey());
            } else {
                attachmentMessage = splitAttachment(inMessage, entry.getKey(), entry.getValue());
            }
            if (attachmentMessage != null) {
                answer.add(attachmentMessage);
            }
        }
        return answer;
    } catch (Exception e) {
        throw new RuntimeCamelException("Unable to split attachments from MimeMultipart message", e);
    }
}
Also used : Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) ArrayList(java.util.ArrayList) Attachment(org.apache.camel.Attachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Map(java.util.Map) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

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