Search in sources :

Example 1 with OMOutputFormat

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

the class OMOutputTest method testComplete.

public void testComplete() throws Exception {
    OMOutputFormat mtomOutputFormat = new OMOutputFormat();
    mtomOutputFormat.setDoOptimize(true);
    OMOutputFormat baseOutputFormat = new OMOutputFormat();
    baseOutputFormat.setDoOptimize(false);
    envelope.serializeAndConsume(new NullOutputStream(), baseOutputFormat);
    envelope.serializeAndConsume(new NullOutputStream(), mtomOutputFormat);
}
Also used : OMOutputFormat(org.apache.axiom.om.OMOutputFormat) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 2 with OMOutputFormat

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

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

the class MTOMStAXSOAPModelBuilderTest method checkSerialization.

private void checkSerialization(OMElement root, boolean optimize) throws Exception {
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(optimize);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    root.serialize(baos, format);
    String msg = baos.toString();
    if (optimize) {
        // Make sure there is an xop:Include element and an optimized attachment
        assertTrue(msg.indexOf("xop:Include") > 0);
        assertTrue(msg.indexOf("Content-ID: <-1609420109260943731>") > 0);
    } else {
        assertTrue(msg.indexOf("xop:Include") < 0);
        assertTrue(msg.indexOf("Content-ID: <-1609420109260943731>") < 0);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 4 with OMOutputFormat

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

the class CharacterEncoding2Test method testISO99591.

public void testISO99591() throws Exception {
    ByteArrayInputStream byteInStr = new ByteArrayInputStream(xml.getBytes("iso-8859-1"));
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(byteInStr, null);
    SOAPEnvelope envelope = builder.getSOAPEnvelope();
    envelope.build();
    assertEquals("iso-8859-1", builder.getDocument().getXMLStreamReader().getCharacterEncodingScheme());
    ByteArrayOutputStream byteOutStr = new ByteArrayOutputStream();
    OMOutputFormat outputFormat = new OMOutputFormat();
    outputFormat.setCharSetEncoding("iso-8859-1");
    envelope.serialize(byteOutStr, outputFormat);
    assertAbout(xml()).that(new InputStreamReader(new ByteArrayInputStream(byteOutStr.toByteArray()), "iso-8859-1")).hasSameContentAs(new InputStreamReader(new ByteArrayInputStream(xml.getBytes("iso-8859-1")), "iso-8859-1"));
    builder.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 5 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat 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)

Aggregations

OMOutputFormat (org.apache.axiom.om.OMOutputFormat)64 MessageFormatter (org.apache.axis2.transport.MessageFormatter)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 OutputStream (java.io.OutputStream)18 IOException (java.io.IOException)16 AxisFault (org.apache.axis2.AxisFault)13 MessageContext (org.apache.axis2.context.MessageContext)12 OMElement (org.apache.axiom.om.OMElement)11 DataHandler (javax.activation.DataHandler)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 StringWriter (java.io.StringWriter)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 OMText (org.apache.axiom.om.OMText)5 Map (java.util.Map)4 MultipartBody (org.apache.axiom.mime.MultipartBody)4 SOAPFactory (org.apache.axiom.soap.SOAPFactory)4 Collection (java.util.Collection)3