Thursday, February 6, 2014

Mediators : How to use Enrich Mediator to download an image from a URL and use Synapse functions to base64 encode same using WSO2 ESB

We can do this by doing a "GET" on the image URL, this will make the ESB download the contents, after which you may use the Enrich mediator to assign the "body" contents, to a synapse variable.



 
  
 


 
 


 


That should do it. We are not done however, when you run this config you will notice a huge error on the ESB console, like below.

ERROR - DeferredMessageBuilder Error building message using POX Builder
javax.xml.stream.XMLStreamException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

This is because ESB is trying to build the image as a XML, since we have not assigned a specific builder to tackle with the its type, which is : image/jpg. We can make this config change in the axis2.xml. (Please refer [1] for further details on this)

<messageBuilder contentType="image/jpeg" class="org.wso2.carbon.relay.BinaryRelayBuilder"/>

<messageFormatter contentType="image/jpeg" class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>

You need to add the formatter under the <messageFormatters> section and builder under the <messageBuilders>.

Ok. We are good to go. Now all that remains to be done is the base64 encoding part. We can simply do this by calling the synapse function base64Encode

The following is an example on how it can be done.

<property name="encoded_image" expression="base64Encode($ctx:image_val)" scope="default" type="STRING" description="base64 encode Image"/>

Thats it.


References :

[1] - http://wso2.com/library/articles/binary-relay-efficient-way-pass-both-xml-non-xml-content-through-apache-synapse/

Wednesday, February 5, 2014

Mediators : How to use WSO2 Script Mediator to do date validations.

The WSO2 ESB provides a whole suite of mediators with which you can do all sorts of request/response/variable manipulations and validations.

This article will be the first of a series of posts to follow on various mediators.

For the sake of this article, we will be using javascript inside the script mediator to do a simple XML DATE validation.



The above code would convert a XML DATE which is of the following format "YYYY-MM-DDTHH:MM:SS" (E.g. : 2010-08-01T00:00:00) to a regular Javascript date, add 5 years to the same and see if current date is greater than said value. Accordingly it would assign a variable to synapse messagecontext, which you may access later as you would any synapse property. (Please refer [1] for additional methods, you may call on the Synapse MessageContext from the script mediator.)

You may use a filter/switch mediator later on in your flow to execute appropriate logic based on the "result" variable you assigned from inside the script.

The script also has logic on how we can do xpath extractions from javascript itself.

var endDay = mc.getPayloadXML()..*::Date.toString(); 

This will seek out an xml elements like <Date>2010-08-01T00:00:00</Date> or JSON elements like
{"Date" : "2010-08-01T00:00:00"}. The principal to remember here is that WSO2 ESB converts JSON requests/responses to XML format in it the run time. So with the above line of code, you may access both XML and JSON payloads, like you would any XPATH.

Note that you may either embed the script inline (like above) or either store it in the registry as a file and point like

<script language="js" key="gov:trunk/scripts/test.js" function="validateDate"/>

References :