Search in sources :

Example 1 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project webservices-axiom by apache.

the class MTOMSample method retrieveContent.

// START SNIPPET: retrieveContent
public void retrieveContent(URL serviceURL, String id, OutputStream result) throws Exception {
    // Build the SOAP request
    SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope request = soapFactory.getDefaultEnvelope();
    OMElement retrieveContent = soapFactory.createOMElement(new QName("urn:test", "retrieveContent"), request.getBody());
    OMElement fileId = soapFactory.createOMElement(new QName("fileId"), retrieveContent);
    fileId.setText(id);
    // Use the java.net.URL API to connect to the service
    URLConnection connection = serviceURL.openConnection();
    connection.setDoOutput(true);
    connection.addRequestProperty("Content-Type", "text/xml; charset=UTF-8");
    OutputStream out = connection.getOutputStream();
    // Send the request
    OMOutputFormat format = new OMOutputFormat();
    format.setCharSetEncoding("UTF-8");
    request.serialize(out, format);
    out.close();
    // Get the SOAP response
    InputStream in = connection.getInputStream();
    MultipartBody multipartBody = MultipartBody.builder().setInputStream(in).setContentType(connection.getContentType()).build();
    SOAPEnvelope response = OMXMLBuilderFactory.createSOAPModelBuilder(multipartBody).getSOAPEnvelope();
    OMElement retrieveContentResponse = response.getBody().getFirstElement();
    OMElement content = retrieveContentResponse.getFirstElement();
    // Extract the DataHandler representing the optimized binary data
    DataHandler dh = ((OMText) content.getFirstOMChild()).getDataHandler();
    // Stream the content of the MIME part
    InputStream contentStream = ((PartDataHandler) dh).getPart().getInputStream(false);
    // Write the content to the result stream
    IOUtils.copy(contentStream, result);
    contentStream.close();
    in.close();
}
Also used : QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) MultipartBody(org.apache.axiom.mime.MultipartBody) OutputStream(java.io.OutputStream) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) PartDataHandler(org.apache.axiom.mime.PartDataHandler) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) SOAPFactory(org.apache.axiom.soap.SOAPFactory) URLConnection(java.net.URLConnection)

Example 2 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project webservices-axiom by apache.

the class CharacterEncodingTest method runTest.

public void runTest(String value, String expected) throws XMLStreamException, FactoryConfigurationError, IOException {
    SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope envelope = factory.getDefaultEnvelope();
    String ns = "http://testuri.org";
    OMNamespace namespace = factory.createOMNamespace(ns, "tst");
    String ln = "Child";
    OMElement bodyChild = factory.createOMElement(ln, namespace);
    bodyChild.addChild(factory.createOMText(value));
    envelope.getBody().addChild(bodyChild);
    ByteArrayOutputStream byteOutStr = new ByteArrayOutputStream();
    OMOutputFormat outputFormat = new OMOutputFormat();
    outputFormat.setCharSetEncoding(UTF_16);
    envelope.serialize(byteOutStr, outputFormat);
    ByteArrayInputStream byteInStr = new ByteArrayInputStream(byteOutStr.toByteArray());
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(byteInStr, UTF_16);
    SOAPEnvelope resultEnv = builder.getSOAPEnvelope();
    OMElement bodyChildResult = resultEnv.getBody().getFirstElement();
    assertNotNull("No child in body element", bodyChildResult);
    String result = bodyChildResult.getText();
    assertNotNull("No value for testParam param", result);
    assertEquals("Expected result not received.", expected, result);
    builder.close();
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) ByteArrayInputStream(java.io.ByteArrayInputStream) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 3 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project webservices-axiom by apache.

the class ConvertLLOMToDOOMTest method testConvert1.

public void testConvert1() {
    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope env = fac.getDefaultEnvelope();
    fac.createOMElement(new QName("http://test.org", "Test"), env.getBody());
    env.build();
    SOAPModelBuilder doomBuilder = OMXMLBuilderFactory.createStAXSOAPModelBuilder(OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM), env.getXMLStreamReader());
    SOAPEnvelope doomEnv = doomBuilder.getSOAPEnvelope();
    doomEnv.build();
}
Also used : QName(javax.xml.namespace.QName) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 4 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory 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 5 with SOAPFactory

use of org.apache.axiom.soap.SOAPFactory in project webservices-axiom by apache.

the class TestMTOMForwardStreaming method runTest.

@Override
protected void runTest() throws Throwable {
    DataSource ds1 = new TestDataSource('A', Runtime.getRuntime().maxMemory());
    DataSource ds2 = new TestDataSource('B', Runtime.getRuntime().maxMemory());
    // Programmatically create the original message
    SOAPFactory factory = metaFactory.getSOAP12Factory();
    final SOAPEnvelope orgEnvelope = factory.createSOAPEnvelope();
    SOAPBody orgBody = factory.createSOAPBody(orgEnvelope);
    OMElement orgBodyElement = factory.createOMElement("test", factory.createOMNamespace("urn:test", "p"), orgBody);
    OMElement orgData1 = factory.createOMElement("data", null, orgBodyElement);
    orgData1.addChild(factory.createOMText(new DataHandler(ds1), true));
    OMElement orgData2 = factory.createOMElement("data", null, orgBodyElement);
    orgData2.addChild(factory.createOMText(new DataHandler(ds2), true));
    final OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    format.setSOAP11(false);
    final String contentType = format.getContentType();
    final PipedOutputStream pipe1Out = new PipedOutputStream();
    final PipedInputStream pipe1In = new PipedInputStream(pipe1Out);
    // Create the producer thread (simulating the client sending the MTOM message)
    Thread producerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                try {
                    orgEnvelope.serialize(pipe1Out, format);
                } finally {
                    pipe1Out.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    producerThread.start();
    final PipedOutputStream pipe2Out = new PipedOutputStream();
    PipedInputStream pipe2In = new PipedInputStream(pipe2Out);
    // Create the forwarder thread (simulating the mediation engine that forwards the message)
    Thread forwarderThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                try {
                    MultipartBody mb = MultipartBody.builder().setInputStream(pipe1In).setContentType(contentType).build();
                    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
                    // the element is built. Therefore we need two different test executions.
                    if (buildSOAPPart) {
                        envelope.build();
                    }
                    // Usage of serializeAndConsume should enable streaming
                    envelope.serializeAndConsume(pipe2Out, format);
                } finally {
                    pipe2Out.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    forwarderThread.start();
    try {
        MultipartBody mb = MultipartBody.builder().setInputStream(pipe2In).setContentType(contentType).build();
        SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
        OMElement bodyElement = envelope.getBody().getFirstElement();
        Iterator<OMElement> it = bodyElement.getChildElements();
        OMElement data1 = it.next();
        OMElement data2 = it.next();
        IOTestUtils.compareStreams(ds1.getInputStream(), ((PartDataHandler) ((OMText) data1.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
        IOTestUtils.compareStreams(ds2.getInputStream(), ((PartDataHandler) ((OMText) data2.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
    } finally {
        pipe2In.close();
    }
}
Also used : TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) OMElement(org.apache.axiom.om.OMElement) PipedOutputStream(java.io.PipedOutputStream) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) PartDataHandler(org.apache.axiom.mime.PartDataHandler) PipedInputStream(java.io.PipedInputStream) SOAPFactory(org.apache.axiom.soap.SOAPFactory) DataSource(javax.activation.DataSource) TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) SOAPBody(org.apache.axiom.soap.SOAPBody) PartDataHandler(org.apache.axiom.mime.PartDataHandler) MultipartBody(org.apache.axiom.mime.MultipartBody) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Aggregations

SOAPFactory (org.apache.axiom.soap.SOAPFactory)69 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)49 OMElement (org.apache.axiom.om.OMElement)38 QName (javax.xml.namespace.QName)16 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)14 SOAPBody (org.apache.axiom.soap.SOAPBody)12 MessageContext (org.apache.axis2.context.MessageContext)11 DataHandler (javax.activation.DataHandler)10 AxisFault (org.apache.axis2.AxisFault)10 OMNamespace (org.apache.axiom.om.OMNamespace)9 SOAPHeader (org.apache.axiom.soap.SOAPHeader)9 SynapseException (org.apache.synapse.SynapseException)8 Iterator (java.util.Iterator)7 OMNode (org.apache.axiom.om.OMNode)7 SOAPFault (org.apache.axiom.soap.SOAPFault)7 EndpointReference (org.apache.axis2.addressing.EndpointReference)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 OMAttribute (org.apache.axiom.om.OMAttribute)4