Clarity

  • 1.  GEL script - string encode to base64

    Posted Jan 10, 2017 11:57 AM

    Hi all,

     

    I need to encode string to base64 to be able to call webservice. But I'm not able to find any possibility how to achieve this in GEL script.

     

    Have someone tried it? Or has a solution?

     

    Thanks in advance,

    Martin



  • 2.  Re: GEL script - string encode to base64
    Best Answer

    Posted Jan 10, 2017 02:25 PM

    How about this (with edits!).

     

     

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script xmlns:core="jelly:core"
         xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
         xmlns:sql="jelly:sql"
         xmlns:util="jelly:util"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


         <core:set var="stringToEncode" value="Encode This String" />

         <core:invokeStatic var="encoded7String" className="javax.xml.bind.DatatypeConverter" method="printBase64Binary" >
         <core:arg value='${stringToEncode.getBytes("UTF-8")}'/>
         </core:invokeStatic>
         
         <core:invokeStatic var="decoded7Bytes" className="javax.xml.bind.DatatypeConverter" method="parseBase64Binary" >
         <core:arg  value='${encoded7String}'/>
         </core:invokeStatic>
         
         <core:new className='java.lang.String' var='decoded7String'>
              <core:arg value='${decoded7Bytes}' />
         </core:new>
         
         <gel:log>Java 7 stringToEncode = ${stringToEncode} </gel:log>
         <gel:log>Java 7 encoded7String = ${encoded7String} </gel:log>
         <gel:log>Java 7 decoded7String = ${decoded7String} </gel:log>
         

         <core:invokeStatic var="encoder" className="java.util.Base64" method="getEncoder" />

         <core:invoke var="encoded8String" on="${encoder}" method="encodeToString" >
              <core:arg value='${stringToEncode.getBytes("UTF-8")}'/>
         </core:invoke>

         <core:invokeStatic var="decoder" className="java.util.Base64" method="getDecoder" />
         
         <core:invoke var="decoded8Bytes" on="${decoder}" method="decode" >
              <core:arg value='${encoded8String}'/>
         </core:invoke>
         
              <core:new className='java.lang.String' var='decoded8String'>
              <core:arg value='${decoded8Bytes}' />
         </core:new>
         
         <gel:log>Java 8 stringToEncode = ${stringToEncode} </gel:log>
         <gel:log>Java 8 encoded8String = ${encoded8String} </gel:log>
         <gel:log>Java 8 decoded8String = ${decoded8String} </gel:log>
         
    </gel:script>

     

     

     

    V/r,

    Gene



  • 3.  Re: GEL script - string encode to base64

    Posted Jan 11, 2017 12:52 PM

    Thank you Gene, it's fantastic.

     

    Martin



  • 4.  Re: GEL script - string encode to base64

    Posted Jan 11, 2017 05:42 PM

    I just bumped into this way which is a little shorter:

     

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script xmlns:core="jelly:core"
         xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


         <core:set var="stringToEncode" value="Encode This String" />
         
         <core:invokeStatic var="encodedNikuString" className="com.niku.union.utility.Base64" method="encode" >
              <core:arg value='${stringToEncode}'/>
         </core:invokeStatic>
         
         <core:invokeStatic var="decodedNikuString" className="com.niku.union.utility.Base64" method="decode" >
              <core:arg value='${encodedNikuString}'/>
         </core:invokeStatic>
         
         <gel:log>niku.union stringToEncode = ${stringToEncode} </gel:log>
         <gel:log>niku.union encoded7String = ${encodedNikuString} </gel:log>
         <gel:log>niku.union decoded7String = ${decodedNikuString} </gel:log>
         
    </gel:script>

     

     

    V/r,

    Gene