Search in sources :

Example 16 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project nhin-d by DirectProject.

the class MDNFactory method create.

/**
     * Answers a MimeMultipartReport containing a
     * Message Delivery Notification as specified by RFC 2298.
     * 
     * @param humanText
     * @param reporting_UA_name
     * @param reporting_UA_product
     * @param original_recipient
     * @param final_recipient
     * @param original_message_id
     * @param disposition
     * @return MimeMultipartReport
     * @throws MessagingException
     */
public static MimeMultipartReport create(String humanText, String reporting_UA_name, String reporting_UA_product, String original_recipient, String final_recipient, String original_message_id, String error, MdnGateway gateway, Disposition disposition) throws MessagingException {
    if (disposition == null)
        throw new IllegalArgumentException("Disposition can not be null.");
    // Create the message parts. According to RFC 2298, there are two
    // compulsory parts and one optional part...
    MimeMultipartReport multiPart = new MimeMultipartReport();
    multiPart.setReportType("disposition-notification");
    // Part 1: The 'human-readable' part
    MimeBodyPart humanPart = new MimeBodyPart();
    humanPart.setText(humanText);
    multiPart.addBodyPart(humanPart);
    // Part 2: MDN Report Part
    // 1) reporting-ua-field
    StringBuilder mdnReport = new StringBuilder(128);
    if (reporting_UA_name != null && !reporting_UA_name.isEmpty()) {
        mdnReport.append("Reporting-UA: ");
        mdnReport.append((reporting_UA_name == null ? "" : reporting_UA_name));
        mdnReport.append("; ");
        mdnReport.append((reporting_UA_product == null ? "" : reporting_UA_product));
        mdnReport.append("\r\n");
    }
    // 2) original-recipient-field
    if (original_recipient != null && !original_recipient.isEmpty()) {
        mdnReport.append("Original-Recipient: ");
        mdnReport.append("rfc822; ");
        mdnReport.append(original_recipient);
        mdnReport.append("\r\n");
    }
    // 3) final-recipient-field
    if (final_recipient != null && !final_recipient.isEmpty()) {
        mdnReport.append("Final-Recipient: ");
        mdnReport.append("rfc822; ");
        mdnReport.append(final_recipient);
        mdnReport.append("\r\n");
    }
    // 4) original-message-id-field
    if (original_message_id != null && !original_message_id.isEmpty()) {
        mdnReport.append("Original-Message-ID: ");
        mdnReport.append(original_message_id);
        mdnReport.append("\r\n");
    }
    // 5) mdn-gateway-field
    if (gateway != null) {
        mdnReport.append("MDN-Gateway: ");
        mdnReport.append(gateway.toString());
        mdnReport.append("\r\n");
    }
    // 6) error-field
    if (error != null && !error.isEmpty()) {
        mdnReport.append("Error: ");
        mdnReport.append(error);
        mdnReport.append("\r\n");
    }
    mdnReport.append(disposition.toString());
    mdnReport.append("\r\n");
    MimeBodyPart mdnPart = new MimeBodyPart();
    try {
        // using a DataSource gets around some of the issues with the JAF dynamically loading content handlers that may not work, speicifically
        // the java dsn library and the DispostionNotification class which doesn't know how to handle byte arrays
        ByteArrayDataSource dataSource = new ByteArrayDataSource(new ByteArrayInputStream(mdnReport.toString().getBytes()), "message/disposition-notification");
        mdnPart.setDataHandler(new DataHandler(dataSource));
        multiPart.addBodyPart(mdnPart);
    } catch (IOException e) {
    /*no-op*/
    }
    // described in RFC 1892. It would be a useful addition!        
    return multiPart;
}
Also used : MimeMultipartReport(org.apache.mailet.base.mail.MimeMultipartReport) ByteArrayInputStream(java.io.ByteArrayInputStream) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 17 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project webservices-axiom by apache.

the class BufferUtilsTest method testByteArrayDataSourceBackedDataHandlerExceedLimit.

public void testByteArrayDataSourceBackedDataHandlerExceedLimit() throws Exception {
    String str = "This is a test String";
    byte[] b = str.getBytes();
    ByteArrayDataSource bads = new ByteArrayDataSource(b, "text/plain");
    DataHandler dh = new DataHandler(bads);
    int unsupported = BufferUtils.doesDataHandlerExceedLimit(dh, 0);
    assertEquals(-1, unsupported);
    int doesExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 10);
    assertEquals(1, doesExceed);
    int doesNotExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 100);
    assertEquals(0, doesNotExceed);
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 18 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource 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 19 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource 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 20 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource 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)

Aggregations

ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)40 DataHandler (javax.activation.DataHandler)27 MimeMultipart (javax.mail.internet.MimeMultipart)18 IOException (java.io.IOException)16 DataSource (javax.activation.DataSource)15 MessagingException (javax.mail.MessagingException)14 MimeBodyPart (javax.mail.internet.MimeBodyPart)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)9 MimeMessage (javax.mail.internet.MimeMessage)7 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)5 List (java.util.List)5 Test (org.junit.Test)5 ContentDisposition (com.zimbra.common.mime.ContentDisposition)4 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)4 Document (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document)4 Exchange (org.apache.camel.Exchange)3 ByteString (com.linkedin.data.ByteString)2