Monday, November 25, 2013

WSO2 ESB append full body to a POST parameter

When you have to invoke a back end service which can take only on parameter and you need to send your full soap envelop body as single post parameter you can use script mediator and payload factory mediator as following. I have an api which calls to "http://localhost:9000/services/SimpleStockQuoteService"  and response is received by the "second_sequence".
In the second_sequence response is processed and assign it single parameter using script mediator and payload factory.

API


<api xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteAPI2" context="/stockquote_api">
   <resource methods="GET" uri-template="/view/{symbol}">
      <inSequence>
         <payloadFactory>
            <format>
               <m0:getQuote xmlns:m0="http://services.samples">
                  <m0:request>
                     <m0:symbol>$1</m0:symbol>
                  </m0:request>
               </m0:getQuote>
            </format>
            <args>
               <arg expression="get-property('uri.var.symbol')"/>
            </args>
         </payloadFactory>
         <send receive="second_sequence">
            <endpoint>
               <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </resource>
</api>
                     

SEQUENCE



<sequence xmlns="http://ws.apache.org/ns/synapse" name="second_sequence">
   <property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"/>
   <script language="js">
      <![CDATA[
                     var payload = mc.getEnvelope().getBody().getFirstElement().toString();
                     payload = encodeURI(payload);        
                     mc.setProperty("test",payload);
        ]]>
    </script>
      <payloadFactory>
         <format>
            <getSimpleQuote>
               <symbol>$1</symbol>
            </getSimpleQuote>
         </format>
         <args>
            <arg xmlns:ns="http://org.apache.synapse/xsd" expression="$ctx:test"/>
         </args>
      </payloadFactory>
      <log level="custom">
         <property xmlns:ns="http://org.apache.synapse/xsd" name="xxxxxxx....................." expression="$ctx:test"/>
      </log>
      <log level="full"/>
      <send>
         <endpoint>
            <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="get"/>
         </endpoint>
      </send>
      <log level="full" category="DEBUG"/>
   </sequence>


You will find that in the sequence it sends the request as follows

GET /services/SimpleStockQuoteService/getSimpleQuote?symbol=%253cns:getQuoteResponse%2520xmlns:ns=%2522http://services.samples%2522%253e%253cns:return%2520xmlns:ax21=%2522http://services.samples/xsd%2522%2520xmlns:xsi=%2522http://www.w3.org/2001/XMLSchema-instance%2522%2520xsi:type=%2522ax21:GetQuoteResponse%2522%253e%253cax21:change%253e-2.878332384294565%253c/ax21:change%253e%253cax21:earnings%253e13.343243692880526%253c/ax21:earnings%253e%253cax21:high%253e83.90879243287203%253c/ax21:high%253e%253cax21:last%253e80.71927384911324%253c/ax21:last%253e%253cax21:lastTradeTimestamp%253eMon%2520Nov%252025%252012:10:46%2520PST%25202013%253c/ax21:lastTradeTimestamp%253e%253cax21:low%253e84.13935291865967%253c/ax21:low%253e%253cax21:marketCap%253e4.856632338341972E7%253c/ax21:marketCap%253e%253cax21:name%253eibm%2520Company%253c/ax21:name%253e%253cax21:open%253e84.41127937872128%253c/ax21:open%253e%253cax21:peRatio%253e-17.33795236794525%253c/ax21:peRatio%253e%253cax21:percentageChange%253e3.7341845696623412%253c/ax21:percentageChange%253e%253cax21:prevClose%253e-77.08061373503116%253c/ax21:prevClose%253e%253cax21:symbol%253eibm%253c/ax21:symbol%253e%253cax21:volume%253e9235%253c/ax21:volume%253e%253c/ns:return%253e%253c/ns:getQuoteResponse%253e






Monday, November 11, 2013

SSL handshake failed: SSL error: Key usage violation in certificate has been detected

SVN causing problems in ubuntu

When I was checking out a code in a svn repository which can only connect through https, started giving me errors as following.

SSL handshake failed: SSL error: Key usage violation in certificate has been detected.

This is caused by a bug in the version of libneon, fortunately this is fixed in version 0.29.3.  

To fix the problem, please follow the steps


  1. Uninstall the current libneon package: sudo apt-get remove libneon27
  2. Download the latest libneon package http://packages.debian.org/squeeze/libneon27
  3. Install the required libssl dependency: sudo apt-get install libssl0.9.8
  4. Install the downloaded libneon package: dpkg -i libneon27_0.29.3-3_amd64.deb
  5. Make the changes to symbolic links as described above: sudo mv /usr/lib/libneon-gnutls.so.27 /usr/lib/libneon-gnutls.so.27.old sudo ln -s /usr/lib/libneon.so.27 /usr/lib/libneon-gnutls.so.27

Monday, November 4, 2013

WSO2 ESB Stop suffix getting appended to endpoint.

When you are developing a synapse configurations, there can be some places where you do a POST request to rest end point from a proxy service. And you find that your end is get appended by some other part of the previous url end point which supposed to call the current proxy service.

You can get rid of it with this property mediator.

<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/> 

WSO2 ESB Script Mediator (Remove body of an soap envelop)

When you are implementing synapse configurations in WSO2 ESB (4.6.0), sometimes you might want to remove soap body. This happens to me when I was developing complex synapse configuration, which had lot of service chaining. The last of the service chaining was to call to a rest (GET) end point using rest api. Up to final call to rest api, everything seems to work perfect. But the last rest api call fails due to a soap envelop generated from service chaining. Simple solution was to remove the soap body from the envelop. then it worked perfectly as I expected.

This is the simple solution I used to remove the body from the soap envelop using script mediator.

<script language="js">
mc.getEnvelope().getBody().getFirstElement().detach();
print(mc.getEnvelope().getBody());
</script>


WSO2 ESB Dynamic End Point Resolution

From WSO2 ESB 4.7.0 onwards, support dynamic end point resolutions.

If you have multiple deployment environments (DEV, QA, Production), then you may end up having many endpoints and will have to change them once you migrate your synapse configs to other environment. As a simple solution for this time consuming problem (for Call out mediator and send mediator only), you can initialize end points as jvm parameters at the server start up.

Ex:

  1. SEND Mediator

    <send>
                <endpoint name="MyEp"
                          template="endPointTemplete"
                          uri="http://{system.prop.bpm.stock.host}:{system.prop.bpm.stock.port}/services/SimpleStockQuoteService"/>
             </send>

    You have to create a endpoint template in order to use this feature for send mediator. When you start the ESB, you can initialize the {system.prop.bpm.stock.host} and {system.prop.bpm.stock.port} by using JVM parameters as follows. And then those end points will be initialized correctly.

    -Dbpm.stock.host=.............    - Dbpm.stock.port=..................

  2. CALLOUT Mediator
Callout mediator will be same as send mediator. you can initialize the parameters with jvm parameters

   <callout serviceURL="http://{system.prop.bpm.stock.host}:{system.prop.bpm.stock.port}/services/SimpleStockQuoteService"
                  action="urn:getQuote">
            <source xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
                    xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"
                    xpath="s11:Body/child::*[fn:position()=1] | s12:Body/child::*[fn:position()=1]"/>
            <target xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
                    xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"
                    xpath="s11:Body/child::*[fn:position()=1] | s12:Body/child::*[fn:position()=1]"/>
         </callout>