writing into file (Converting Base64 to Binary) values Using VFS and ESB 4.8.1

297 views Asked by At

From Ui base64 values I got for image files,my requirement is to convert that all to binary then write into the file.And if I open that file the image should open.

This code is for base64 to binary conversion using script and then write into the file:

<filter source="get-property('type')='Yes'" regex="true">
                 <then>
                    <property xmlns:ns2="http://org.apache.synapse/xsd"
                              name="transport.vfs.ReplyFileName"
                              expression="fn:concat(fn:substring-after(get-property('MessageID'), 'urn:uuid:'), '.jpeg')"
                              scope="transport"
                              type="STRING"/>
                    <property name="fieldValue"
                              expression="//FieldValue"
                              scope="default"
                              type="STRING"/>
                    <script language="js">var t = mc.getProperty("fieldValue");
                    var input= t;    


 var chr1, chr2, chr3 = "";
 var enc1, enc2, enc3, enc4 = "";
 var i = 0;
 var keyStr = "ABCDEFGHIJKLMNOP" +
           "QRSTUVWXYZabcdef" +
           "ghijklmnopqrstuv" +
           "wxyz0123456789+/" +
           "=";
           var output=t.length;
 for( i=0;i &lt; input.length;i++){
    enc1 = keyStr.indexOf(input.charAt(i++));
    enc2 = keyStr.indexOf(input.charAt(i++));
    enc3 = keyStr.indexOf(input.charAt(i++));
    enc4 = keyStr.indexOf(input.charAt(i++));
    chr1 = (enc1 &lt;&lt; 2) | (enc2 &gt;&gt; 4);
    chr2 = ((enc2 &amp; 15) &lt;&lt; 4) | (enc3 &gt;&gt; 2);
    chr3 = ((enc3 &amp; 3) &lt;&lt; 6) | enc4;    output = output + String.fromCharCode(chr1);

    if (enc3 != 64) {
       output = output + String.fromCharCode(chr2);
    }
    if (enc4 != 64) {
       output = output + String.fromCharCode(chr3);
    }

   chr1 = chr2 = chr3 = "";
   enc1 = enc2 = enc3 = enc4 = ""; 
   }
   mc.setProperty("result",output);</script><log level="custom">
                       <property name="Before Payload" expression="get-property('result')"/>
                    </log>
                    <payloadFactory media-type="xml">
                       <format>
                          <ns:text xmlns:ns="http://ws.apache.org/commons/ns/payload">$1</ns:text>
                       </format>
                       <args>
                          <arg evaluator="xml" expression="get-property('output')"/>
                       </args>
                    </payloadFactory>
                    <property name="OUT_ONLY" value="true"/>
                    <property name="messageType"
                              value="text/plain; charset=windows-1252"
                              scope="axis2"/>
                    <send>
                       <endpoint name="FileEpr">
                          <address uri="vfs:file://D:/Documents/File/out"/>
                       </endpoint>
                    </send>
                 </then>

There is also some issue with script file,length function its not taking,but in the script if I declare some static value for those its taking. So,anybody can please help me to resolve this problems 1.Is there any way to directly convert base64 to Binary?? 2.This script as a stand alone working fine.how could I make it work into code??

0

There are 0 answers