Search in sources :

Example 26 with DataSource

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

the class MimeMultipartDataFormat method writeBodyPart.

private void writeBodyPart(byte[] bodyContent, Part part, ContentType contentType) throws MessagingException {
    DataSource ds = new ByteArrayDataSource(bodyContent, contentType.toString());
    part.setDataHandler(new DataHandler(ds));
    part.setHeader(CONTENT_TYPE, contentType.toString());
    if (contentType.match("text/*")) {
        part.setHeader(CONTENT_TRANSFER_ENCODING, "8bit");
    } else if (binaryContent) {
        part.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
    } else {
        part.setHeader(CONTENT_TRANSFER_ENCODING, "base64");
    }
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 27 with DataSource

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

the class MailBinding method populateContentOnMimeMessage.

protected String populateContentOnMimeMessage(MimeMessage part, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException {
    String contentType = determineContentType(configuration, exchange);
    LOG.trace("Using Content-Type {} for MimeMessage: {}", contentType, part);
    String body = exchange.getIn().getBody(String.class);
    if (body == null) {
        body = "";
    }
    // always store content in a byte array data store to avoid various content type and charset issues
    DataSource ds = new ByteArrayDataSource(body, contentType);
    part.setDataHandler(new DataHandler(ds));
    // set the content type header afterwards
    part.setHeader("Content-Type", contentType);
    return contentType;
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 28 with DataSource

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

the class MailBinding method populateContentOnBodyPart.

protected String populateContentOnBodyPart(BodyPart part, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException {
    String contentType = determineContentType(configuration, exchange);
    if (contentType != null) {
        LOG.trace("Using Content-Type {} for BodyPart: {}", contentType, part);
        // always store content in a byte array data store to avoid various content type and charset issues
        String data = exchange.getContext().getTypeConverter().tryConvertTo(String.class, exchange.getIn().getBody());
        // use empty data if the body was null for some reason (otherwise there is a NPE)
        data = data != null ? data : "";
        DataSource ds = new ByteArrayDataSource(data, contentType);
        part.setDataHandler(new DataHandler(ds));
        // set the content type header afterwards
        part.setHeader("Content-Type", contentType);
    }
    return contentType;
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 29 with DataSource

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

the class MimeMessageConsumeTest method populateMimeMessageBody.

/**
     * Lets encode a multipart mime message
     */
protected void populateMimeMessageBody(MimeMessage message) throws MessagingException {
    MimeBodyPart plainPart = new MimeBodyPart();
    plainPart.setText(body);
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setText("<html><body>" + body + "</body></html>");
    Multipart alt = new MimeMultipart("alternative");
    alt.addBodyPart(plainPart);
    alt.addBodyPart(htmlPart);
    Multipart mixed = new MimeMultipart("mixed");
    MimeBodyPart wrap = new MimeBodyPart();
    wrap.setContent(alt);
    mixed.addBodyPart(wrap);
    mixed.addBodyPart(plainPart);
    mixed.addBodyPart(htmlPart);
    DataSource ds;
    try {
        File f = new File(getClass().getResource("/log4j2.properties").toURI());
        ds = new FileDataSource(f);
    } catch (URISyntaxException ex) {
        ds = new URLDataSource(getClass().getResource("/log4j2.properties"));
    }
    DataHandler dh = new DataHandler(ds);
    BodyPart attachmentBodyPart;
    // Create another body part
    attachmentBodyPart = new MimeBodyPart();
    // Set the data handler to the attachment
    attachmentBodyPart.setDataHandler(dh);
    // Set the filename
    attachmentBodyPart.setFileName(dh.getName());
    // Set Disposition
    attachmentBodyPart.setDisposition(Part.ATTACHMENT);
    mixed.addBodyPart(plainPart);
    mixed.addBodyPart(htmlPart);
    // Add attachmentBodyPart to multipart
    mixed.addBodyPart(attachmentBodyPart);
    message.setContent(mixed);
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) URLDataSource(javax.activation.URLDataSource) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) URISyntaxException(java.net.URISyntaxException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource)

Example 30 with DataSource

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

the class MimeMultipartDataFormatTest method addAttachment.

private void addAttachment(String attContentType, String attText, String attFileName, Map<String, String> headers) throws IOException {
    DataSource ds = new ByteArrayDataSource(attText, attContentType);
    DefaultAttachment attachment = new DefaultAttachment(ds);
    if (headers != null) {
        for (String headerName : headers.keySet()) {
            attachment.addHeader(headerName, headers.get(headerName));
        }
    }
    in.addAttachmentObject(attFileName, attachment);
}
Also used : DefaultAttachment(org.apache.camel.impl.DefaultAttachment) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Aggregations

DataSource (javax.activation.DataSource)53 DataHandler (javax.activation.DataHandler)31 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)16 InputStream (java.io.InputStream)12 MimeMultipart (javax.mail.internet.MimeMultipart)11 OMFactory (org.apache.axiom.om.OMFactory)11 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)11 MimeBodyPart (javax.mail.internet.MimeBodyPart)10 IOException (java.io.IOException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 FileDataSource (javax.activation.FileDataSource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OMElement (org.apache.axiom.om.OMElement)7 File (java.io.File)6 Reader (java.io.Reader)5 QName (javax.xml.namespace.QName)5 MimeHandlerException (com.zimbra.cs.mime.MimeHandlerException)4 Document (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document)4 URI (java.net.URI)4 Multipart (javax.mail.Multipart)4