Search in sources :

Example 11 with MemoryBlob

use of org.apache.axiom.blob.MemoryBlob in project webservices-axiom by apache.

the class XMLStreamReaderUtils method getDataHandlerFromElement.

/**
     * Get a {@link DataHandler} for the binary data encoded in an element. The method supports
     * base64 encoded character data as well as optimized binary data through the
     * {@link DataHandlerReader} extension.
     * <p>
     * <em>Precondition</em>: the reader is on a {@link XMLStreamConstants#START_ELEMENT}
     * <p>
     * <em>Postcondition</em>: the reader is on the corresponding
     * {@link XMLStreamConstants#END_ELEMENT}
     * 
     * @param reader the stream to read the data from
     * @return the binary data from the element
     */
public static DataHandler getDataHandlerFromElement(XMLStreamReader reader) throws XMLStreamException {
    int event = reader.next();
    if (event == XMLStreamConstants.END_ELEMENT) {
        // This means that the element is actually empty -> return empty DataHandler
        return new DataHandler(new EmptyDataSource("application/octet-stream"));
    } else if (event != XMLStreamConstants.CHARACTERS) {
        throw new XMLStreamException("Expected a CHARACTER event");
    }
    DataHandlerReader dhr = getDataHandlerReader(reader);
    if (dhr != null && dhr.isBinary()) {
        DataHandler dh = dhr.getDataHandler();
        reader.next();
        return dh;
    } else {
        MemoryBlob blob = Blobs.createMemoryBlob();
        Writer out = new Base64DecodingOutputStreamWriter(blob.getOutputStream());
        try {
            writeTextTo(reader, out);
            // CHARACTERS events
            loop: while (true) {
                switch(reader.next()) {
                    case XMLStreamConstants.CHARACTERS:
                        writeTextTo(reader, out);
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        break loop;
                    default:
                        throw new XMLStreamException("Expected a CHARACTER event");
                }
            }
            out.close();
        } catch (IOException ex) {
            throw new XMLStreamException("Error during base64 decoding", ex);
        }
        return new DataHandler(new BlobDataSource(blob, "application/octet-string"));
    }
}
Also used : BlobDataSource(org.apache.axiom.blob.BlobDataSource) Base64DecodingOutputStreamWriter(org.apache.axiom.util.base64.Base64DecodingOutputStreamWriter) EmptyDataSource(org.apache.axiom.util.activation.EmptyDataSource) XMLStreamException(javax.xml.stream.XMLStreamException) MemoryBlob(org.apache.axiom.blob.MemoryBlob) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) DataHandlerReader(org.apache.axiom.ext.stax.datahandler.DataHandlerReader) Base64DecodingOutputStreamWriter(org.apache.axiom.util.base64.Base64DecodingOutputStreamWriter) Writer(java.io.Writer)

Example 12 with MemoryBlob

use of org.apache.axiom.blob.MemoryBlob in project webservices-axiom by apache.

the class TestReadAttachmentBeforeRootPartComplete method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    // Programmatically create the message
    OMElement orgRoot = factory.createOMElement("root", null);
    OMElement orgChild1 = factory.createOMElement("child1", null, orgRoot);
    DataSource ds = new RandomDataSource(54321, 4096);
    orgChild1.addChild(factory.createOMText(new DataHandler(ds), true));
    // Create a child with a large text content and insert it after the binary node.
    // If we don't do this, then the root part may be buffered entirely by the parser,
    // and the test would not be effective.
    OMElement orgChild2 = factory.createOMElement("child2", null, orgRoot);
    String s = RandomUtils.randomString(128 * 1024);
    orgChild2.setText(s);
    // Serialize the message
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    orgRoot.serialize(out, format);
    out.close();
    // Parse the message
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, StAXParserConfiguration.NON_COALESCING, MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(format.getContentType()).build());
    OMElement root = builder.getDocumentElement();
    OMElement child1 = (OMElement) root.getFirstOMChild();
    OMText text = (OMText) child1.getFirstOMChild();
    assertTrue(text.isBinary());
    // Access the DataHandler
    DataHandler dh = text.getDataHandler();
    IOTestUtils.compareStreams(ds.getInputStream(), dh.getInputStream());
    OMElement child2 = (OMElement) child1.getNextOMSibling();
    assertFalse(child2.isComplete());
    assertEquals(s, child2.getText());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Aggregations

MemoryBlob (org.apache.axiom.blob.MemoryBlob)12 OutputStream (java.io.OutputStream)9 DataHandler (javax.activation.DataHandler)7 IOException (java.io.IOException)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMessage (javax.mail.internet.MimeMessage)3 MimeMultipart (javax.mail.internet.MimeMultipart)3 OMElement (org.apache.axiom.om.OMElement)3 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)3 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 Writer (java.io.Writer)2 DataSource (javax.activation.DataSource)2 QName (javax.xml.namespace.QName)2 ContentType (org.apache.axiom.mime.ContentType)2 MultipartBody (org.apache.axiom.mime.MultipartBody)2 OMException (org.apache.axiom.om.OMException)2 OMText (org.apache.axiom.om.OMText)2 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)2