How to Create a Response Message

This article describes how to create a virtual service process that creates a response message using input from the request message and returns this response to the consumer. This is useful for creating sandbox services. In this case I am adding a protocol header that is specific to SOAP, this header would only be returned via a SOAP binding, but the process generates a normalized XML message that can be returned via any of the bindings, including being mediated to JSON if that is required.

Note: this article doesn't cover configuring the bindings, all it does is tell you how to create the response message and wire that into your process.

Step 1 - Define a Variable

Use the A button on the Process Editor palette to define a new variable name of type message. To match the script below, we'll call this new message variable newMsg.

Note: all adding a variable here does is to make the variable name available to other activities in the Process Editor.

Step 2 - Set the Reply to use the new Variable

Edit the reply activity and pick your new message variable as the response message (the field labelled variable).

Step 3 - Add a Script Activity between the receive and the reply

Add a script activity between the receive and the reply, and use the script below. This script performs a few operations:

  • Creates a new normalized message.
  • Normalizes the input request and converts the resulting normalized message to a JavaScript object.
  • Sets the content of the new normalized message as a string built using an element taken from the JavaScript object (in this case we know that the input request contains an element called cin).
  • Adds a new protocol (binding) header to the new message—this is optional, and is here simply to show how this works between SOAP and REST bindings (it's ignored by the REST binding).
// Create a new normalized message and add it as a variable to the process context
var newMsg = msgFactory.createNormalized();

// Get the input message and normalize it
var nm = processContext.getVariable("message").normalize();

// Convert the normalized input message to JSON and parse it to a JavaScript Object
converter = com.soa.json.xml.script.XML2JSON.converter();
var jsonString = converter.convert(nm.getContentAsString());
var jsObj = JSON.parse(jsonString,null);

// Set the content of our new normalized message to the body content of the soap response
// NOTE: this is the bit that will be inside the <soap:body> tag, it does not include that tag
newMsg.setStringContent("<brwr:cin xmlns:brwr=\"\">" + jsObj.cin + "</brwr:cin>")

// Add a protocol header to the new normalized message. The outbound binding will add this to the SOAP envelope when it sends it
newMsg.setProtocolHeader("{}DemoHead", "<demo:Demohead xmlns:demo=\"\"/>");

Step 4 - Preserve Binding Headers - Optional

Edit the operation, select Header Propagation, and set Preserved Binding Headers to all, or add the specific header you want to pass through the downstream binding in this list.

This is only needed if you want to pass specific headers back to the consumer. For REST consumers you would typically enable Preserve Transport Headers and would use the setTransportHeaders() function, as shown below.

headers = processContext.getVariable("newMsg").getTransportHeaders();
headers.add("HeaderName", "headerValue");