Search in sources :

Example 1 with MemoryBlob

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

the class TestDataHandlerSerializationWithMTOM method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPFactory factory = metaFactory.getSOAP11Factory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    // Construct the original message
    DocumentBean object = new DocumentBean();
    object.setId("123456");
    object.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
    SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
    OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, object));
    orgEnvelope.getBody().addChild(element);
    // Serialize the message
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    orgEnvelope.serialize(out, format);
    out.close();
    assertFalse(element.isExpanded());
    // Parse the serialized message
    MultipartBody mb = MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(format.getContentType()).build();
    assertEquals(2, mb.getPartCount());
    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
    OMElement contentElement = envelope.getBody().getFirstElement().getFirstChildWithName(new QName("http://ws.apache.org/axiom/test/jaxb", "content"));
    OMText content = (OMText) contentElement.getFirstOMChild();
    assertTrue(content.isBinary());
    assertTrue(content.isOptimized());
    DataHandler dh = content.getDataHandler();
    assertEquals("some content", dh.getContent());
}
Also used : MemoryBlob(org.apache.axiom.blob.MemoryBlob) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) JAXBContext(javax.xml.bind.JAXBContext) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource) MultipartBody(org.apache.axiom.mime.MultipartBody) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) OMText(org.apache.axiom.om.OMText) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 2 with MemoryBlob

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

the class TestSerializeAsChild method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPEnvelope envelope = soapFactory.createDefaultSOAPMessage().getSOAPEnvelope();
    soapFactory.createOMElement("echo", soapFactory.createOMNamespace("urn:test", "p"), envelope.getBody());
    OMElement log = soapFactory.createOMElement("log", null);
    OMElement entry = soapFactory.createOMElement("entry", null, log);
    entry.addChild(envelope);
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    envelope.serialize(out);
    out.close();
    assertAbout(xml()).that(blob.getInputStream()).hasSameContentAs(xml(OMElement.class, envelope));
}
Also used : MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 3 with MemoryBlob

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

the class ElementHelper method toSOAPHeaderBlock.

/**
     * This is a method to convert regular OMElements to SOAPHeaderBlocks.
     * 
     * @param omElement
     * @param factory
     * @return TODO
     * @throws Exception
     */
public static SOAPHeaderBlock toSOAPHeaderBlock(OMElement omElement, SOAPFactory factory) throws Exception {
    if (omElement instanceof SOAPHeaderBlock)
        return (SOAPHeaderBlock) omElement;
    QName name = omElement.getQName();
    String localName = name.getLocalPart();
    OMNamespace namespace = factory.createOMNamespace(name.getNamespaceURI(), name.getPrefix());
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    omElement.serialize(out);
    out.close();
    BlobOMDataSource ds = new BlobOMDataSource(blob, "utf-8");
    SOAPHeaderBlock block = factory.createSOAPHeaderBlock(localName, namespace, ds);
    return block;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) MemoryBlob(org.apache.axiom.blob.MemoryBlob) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock)

Example 4 with MemoryBlob

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

the class AttachmentsTest method testGetRootPartContentID.

private void testGetRootPartContentID(String contentTypeStartParam, String contentId) throws Exception {
    MimeMessage message = new MimeMessage((Session) null);
    MimeMultipart mp = new MimeMultipart("related");
    MimeBodyPart rootPart = new MimeBodyPart();
    rootPart.setText("<root/>", "utf-8", "xml");
    rootPart.addHeader("Content-Transfer-Encoding", "binary");
    rootPart.addHeader("Content-ID", "<" + contentId + ">");
    mp.addBodyPart(rootPart);
    message.setContent(mp);
    message.saveChanges();
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    mp.writeTo(out);
    out.close();
    Attachments attachments = new Attachments(blob.getInputStream(), new ContentType(message.getContentType()).toBuilder().setParameter("start", contentTypeStartParam).build().toString());
    assertEquals("Did not obtain correct content ID", contentId, attachments.getRootPartContentID());
}
Also used : ContentType(org.apache.axiom.mime.ContentType) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MemoryBlob(org.apache.axiom.blob.MemoryBlob) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PipedOutputStream(java.io.PipedOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 5 with MemoryBlob

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

the class TestRegisterCustomBuilderForPayloadJAXBPlain method runTest.

@Override
protected void runTest() throws Throwable {
    DataHandler dh = new DataHandler(new RandomDataSource(10000));
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    createTestDocument(dh).serialize(out);
    out.close();
    test(dh, OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), blob.getInputStream()), false);
}
Also used : RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) DataHandler(javax.activation.DataHandler)

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