Search in sources :

Example 16 with DataSource

use of javax.activation.DataSource in project webservices-axiom by apache.

the class TestBase64StreamingWithGetSAXSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement elem = factory.createOMElement("test", null);
    // Create a data source that would eat up all memory when loaded. If the test
    // doesn't fail with an OutOfMemoryError, we know that the OMText implementation
    // supports streaming.
    DataSource ds = new RandomDataSource(654321L, Runtime.getRuntime().maxMemory());
    OMText text = factory.createOMText(new DataHandler(ds), false);
    elem.addChild(text);
    SAXSource saxSource = elem.getSAXSource(true);
    XMLReader xmlReader = saxSource.getXMLReader();
    xmlReader.setContentHandler(new Base64Comparator(ds.getInputStream()));
    xmlReader.parse(saxSource.getInputSource());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) SAXSource(javax.xml.transform.sax.SAXSource) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) XMLReader(org.xml.sax.XMLReader) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 17 with DataSource

use of javax.activation.DataSource in project webservices-axiom by apache.

the class TestCreateSOAPModelBuilderMTOMContentTypeMismatch method runTest.

@Override
protected void runTest() throws Throwable {
    final SOAPSample sample = SOAPSampleSet.NO_HEADER.getMessage(spec);
    // Generate an MTOM message with the wrong content type
    MimeMessage message = new MimeMessage((Session) null);
    MimeMultipart mp = new MimeMultipart("related");
    MimeBodyPart bp = new MimeBodyPart();
    String contentID = "<" + UIDGenerator.generateContentId() + ">";
    bp.setDataHandler(new DataHandler(new DataSource() {

        @Override
        public String getContentType() {
            return "application/xop+xml; charset=\"" + sample.getEncoding() + "\"; type=\"" + spec.getAltSpec().getContentType() + "\"";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return sample.getInputStream();
        }

        @Override
        public String getName() {
            return null;
        }

        @Override
        public OutputStream getOutputStream() {
            throw new UnsupportedOperationException();
        }
    }));
    bp.addHeader("Content-Transfer-Encoding", "binary");
    bp.addHeader("Content-ID", contentID);
    mp.addBodyPart(bp);
    message.setContent(mp);
    message.saveChanges();
    ContentType contentType = new ContentType(message.getContentType()).toBuilder().setParameter("type", "application/xop+xml").setParameter("start", contentID).setParameter("start-info", spec.getAltSpec().getContentType()).build();
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    mp.writeTo(out);
    out.close();
    // Now attempt to create an Axiom builder
    try {
        OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(contentType).build());
        fail("Expected SOAPProcessingException");
    } catch (SOAPProcessingException ex) {
    // Expected
    }
}
Also used : ContentType(org.apache.axiom.mime.ContentType) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) SOAPProcessingException(org.apache.axiom.soap.SOAPProcessingException) DataHandler(javax.activation.DataHandler) SOAPSample(org.apache.axiom.ts.soap.SOAPSample) MimeBodyPart(javax.mail.internet.MimeBodyPart) DataSource(javax.activation.DataSource)

Example 18 with DataSource

use of javax.activation.DataSource in project webservices-axiom by apache.

the class TestGetDataHandlerFromElement method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    RandomDataSource orgDS = new RandomDataSource(64 * 1024);
    OMElement orgRoot = factory.createOMElement(new QName("root"));
    OMElement orgChild = factory.createOMElement(new QName("child"), orgRoot);
    orgChild.addChild(factory.createOMText(new DataHandler(orgDS), false));
    OMElement root = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader(orgRoot.toString())).getDocumentElement();
    XMLStreamReader reader = root.getXMLStreamReader(cache);
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    DataSource ds = XMLStreamReaderUtils.getDataHandlerFromElement(reader).getDataSource();
    IOTestUtils.compareStreams(orgDS.getInputStream(), ds.getInputStream());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 19 with DataSource

use of javax.activation.DataSource in project webservices-axiom by apache.

the class PartOnFileTest method testHeaderGetSet.

public void testHeaderGetSet() throws Exception {
    InputStream inStream = MTOMSample.SAMPLE1.getInputStream();
    Attachments attachments = new Attachments(inStream, MTOMSample.SAMPLE1.getContentType(), true, temp.getPath(), "1");
    DataHandler dh = attachments.getDataHandler("1.urn:uuid:A3ADBAEE51A1A87B2A11443668160943@apache.org");
    assertNotNull(dh);
    DataSource ds = dh.getDataSource();
    assertNotNull(ds);
    // The attachment cleanup code in Axis2 relies on the assumption that attachments written
    // to disk produce CachedFileDataSource instances.
    assertThat(ds).isInstanceOf(CachedFileDataSource.class);
    assertEquals("image/jpeg", dh.getContentType());
}
Also used : InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) DataSource(javax.activation.DataSource)

Example 20 with DataSource

use of javax.activation.DataSource 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)

Aggregations

DataSource (javax.activation.DataSource)53 DataHandler (javax.activation.DataHandler)31 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)16 InputStream (java.io.InputStream)12 MimeMultipart (javax.mail.internet.MimeMultipart)11 OMFactory (org.apache.axiom.om.OMFactory)11 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)11 MimeBodyPart (javax.mail.internet.MimeBodyPart)10 IOException (java.io.IOException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 FileDataSource (javax.activation.FileDataSource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OMElement (org.apache.axiom.om.OMElement)7 File (java.io.File)6 Reader (java.io.Reader)5 QName (javax.xml.namespace.QName)5 MimeHandlerException (com.zimbra.cs.mime.MimeHandlerException)4 Document (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document)4 URI (java.net.URI)4 Multipart (javax.mail.Multipart)4