Search in sources :

Example 1 with DocumentBean

use of org.apache.axiom.ts.jaxb.beans.DocumentBean 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 DocumentBean

use of org.apache.axiom.ts.jaxb.beans.DocumentBean in project webservices-axiom by apache.

the class TestUnmarshalWithDataHandler method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    DocumentBean orgBean = new DocumentBean();
    orgBean.setId("AB23498");
    orgBean.setContent(new DataHandler("test content", "text/plain"));
    OMElement element = factory.createOMElement(new JAXBOMDataSource(context, orgBean));
    DocumentBean bean = (DocumentBean) element.unmarshal(context, null, true);
    assertEquals(orgBean.getId(), bean.getId());
    assertEquals(orgBean.getContent(), bean.getContent());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) JAXBContext(javax.xml.bind.JAXBContext) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)

Example 3 with DocumentBean

use of org.apache.axiom.ts.jaxb.beans.DocumentBean in project webservices-axiom by apache.

the class TestDataHandlerExpansion method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    DataHandler dh = new DataHandler("some content", "text/plain");
    DocumentBean object = new DocumentBean();
    object.setId("123456");
    object.setContent(dh);
    OMElement element = factory.createOMElement(new JAXBOMDataSource(context, object));
    OMElement child = (OMElement) element.getFirstOMChild();
    assertEquals("id", child.getLocalName());
    assertEquals("123456", child.getText());
    child = (OMElement) child.getNextOMSibling();
    assertEquals("content", child.getLocalName());
    OMText content = (OMText) child.getFirstOMChild();
    assertTrue(content.isBinary());
    assertTrue(content.isOptimized());
    assertSame(dh, content.getDataHandler());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) OMText(org.apache.axiom.om.OMText) JAXBContext(javax.xml.bind.JAXBContext) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)

Example 4 with DocumentBean

use of org.apache.axiom.ts.jaxb.beans.DocumentBean in project webservices-axiom by apache.

the class TestDataHandlerSerializationWithoutMTOM method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPFactory factory = metaFactory.getSOAP11Factory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    // Construct the original message
    DocumentBean orgObject = new DocumentBean();
    orgObject.setId("123456");
    orgObject.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
    SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
    OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, orgObject));
    orgEnvelope.getBody().addChild(element);
    // Serialize the message
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    orgEnvelope.serialize(out);
    assertFalse(element.isExpanded());
    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(new ByteArrayInputStream(out.toByteArray()), null).getSOAPEnvelope();
    DocumentBean object = (DocumentBean) context.createUnmarshaller().unmarshal(envelope.getBody().getFirstElement().getXMLStreamReader(false));
    assertEquals("some content", IOUtils.toString(object.getContent().getInputStream(), "utf-8"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) JAXBContext(javax.xml.bind.JAXBContext) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)

Example 5 with DocumentBean

use of org.apache.axiom.ts.jaxb.beans.DocumentBean in project webservices-axiom by apache.

the class TestExceptionDuringSerialization method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory omFactory = metaFactory.getOMFactory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    DocumentBean object = new DocumentBean();
    object.setId("test");
    OMSourcedElement element = omFactory.createOMElement(new JAXBOMDataSource(context, object));
    XMLStreamException exception = new XMLStreamException("TEST");
    try {
        element.serialize(new ExceptionXMLStreamWriterWrapper(StAX.createNullXMLStreamWriter(), exception));
        fail("Expected XMLStreamException");
    } catch (XMLStreamException ex) {
        assertSame(exception, ex);
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) JAXBContext(javax.xml.bind.JAXBContext) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)6 JAXBOMDataSource (org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)6 DocumentBean (org.apache.axiom.ts.jaxb.beans.DocumentBean)6 DataHandler (javax.activation.DataHandler)4 OMFactory (org.apache.axiom.om.OMFactory)4 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)4 OMElement (org.apache.axiom.om.OMElement)3 OMText (org.apache.axiom.om.OMText)2 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)2 SOAPFactory (org.apache.axiom.soap.SOAPFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 MemoryBlob (org.apache.axiom.blob.MemoryBlob)1 MultipartBody (org.apache.axiom.mime.MultipartBody)1 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)1