Search in sources :

Example 36 with DataHandler

use of javax.activation.DataHandler in project nhin-d by DirectProject.

the class DefaultXdsDirectDocumentsTransformer method transform.

@Override
public DirectDocuments transform(ProvideAndRegisterDocumentSetRequestType provideAndRegisterDocumentSetRequestType) throws TransformationException {
    DirectDocuments documents = new DirectDocuments();
    try {
        documents.setValues(provideAndRegisterDocumentSetRequestType.getSubmitObjectsRequest());
    } catch (MetadataException e) {
        throw new TransformationException("Unable to complete transformation due to metadata error", e);
    }
    for (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document document : provideAndRegisterDocumentSetRequestType.getDocument()) {
        byte[] data = null;
        try {
            DataHandler dataHandler = document.getValue();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            dataHandler.writeTo(outputStream);
            data = outputStream.toByteArray();
        } catch (IOException e) {
            throw new TransformationException("Unable to complete transformation due to document IO error", e);
        }
        DirectDocument2 doc = documents.getDocumentByUniqueId(document.getId());
        if (doc != null) {
            doc.setData(data);
        } else {
            documents.getDocumentById(document.getId()).setData(data);
        }
    }
    return documents;
}
Also used : TransformationException(org.nhindirect.xd.transform.exception.TransformationException) DirectDocument2(org.nhindirect.xd.common.DirectDocument2) DirectDocuments(org.nhindirect.xd.common.DirectDocuments) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MetadataException(org.nhindirect.xd.common.exception.MetadataException) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Example 37 with DataHandler

use of javax.activation.DataHandler in project nhin-d by DirectProject.

the class DefaultXdmXdsTransformerTest method getSampleXdmAsDataHandler.

/**
     * Return a sample XDM file as a DataHandler.
     * 
     * @return a sample XDM file.
     */
private static DataHandler getSampleXdmAsDataHandler() {
    URL url = DefaultXdmXdsTransformerTest.class.getClassLoader().getResource("samplexdm.zip");
    DataHandler dh = new DataHandler(url);
    return dh;
}
Also used : DataHandler(javax.activation.DataHandler) URL(java.net.URL)

Example 38 with DataHandler

use of javax.activation.DataHandler in project opennms by OpenNMS.

the class JavaMailer2 method createFileAttachment.

/*
    public Message buildMessage(String m_charSet, String m_encoding, String m_contentType) throws JavaMailerException {
        try {

            String encodedText = MimeUtility.encodeText(getMessageText(), m_charSet, m_encoding);
            if (getFileName() == null) {
                message.setContent(encodedText, m_contentType+"; charset="+m_charSet);
            } else {
                BodyPart bp = new MimeBodyPart();
                bp.setContent(encodedText, m_contentType+"; charset="+m_charSet);

                MimeMultipart mp = new MimeMultipart();
                mp.addBodyPart(bp);
                mp = new MimeMultipart();
                mp.addBodyPart(createFileAttachment(new File(getFileName())));
                message.setContent(mp);
            }

            message.setHeader("X-Mailer", getMailer());
            message.setSentDate(new Date());

            message.saveChanges();

            return message;
        } catch (AddressException e) {
            log().error("Java Mailer Addressing exception: ", e);
            throw new JavaMailerException("Java Mailer Addressing exception: ", e);
        } catch (MessagingException e) {
            log().error("Java Mailer messaging exception: ", e);
            throw new JavaMailerException("Java Mailer messaging exception: ", e);
        } catch (UnsupportedEncodingException e) {
            log().error("Java Mailer messaging exception: ", e);
            throw new JavaMailerException("Java Mailer encoding exception: ", e);
        }
    }
    */
/**
     * Create a file attachment as a MimeBodyPart, checking to see if the file
     * exists before we create the attachment.
     *
     * @param file file to attach
     * @return attachment body part
     * @throws javax.mail.MessagingException if we can't set the data handler or
     *      the file name on the MimeBodyPart
     * @throws org.opennms.javamail.JavaMailerException if the file does not exist or is not
     *      readable
     */
public MimeBodyPart createFileAttachment(final File file) throws MessagingException, JavaMailerException {
    if (!file.exists()) {
        LOG.error("File attachment '{}' does not exist.", file.getAbsolutePath());
        throw new JavaMailerException("File attachment '" + file.getAbsolutePath() + "' does not exist.");
    }
    if (!file.canRead()) {
        LOG.error("File attachment '{}' is not readable.", file.getAbsolutePath());
        throw new JavaMailerException("File attachment '" + file.getAbsolutePath() + "' is not readable.");
    }
    MimeBodyPart bodyPart = new MimeBodyPart();
    FileDataSource fds = new FileDataSource(file);
    bodyPart.setDataHandler(new DataHandler(fds));
    bodyPart.setFileName(fds.getName());
    return bodyPart;
}
Also used : FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 39 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class JavaMailMimeBodyPart method setContent.

@Override
public void setContent(Object o, String type) throws MessagingException {
    if (ZPARSER) {
        com.zimbra.common.mime.ContentType ctype = new com.zimbra.common.mime.ContentType(type);
        if (o instanceof JavaMailMimeMultipart) {
            setContent((Multipart) o);
            setHeader("Content-Type", type);
        } else if (o instanceof JavaMailMimeMessage) {
            replaceInParent(((JavaMailMimeMessage) o).getZimbraMimeMessage());
            setHeader("Content-Type", type);
            this.jmcontent = o;
        } else if (o instanceof String) {
            if (ctype.getPrimaryType().equals("text")) {
                setText((String) o, ctype.getParameter("charset"), ctype.getSubType());
                setHeader("Content-Type", type);
            } else {
                try {
                    setDataSource(new ByteArrayDataSource((String) o, type));
                } catch (IOException ioe) {
                    throw new MessagingException("error setting string content", ioe);
                }
            }
        } else {
            setDataHandler(new DataHandler(o, type));
        }
    } else {
        super.setContent(o, type);
    }
}
Also used : MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 40 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class JavaMailMimeMessage method setContent.

@Override
public void setContent(Object o, String type) throws MessagingException {
    if (ZPARSER) {
        com.zimbra.common.mime.ContentType ctype = new com.zimbra.common.mime.ContentType(type);
        if (o instanceof JavaMailMimeMultipart) {
            setContent((Multipart) o);
            setHeader("Content-Type", type);
        } else if (o instanceof JavaMailMimeMessage) {
            zmessage.setBodyPart(((JavaMailMimeMessage) o).getZimbraMimeMessage());
            setHeader("Content-Type", type);
            jmcontent = o;
        } else if (o instanceof String) {
            if (ctype.getPrimaryType().equals("text")) {
                setText((String) o, ctype.getParameter("charset"), ctype.getSubType());
                setHeader("Content-Type", type);
            } else {
                try {
                    setDataSource(new ByteArrayDataSource((String) o, type));
                } catch (IOException ioe) {
                    throw new MessagingException("error setting string content", ioe);
                }
            }
        } else {
            setDataHandler(new DataHandler(o, type));
        }
    } else {
        super.setContent(o, type);
    }
}
Also used : MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

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