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);
}
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();
}
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);
}
}
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();
}
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();
}
Aggregations