Search in sources :

Example 6 with ByteArrayDataSource

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

the class DirectDocuments method toProvideAndRegisterDocumentSetRequestType.

public ProvideAndRegisterDocumentSetRequestType toProvideAndRegisterDocumentSetRequestType() throws IOException {
    ProvideAndRegisterDocumentSetRequestType request = new ProvideAndRegisterDocumentSetRequestType();
    request.setSubmitObjectsRequest(this.getSubmitObjectsRequest());
    for (DirectDocument2 document : documents) {
        if (document.getData() != null) {
            DataSource source = new ByteArrayDataSource(document.getData(), document.getMetadata().getMimeType());
            DataHandler dhnew = new DataHandler(source);
            Document pdoc = new Document();
            pdoc.setValue(dhnew);
            String id = document.getMetadata().getId();
            pdoc.setId(id);
            request.getDocument().add(pdoc);
        }
    }
    return request;
}
Also used : DataHandler(javax.activation.DataHandler) Document(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 7 with ByteArrayDataSource

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

the class DefaultXdmXdsTransformer method transform.

/*
     * (non-Javadoc)
     * 
     * @see org.nhindirect.transform.XdmXdsTransformer#transform(java.io.File)
     */
@Override
public ProvideAndRegisterDocumentSetRequestType transform(File file) throws TransformationException {
    LOGGER.trace("Begin transformation of XDM to XDS (file)");
    String docId = null;
    ZipFile zipFile = null;
    String docName = getDocName(file);
    if (docName != null) {
        XDM_FILENAME_DATA = docName;
    }
    ProvideAndRegisterDocumentSetRequestType prsr = new ProvideAndRegisterDocumentSetRequestType();
    try {
        zipFile = new ZipFile(file, ZipFile.OPEN_READ);
        Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        ZipEntry zipEntry = null;
        // load the ZIP archive into memory
        while (zipEntries.hasMoreElements()) {
            zipEntry = zipEntries.nextElement();
            String zname = zipEntry.getName();
            LOGGER.trace("Processing a ZipEntry " + zname);
            if (!zipEntry.isDirectory()) {
                String subsetDirspec = getSubmissionSetDirspec(zipEntry.getName());
                // Read metadata
                if (matchName(zname, subsetDirspec, XDM_FILENAME_METADATA)) {
                    ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
                    SubmitObjectsRequest submitObjectRequest = (SubmitObjectsRequest) XmlUtils.unmarshal(byteArrayOutputStream.toString(), oasis.names.tc.ebxml_regrep.xsd.lcm._3.ObjectFactory.class);
                    prsr.setSubmitObjectsRequest(submitObjectRequest);
                    docId = getDocId(submitObjectRequest);
                } else // Read data
                if (matchName(zname, subsetDirspec, XDM_FILENAME_DATA)) {
                    ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
                    DataSource source = new ByteArrayDataSource(byteArrayOutputStream.toByteArray(), MimeType.APPLICATION_XML + "; charset=UTF-8");
                    DataHandler dhnew = new DataHandler(source);
                    Document pdoc = new Document();
                    pdoc.setValue(dhnew);
                    pdoc.setId(docId);
                    List<Document> docs = prsr.getDocument();
                    docs.add(pdoc);
                }
            }
            if (!prsr.getDocument().isEmpty()) {
                ((Document) prsr.getDocument().get(0)).setId(zname);
            }
        }
        zipFile.close();
    } catch (Exception e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Unable to complete transformation.", e);
        }
        throw new TransformationException("Unable to complete transformation.", e);
    }
    return prsr;
}
Also used : TransformationException(org.nhindirect.xd.transform.exception.TransformationException) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataHandler(javax.activation.DataHandler) Document(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document) SubmitObjectsRequest(oasis.names.tc.ebxml_regrep.xsd.lcm._3.SubmitObjectsRequest) TransformationException(org.nhindirect.xd.transform.exception.TransformationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) ZipFile(java.util.zip.ZipFile) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Example 8 with ByteArrayDataSource

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

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

Example 10 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project zm-mailbox by Zimbra.

the class MailboxTestUtil method generateMessageWithAttachment.

public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Vera Oliphant <oli@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.setText("Good as gold");
    MimeMultipart multi = new ZMimeMultipart("mixed");
    ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
    cdisp.setParameter("filename", "fun.txt");
    ZMimeBodyPart bp = new ZMimeBodyPart();
    // it gets called before setting Content-Type and CTE headers.
    try {
        bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain")));
    } catch (IOException e) {
        throw new MessagingException("could not generate mime part content", e);
    }
    bp.addHeader("Content-Disposition", cdisp.toString());
    bp.addHeader("Content-Type", "text/plain");
    bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
    multi.addBodyPart(bp);
    mm.setContent(multi);
    mm.saveChanges();
    return new ParsedMessage(mm, false);
}
Also used : ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

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