This example assumes you are using an HTTP Listener source node and the web service being called is SOAP based. It uses MTOM (Message Transmission Optimization Mechanism) to send the response as a seperate part rather than embed it in the SOAP XML. A REST webservice may do something similar using multipart messages without the need for MTOM (Message Transmission Optimization Mechanism).
Let's start with this function:
function checkForMTOM(response) {
// Convert to MTOM response if the request indicates that the server might expect an MTOM response.
if (source.getNode('/Request/Part/content-type').contains('xop+xml')) {
// Create a unique boundary string. This string separates the different parts of the MTOM message.
var boundary = qie.getUUID(true);
// Parse the response argument into an XML message type to cache the original and so we can use node paths to access its parts below
var responseM = qie.parseXMLString(response);
// Start a new response and wrap the original response(the SOAP XML envelope) with an MTOM response with one part.
response = 'Content-Type: multipart/related;' +
'type="application/xop+xml";' +
'boundary="' + boundary + '";' + // Identifies what boundary string will be used to separate the parts
'start="";' +
'start-info="application/soap+xml";' +
'action="' + responseM.getNode('/Envelope/Header/Action') + '"\r\n' + // The carage return and new lines used in this code are required
'\r\n' + // This blank line is also required
'--' + boundary + '\r\n' + // Each part starts with the double dashes and the boundary string
'Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"\r\n' +
'Content-Transfer-Encoding: binary\r\n' +
'Content-ID: \r\n' +
'\r\n' +
response + '\r\n' +
'--' + boundary + '--\r\n'; // Close the MTOM message with the double dashes, the boundary string, followed by ending double dashes.
}
return response;
}
This function will check the request to see if it expects an MTOM message. This check may not be needed for the web services and servers in your case. The body of the if statement demonstrates creating an MTOM message with a single part containing the SOAP XML reponse example shown below. See the comments in the above code for additional details.
Now let's see how to use the above function and then how to post the response using qie.postMessageResponse().
var response = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n" +
"<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">\r\n" +
" <soapenv:Header>\r\n" +
" <wsa:Action>urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-bResponse</wsa:Action>\r\n" +
" <wsa:RelatesTo>" + qie.escapeXml(message.getNode('/Envelope/Header/MessageID')) + "</wsa:RelatesTo>\r\n" +
" <direct:addressBlock xmlns:direct=\"urn:direct:addressing\" soapenv:relay=\"true\" soapenv:role=\"urn:direct:addressing:destination\">\r\n" +
" <direct:from>" + message.getNode('/Envelope/Header/addressBlock/from') + "</direct:from>\r\n" +
" <direct:to>" + message.getNode('/Envelope/Header/addressBlock/to') + "</direct:to>\r\n" +
" <direct:metadata-level>minimal</direct:metadata-level>\r\n" +
" </direct:addressBlock>\r\n" +
" </soapenv:Header>\r\n" +
" <soapenv:Body>\r\n" +
" <rs:RegistryResponse xmlns:rs=\"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0\" status=\"urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success\"/>\r\n" +
" </soapenv:Body>\r\n" +
"</soapenv:Envelope>";
response = checkForMTOM(response);
qie.postMessageResponse(response.toString());
In the above we see three things happening.
1. Declaration of the response variable which holds the SOAP envelope to be retuned to the requesting server.
2. A call to the checkForMTOM function and passing in the response variable as a parameter. The returned value from the checkForMTOM function will be reassigned to the response variable. The returned value will then be either the original SOAP envelope or it will be an MTOM message that wraps the SOAP envelope.
3. The final response to be posted is then done by passing the response variable to qie.postMessageResponse() as a string.
See also:
Handling HTTP requests and responses
Posting Responses via qie.postMessageResponse() in a channel that uses an HTTP Listener source node