This documentation is for WSO2 ESB version 4.5.1. View documentation for the latest release.

Generating a Simple Response

WSO2 Enterprise Service Bus has the concept of Proxy Services. Proxy Services are designed to be virtual services that expose an actual service to the outside world. Usually when a request comes to a Proxy Service, it forwards it to an actual service. Response from the actual service is sent to the client.

The above example is the normal behavior of Proxy Services. A Proxy Service is created using an XML configuration. This configuration determines where to send the message, what are the transformations that need to be done prior to sending the message etc. Also a Proxy Service configuration specifies what needs to be done to the actual response message before sending to the client. This XML language is called Apache Synapse language.

Message mediation is the other way WSO2 Enterprise Service Bus handles messages. In message mediation all the messages are sent to the main sequence. Usually messages are filtered and transformed in the main sequence before sending to the actual endpoint.

As with the Proxy Services, users can create a response and send it back from the main sequence.

Here is such a configuration.

<?xml version="1.0" encoding="UTF-8"?>
<syn:definitions xmlns:syn="http://ws.apache.org/ns/synapse">
	<syn:registry provider="org.wso2.carbon.mediation.registry.WSO2Registry">
		<syn:parameter name="cachableDuration">15000</syn:parameter>
	</syn:registry>
	<syn:proxy name="EchoProxy" transports="https http" startOnLoad="true" trace="disable">
		<syn:target inSequence="response"/>
	</syn:proxy>
	<syn:sequence name="main">
		<syn:sequence key="response"/>
	</syn:sequence>
	<syn:sequence name="fault">
		<syn:log/>
	</syn:sequence>
	<syn:sequence name="response">
		<syn:script language="js">
			mc.setPayloadXML(
			&lt;greeting&gt;Hello World&lt;/greeting&gt;
			);
		</syn:script>
		<syn:header name="To" action="remove"/>
		<syn:property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
		<syn:property name="RESPONSE" value="true"/>
		<syn:send/>
		<syn:log level="full"/>
	</syn:sequence>
</syn:definitions>

There are a Proxy Service and main sequence. Both Proxy Service and main sequence uses a sequence called response. This sequence creates a response and sends it back to the client.

1. Start WSO2 Enterprise Service Bus with this configuration. You can copy the above configuration into synapse.xml which is found in the conf directory and start WSO2 Enterprise Service Bus with --DuseSynapseXML parameter.

2. Then send a GET or POST request to the Proxy Service or main sequence.

For invoking the proxy service

http://localhost:8280/services/EchoProxy

For invoking the main sequence, you need to use a URL which is not a Proxy Service.

http://localhost:8280/services/Response

3. Both Proxy Service and the main sequence executes the response sequence when a message comes to them. The response sequence is in the request path. So we are transforming the request message to a response message.

4. Response sequence uses the Script Mediator to set a custom payload to the message. Then it removes the To header. Since we are sending the message back, To header is not required and not expected by WSO2 Enterprise Service Bus.

Note

All these changes are done to the incoming request message.

5. Now it removes another property called NO_ENTITY_BODY. If this property is present and if its value is true WSO2 Enterprise Service Bus will not send the response body. Instead it will just send the headers. In case of a GET request this property is set. Remove it, in order to send the response body.

6. The property RESPONSE is set. This property indicates that this is a response message and it needs to be sent back to the client. At this point we are virtually turning the request back to a response. If you use a Send Mediator without setting this property, you can see that our custom message is being sent to the service.

7. Finally we do a send. This send, sends back the created response message.

See the source in the WSO2 library.