Search in sources :

Example 1 with BlobDataSource

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

the class TestGetObject method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    DataSource ds = new BlobDataSource(Blobs.createBlob("test".getBytes("utf-8")), "text/plain; charset=utf-8");
    OMSourcedElement element = factory.createOMElement(new WrappedTextNodeOMDataSourceFromDataSource(new QName("wrapper"), ds, Charset.forName("utf-8")));
    // getObject returns null if the data source is not of the expected type
    assertNull(element.getObject(StringOMDataSource.class));
    // Test with the right data source type
    assertSame(ds, element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
    assertSame(ds, element.getObject(WrappedTextNodeOMDataSource.class));
    // Now modify the content of the element
    factory.createOMComment(element, "comment");
    assertNull(element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) BlobDataSource(org.apache.axiom.blob.BlobDataSource) QName(javax.xml.namespace.QName) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) WrappedTextNodeOMDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSource) WrappedTextNodeOMDataSourceFromDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) WrappedTextNodeOMDataSourceFromDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource) DataSource(javax.activation.DataSource) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) BlobDataSource(org.apache.axiom.blob.BlobDataSource) WrappedTextNodeOMDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSource)

Example 2 with BlobDataSource

use of org.apache.axiom.blob.BlobDataSource 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 3 with BlobDataSource

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

the class AttachmentMarshallerImpl method addMtomAttachment.

@Override
public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String elementNamespace, String elementLocalName) {
    // TODO: instead of copying the array, we could use a specialized DataHandler/DataSource
    if (offset != 0 || length != data.length) {
        int len = length - offset;
        byte[] newData = new byte[len];
        System.arraycopy(data, offset, newData, 0, len);
        data = newData;
    }
    return addMtomAttachment(new DataHandler(new BlobDataSource(Blobs.createBlob(data), "application/octet-stream")), elementNamespace, elementLocalName);
}
Also used : BlobDataSource(org.apache.axiom.blob.BlobDataSource) DataHandler(javax.activation.DataHandler)

Aggregations

BlobDataSource (org.apache.axiom.blob.BlobDataSource)3 DataHandler (javax.activation.DataHandler)2 IOException (java.io.IOException)1 Writer (java.io.Writer)1 DataSource (javax.activation.DataSource)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 MemoryBlob (org.apache.axiom.blob.MemoryBlob)1 DataHandlerReader (org.apache.axiom.ext.stax.datahandler.DataHandlerReader)1 OMFactory (org.apache.axiom.om.OMFactory)1 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)1 StringOMDataSource (org.apache.axiom.om.ds.StringOMDataSource)1 WrappedTextNodeOMDataSource (org.apache.axiom.om.ds.WrappedTextNodeOMDataSource)1 WrappedTextNodeOMDataSourceFromDataSource (org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource)1 EmptyDataSource (org.apache.axiom.util.activation.EmptyDataSource)1 Base64DecodingOutputStreamWriter (org.apache.axiom.util.base64.Base64DecodingOutputStreamWriter)1