Search in sources :

Example 6 with ByteArrayDataSource

use of org.apache.axiom.attachments.ByteArrayDataSource in project webservices-axiom by apache.

the class MultipartBodyWriterTest method test.

private void test(String contentTransferEncoding) throws Exception {
    Random random = new Random();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MultipartBodyWriter mpw = new MultipartBodyWriter(baos, UIDGenerator.generateMimeBoundary());
    byte[] content = new byte[8192];
    random.nextBytes(content);
    OutputStream partOutputStream = mpw.writePart("application/octet-stream", contentTransferEncoding, UIDGenerator.generateContentId(), null);
    partOutputStream.write(content);
    partOutputStream.close();
    mpw.complete();
    MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(baos.toByteArray()));
    assertEquals(1, mp.getCount());
    MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(0);
    assertEquals(contentTransferEncoding, bp.getHeader("Content-Transfer-Encoding")[0]);
    baos.reset();
    bp.getDataHandler().writeTo(baos);
    assertTrue(Arrays.equals(content, baos.toByteArray()));
}
Also used : Random(java.util.Random) MimeMultipart(javax.mail.internet.MimeMultipart) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 7 with ByteArrayDataSource

use of org.apache.axiom.attachments.ByteArrayDataSource in project webservices-axiom by apache.

the class MIMEOutputUtils method complete.

/**
     * @deprecated Use {@link OMMultipartWriter} instead.
     */
public static void complete(OutputStream outStream, byte[] xmlData, LinkedList binaryNodeList, String boundary, String contentId, String charSetEncoding, String SOAPContentType, OMOutputFormat omOutputFormat) {
    try {
        log.debug("Start: write the SOAPPart and the attachments");
        // Write out the mime boundary
        startWritingMime(outStream, boundary);
        javax.activation.DataHandler dh = new javax.activation.DataHandler(new ByteArrayDataSource(xmlData, "text/xml; charset=" + charSetEncoding));
        MimeBodyPart rootMimeBodyPart = new MimeBodyPart();
        rootMimeBodyPart.setDataHandler(dh);
        rootMimeBodyPart.addHeader("Content-Type", "application/xop+xml; charset=" + charSetEncoding + "; type=\"" + SOAPContentType + "\"");
        rootMimeBodyPart.addHeader("Content-Transfer-Encoding", "binary");
        rootMimeBodyPart.addHeader("Content-ID", "<" + contentId + ">");
        // Write out the SOAPPart
        writeBodyPart(outStream, rootMimeBodyPart, boundary);
        // Now write out the Attachment parts (which are represented by the
        // text nodes int the binary node list)
        Iterator binaryNodeIterator = binaryNodeList.iterator();
        while (binaryNodeIterator.hasNext()) {
            OMText binaryNode = (OMText) binaryNodeIterator.next();
            writeBodyPart(outStream, createMimeBodyPart(binaryNode.getContentID(), binaryNode.getDataHandler(), omOutputFormat), boundary);
        }
        finishWritingMime(outStream);
        outStream.flush();
        log.debug("End: write the SOAPPart and the attachments");
    } catch (IOException e) {
        throw new OMException("Error while writing to the OutputStream.", e);
    } catch (MessagingException e) {
        throw new OMException("Problem writing Mime Parts.", e);
    }
}
Also used : MessagingException(javax.mail.MessagingException) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) DataHandler(javax.activation.DataHandler) ConfigurableDataHandler(org.apache.axiom.attachments.ConfigurableDataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) OMException(org.apache.axiom.om.OMException)

Aggregations

ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)7 DataHandler (javax.activation.DataHandler)4 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 OMElement (org.apache.axiom.om.OMElement)3 OMFactory (org.apache.axiom.om.OMFactory)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 Random (java.util.Random)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 QName (javax.xml.namespace.QName)2 OMText (org.apache.axiom.om.OMText)2 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 MessagingException (javax.mail.MessagingException)1 Attachments (org.apache.axiom.attachments.Attachments)1 ConfigurableDataHandler (org.apache.axiom.attachments.ConfigurableDataHandler)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMCloneOptions (org.apache.axiom.om.OMCloneOptions)1 OMDataSource (org.apache.axiom.om.OMDataSource)1 OMException (org.apache.axiom.om.OMException)1