use of org.apache.axiom.mime.PartDataHandler 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.mime.PartDataHandler 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();
}
}
Aggregations