Search in sources :

Example 6 with OMElement

use of org.apache.axiom.om.OMElement 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 7 with OMElement

use of org.apache.axiom.om.OMElement in project webservices-axiom by apache.

the class ValidateSample method validate.

// START SNIPPET: sax
public void validate(InputStream in, URL schemaUrl) throws Exception {
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(in, "UTF-8");
    SOAPEnvelope envelope = builder.getSOAPEnvelope();
    OMElement bodyContent = envelope.getBody().getFirstElement();
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaUrl);
    Validator validator = schema.newValidator();
    validator.validate(bodyContent.getSAXSource(true));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) OMElement(org.apache.axiom.om.OMElement) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) Validator(javax.xml.validation.Validator)

Example 8 with OMElement

use of org.apache.axiom.om.OMElement in project webservices-axiom by apache.

the class MTOMStAXSOAPModelBuilderTest method testCreateSerializeAndSerializeOptimized.

/**
     * Test reading a message containing XOP.
     * Serialize the tree (with caching).
     * Then ensure that the XOP is preserved when it is serialized again.
     * <p>
     * Regression test for AXIOM-264.
     * 
     * @throws Exception
     */
public void testCreateSerializeAndSerializeOptimized() throws Exception {
    OMElement root = createTestMTOMMessage();
    // Serialize the tree (with caching).
    root.serialize(new ByteArrayOutputStream());
    // Write out the source
    checkSerialization(root, true);
}
Also used : OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with OMElement

use of org.apache.axiom.om.OMElement in project webservices-axiom by apache.

the class MTOMStAXSOAPModelBuilderTest method testCreateOMElement.

public void testCreateOMElement() throws Exception {
    OMElement root = createTestMTOMMessage();
    OMElement body = (OMElement) root.getFirstOMChild();
    OMElement data = (OMElement) body.getFirstOMChild();
    Iterator childIt = data.getChildren();
    OMElement child = (OMElement) childIt.next();
    OMText blob = (OMText) child.getFirstOMChild();
    /*
         * Following is the procedure the user has to follow to read objects in
         * OBBlob User has to know the object type & whether it is serializable.
         * If it is not he has to use a Custom Defined DataSource to get the
         * Object.
         */
    byte[] expectedObject = new byte[] { 13, 56, 65, 32, 12, 12, 7, -3, -2, -1, 98 };
    DataHandler actualDH;
    actualDH = blob.getDataHandler();
//ByteArrayInputStream object = (ByteArrayInputStream) actualDH
//.getContent();
//byte[] actualObject= null;
//  object.read(actualObject,0,10);
//  assertEquals("Object check", expectedObject[5],actualObject[5] );
}
Also used : Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler)

Example 10 with OMElement

use of org.apache.axiom.om.OMElement in project webservices-axiom by apache.

the class ElementSerializerTest method testDualNamespaces1.

public void testDualNamespaces1() throws Exception {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns1 = factory.createOMNamespace("bar", "x");
    OMNamespace ns2 = factory.createOMNamespace("bar", "y");
    OMElement root = factory.createOMElement("root", ns1);
    OMElement elt11 = factory.createOMElement("foo1", ns1);
    OMElement elt12 = factory.createOMElement("foo2", ns1);
    OMElement elt21 = factory.createOMElement("yuck", ns2);
    OMElement elt22 = factory.createOMElement("yuck", ns2);
    elt11.addChild(elt21);
    elt12.addChild(elt22);
    root.addChild(elt11);
    root.addChild(elt12);
    root.serialize(writer);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement)

Aggregations

OMElement (org.apache.axiom.om.OMElement)379 OMFactory (org.apache.axiom.om.OMFactory)189 OMNamespace (org.apache.axiom.om.OMNamespace)101 QName (javax.xml.namespace.QName)90 StringReader (java.io.StringReader)63 OMNode (org.apache.axiom.om.OMNode)42 OMText (org.apache.axiom.om.OMText)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)37 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)28 OMAttribute (org.apache.axiom.om.OMAttribute)26 StringWriter (java.io.StringWriter)23 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)21 OMDocument (org.apache.axiom.om.OMDocument)19 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)18 Element (org.w3c.dom.Element)18 DataHandler (javax.activation.DataHandler)17 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)16 InputStream (java.io.InputStream)15 OMException (org.apache.axiom.om.OMException)13 SOAPBody (org.apache.axiom.soap.SOAPBody)13