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)
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.
Thats it.
References :
[1] - http://wso2.com/library/articles/binary-relay-efficient-way-pass-both-xml-non-xml-content-through-apache-synapse/
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/
Hi Ruwan,
ReplyDeleteThanks for nice post. I have tried the same by following this, that works fine. But i have doubts. what i need to do if i get message like "expires" value too old while convert imageurl to base64?? Kindly let me know