Wednesday, October 15, 2008

Apache Synapse/WSO2 ESB calling a REST Endpoint

One of the features of Apache Synapse and the WSO2 ESB (Which is Apache Synapse + Monitoring + Management + WSO2 Registry Integration) is that it can change message formats.

There was a question on the ESB-USER mailing list recently asking how you could receive a SOAP message and send out a REST message (HTTP POST serialized as application/x-www-form-urlencoded) to another service. In order to do this you simply need to set two properties on the message. The HTTP_Method to be used and the messageType to be used. The following is a complete synapse.xml
  1. <syn:definitions xmlns:syn="<a href="http://ws.apache.org/ns/synapse">http://ws.apache.org/ns/synapse</a>">  
  2.   
  3.     <syn:registry provider="org.wso2.esb.registry.ESBRegistry">  
  4.   
  5.         <syn:parameter name="root">file:registry/</syn:parameter>  
  6.   
  7.     </syn:registry>  
  8.   
  9.     <syn:proxy name="RESTProxy" startOnLoad="true">  
  10.   
  11.         <syn:target>  
  12.   
  13.             <syn:endpoint name="RESTEndpoint">  
  14.   
  15.                 <syn:address uri="<a href="http://localhost:7763/services/keith/test/foo">http://localhost:7763/services/keith/test/foo</a>"/>  
  16.   
  17.             </syn:endpoint>  
  18.   
  19.             <syn:inSequence>  
  20.   
  21.                 <syn:property name="messageType" value="application/x-www-form-urlencoded" scope="axis2"/>  
  22.   
  23.                 <syn:property name="HTTP_METHOD" value="post" scope="axis2"/>  
  24.   
  25.             </syn:inSequence>  
  26.   
  27.         </syn:target>  
  28.   
  29.     </syn:proxy>     
  30.   
  31.     <syn:sequence name="main">  
  32.   
  33.         <syn:send/>  
  34.   
  35.     </syn:sequence>  
  36.   
  37.     <syn:sequence name="fault">  
  38.   
  39.         <syn:log/>  
  40.   
  41.     </syn:sequence>  
  42.   
  43. </syn:definitions>  


Here is the request sent into the ESB:
  1. POST /soap/RESTProxy/mediate HTTP/1.1  
  2.   
  3. Content-Type: application/soap+xml; charset=UTF-8; action="urn:anonOutInOp"  
  4.   
  5. User-Agent: WSO2 Mashup Server - Version 1.5.1  
  6.   
  7. Host: 127.0.0.1  
  8.   
  9. Content-Length: 191  
  10.   
  11.   
  12.   
  13. <?xml version='1.0' encoding='UTF-8'?>  
  14.   
  15.    <soapenv:Envelope xmlns:soapenv="<a href="http://www.w3.org/2003/05/soap-envelope">http://www.w3.org/2003/05/soap-envelope</a>">  
  16.   
  17.       <soapenv:Body>  
  18.   
  19.          <foo>  
  20.   
  21.             <param>keith</param>  
  22.   
  23.          </foo>  
  24.   
  25.       </soapenv:Body>  
  26.   
  27.    </soapenv:Envelope>  

Here is its response (or Request sent out to the REST service):
  1. POST http://localhost:7763/services/keith/test/foo HTTP/1.1  
  2. Host: 127.0.0.1  
  3. Content-Type: application/x-www-form-urlencoded; charset=UTF-8;action="urn:anonOutInOp";  
  4. Transfer-Encoding: chunked  
  5. Connection: Keep-Alive  
  6. User-Agent: Synapse-HttpComponents-NIO  
  7.   
  8. b  
  9. param=keith  
  10. 0  

1 comment:

Unknown said...

Hi Keith,

What would I need to do if I want to receive POST from a form and send POST to the endpoint?

Thankyou