Announcement

Collapse

Mirth Connect 3.12.0 Released!

Mirth Connect 3.12.0 is now available as an appliance update and on our GitHub page. This release includes database performance improvements, improves visual HL7 representation, message pruning, keystore handling, PDF generation, community contributions, and fixes several security vulnerabilities. This release also contains many improvements to commercial extensions. See the release notes for the list of fixes and updates.

Download | See What's New | Upgrade Guide | Release Notes

For discussion on this release, see this thread.
See more
See less

Decoding a base64 response from soap request

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Decoding a base64 response from soap request

    Hi. I'm receving the following response from a soap request defined in a 'Soap Sender' destination connector called 'Emdeo ITS - TEST':

    ------------
    <?xml version="1.0" encoding="UTF-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
    <SendRequestResponse xmlns="https://ITSWebService.webmd.com/">
    <SendRequestResult>
    <ErrorCode>0</ErrorCode> <Response>SVNBKjAwKiAgICAgICAgICAqMDAqICAgICAgICAg ICpaWipFTlZPWSAgICAgICAgICAqICAqICAgICAgICAgICAgIC AgKjA4MDgyMCoxNDE1KlUqMDA0MDEqMTY5MjIyOTA5KjAqVCo6 flRBMSoxNjkyMjI5MDkqMDgwODIwKjE0MTUqUiowMTF+SUVBKj EqMTY5MjIyOTA5fg==</Response>
    </SendRequestResult>
    </SendRequestResponse>
    </soap:Body>
    </soap:Envelope>
    ------------

    Trying to decode this response in the transformer of a subsequent 'File Writer' destination connector using the following transformer javascript. (Thanks to all the forum posts regarding base64 encode/decode that helped me write this script):

    ------------
    //retrieve response node from SOAP envelope
    var responseVal = responseMap.get('Emdeon ITS - TEST').getMessage();
    msg = new XML(responseVal);
    var base64response = msg..*::Response.toString();
    channelMap.put('base64response',base64response);

    //decode the response
    var x12response1 = FileUtil.decode(base64response);
    channelMap.put('x12response1',x12response1);
    ------------

    However, this is not working as anticipated. I know the base64 can be decoded as I have been successful decoding on online encode/decode forms, so I must be doing something wrong with the decode method or variable assignment.

    Can anyone shed any light on where my javascript is going wrong?

    Thanks!

    Jonah Piascik

  • #2
    Reecoding a base64 response from soap request

    This is the underlying code that you are calling, which looks fine. Although I have no idea what your problem is because you didnt post it, I think your problem may have to do with the fact that you are putting bytes in a map which, when accessed from that map later on, gets automatically converted to a string. I had that problem when putting the boolean true in the map in one destination then when I used it in a subsequent destination, I had to check if channelMap.get('var') == 'true'

    public static byte[] decode(String data) throws IOException {
    return new BASE64Decoder().decodeBuffer(data);
    }
    Brendan Haverlock | Mirth Software Engineer | Mirth Corporation

    Comment


    • #3
      Reecoding a base64 response from soap request

      Brendan,

      Thanks for the reply. I appreciate it.

      My problem is that I can't seem to get the base64 encoded string to convert properly with the decode method.

      I was able to work around this by creating my own 'decode' function in another transformer script (copied from an o'reilly book I had), and then calling that function from my orginal script below).

      The 'base64response' variable has the base64 encoded string as it's value, so I know the problem is how I'm calling FileUtil.decode or how I'm assigning the result to the 'x12response' variable.

      I tried coverting the byte value to a string using this code:

      //decode the response
      x12response1 = new String(FileUtil.decode(base64response));
      channelMap.put('x12response1',x12response1);

      but I get the following return value:

      [[email protected]

      Any other ideas?

      Thanks again

      Comment


      • #4
        Reecoding a base64 response from soap request

        I've faced a very similar problem trying to send a PDF over a SOAP connection.

        I used the Fileutil.encode/decode functions in the preprocessor to get the PDF encoded. And it seems that these functions are not accesible from the pre/post processor scripts.

        Watching how these funcions are implemented, I used this
        Code:
        FileUtil.encode(Packages.java.lang.String(message).getBytes())
        As the encode funcion needs to receive the data as a byte array, not a string. So I construct a Java String to use the .getBytes() method.

        When I used the .decode equivalent in the receiving channel, most of the PDF file is OK, but some bytes appeared with other value: i.e. 31h -> 8Fh, 3Fh -> 8Fh, and the PDF cannot be opened.

        I remember that browsing the code of the FileUtil.encode/decode, is internally using the sun.misc.* packages, that happens to be discouraged by Sun.

        During my tests I saw the same behaviour as you about the decode().

        You can try the getBytes() from the Java String class as parameter for decode, otherwise it seems that is calling the JS toString() method, that doesn't know how to handle binary data.

        Hope that helps!

        Comment


        • #5
          Reecoding a base64 response from soap request

          [duplicate post]

          Post edited by: quimicefa, at: 10/03/2008 09:03

          Comment


          • #6
            re: Decoding a base64 response from soap request

            I wrote the following code to get the proper decoded "Hello". The solution is to cast the data returned from decode to a string variable and then return that.

            Preprocessor: (we send a message of just Hello as a test.
            var encoded = FileUtil.encode(Packages.java.lang.String(message) .getBytes());
            //var encoded = FileUtil.encode(message);

            logger.error ("encoded: " + encoded); //logs: SGVsbG8=

            var decoded = FileUtil.decode(encoded);
            //var decoded = FileUtil.decode(Packages.java.lang.String(encoded) .getBytes());

            var decodedAsString = Packages.java.lang.String(decoded);

            logger.error ("decodedAsString: " + decodedAsString); //logs Hello
            logger.error ("decoded: " + decoded); //logs [[email protected]

            return decodedAsString;

            Comment


            • #7
              Any working code for this. I am getting json response in base64 format.

              Comment


              • #8
                Decoding Base64

                You can use the following code to decode from base64 to plain text.

                This worked for me.


                var decoded = FileUtil.decode(msg);
                var decodedAsString = Packages.java.lang.String(decoded);
                msg = decodedAsString;

                Comment


                • #9
                  Encoding BASE64 after a SHA256 hash using JAVA

                  I had terrible trouble getting this working so am posting for others.
                  avalue is a variable within the Channel.

                  //tried all text versions and no worked, so this was the final outcome
                  var textBytes = $('avalue').getBytes();

                  var hash256 = com.google.common.hash.Hashing.sha256().hashBytes( textBytes);
                  channelMap.put('hash256var', hash256.asBytes());

                  channelMap.put('avalueHASH',
                  com.google.common.io.BaseEncoding.base64().encode( hash256.asBytes()));

                  channelMap.put('base64Encoded',
                  com.google.common.io.BaseEncoding.base64().encode( hash256.asBytes()));

                  Runs with current version of MIRTH

                  Comment

                  Working...
                  X