Skip to Content
avatar image
Former Member

How to Convert Decoded format in sap pi

Hi,

I am doing 'Soap to Soap ' synchronous scenario.The file that we are sending needs to be in Base64 Encoded format and the response would be in encoded format which we have to decode in order to do the mapping in PI.

we are using PI 7.4 Dual stack.

I have already gone through the below blog but that is for Asynchronous scenario and not very explanatory.

  Base64DecodeConverter - Base64 decoding made easy!

Could you please suggest how to achieve the above requirement.

It is an urgent requirement and would very much appreciate your help.

Thanks & Regards,

Srinivas

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Jun 22, 2016 at 11:11 AM

    Hi,

    I had also to decode a Base64 PDF file and searched for a solution. Some solutions using non-standard Java libaries (e.g. from sun) or the latest Java version that have a encoder/decoder inclusive. Unfortunately our PI didn't use the latest Java version.

    So I finally used javax.xml.bind.DatatypeConverter

    I could use it without additional deployment of libaries and it was simple.


    ...

    byte[] decoded = null;

    decoded = DatatypeConverter.parseBase64Binary(codedText);

    ...


    After decoding, I added the PDF file as attachment.



    Regards

    Chris


    Add comment
    10|10000 characters needed characters exceeded

  • Jun 21, 2016 at 05:55 PM

    Hello Srinivas,

    Check the below discussion and see if it helps

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 21, 2016 at 06:00 PM

    Hi,

    import the com.sap.aii.utilicore jar and used in UDF.

    Code of encode you will get in Sritharan link.

    Thanks,

    Sreenivasa

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 22, 2016 at 01:36 AM

    Hi Srinivas,

    If your full payload is coming back as an encoded string, I would suggest to use a Java mapping and perform the decoding in it. Output of Java Mapping will be a well formed XML, so you can either:

    1. Use another message mapping to perform conversion to target format.
    2. Or parse the XML in Java mapping itself to convert it to target format.

    Blog that you have mentioned uses a bean to perform decoding.

    Regards,

    Sanjeev

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Srinivas,

      Here is the code that I had used:

      package somepackage;

      import java.io.BufferedReader;

      import java.io.File;

      import java.io.FileInputStream;

      import java.io.FileOutputStream;

      import java.io.InputStream;

      import java.io.InputStreamReader;

      import java.io.OutputStream;

      import com.oschrenk.io.Base64;

      import com.sap.aii.mapping.api.AbstractTransformation;

      import com.sap.aii.mapping.api.StreamTransformationException;

      import com.sap.aii.mapping.api.TransformationInput;

      import com.sap.aii.mapping.api.TransformationOutput;

      public class ConvertBase64toXMl extends AbstractTransformation {

        public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {

          // TODO Auto-generated method stub

          this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());

        }

        public void execute(InputStream arg0, OutputStream arg1) throws StreamTransformationException { 

          try {

            //String output = "";

            String fileContent  = "";

            BufferedReader reader = new BufferedReader(new InputStreamReader(arg0));

            StringBuilder out = new StringBuilder();

            String line = reader.readLine();

            while(line != null)

            {

              out.append(line + "\n");

              line = reader.readLine();

            }

            fileContent = out.toString();

            byte[] decoded = Base64.decode(fileContent);

            String result = new String(decoded, "UTF-8");

            arg1.write(result.getBytes());

          }

          catch(Exception e){

            throw new StreamTransformationException(e.toString());

             }

        }

        /**

         * @param args

         */

        public static void main(String[] args) {

          // TODO Auto-generated method stub

          try {

            InputStream in = new FileInputStream(new File("C:\\Desktop\\in_Base64Converter.txt"));

            OutputStream out = new FileOutputStream(new File("C:\\Desktop\\out_XMLOutput_Base64Converter.xml"));

            ConvertBase64toXMl myMapping = new ConvertBase64toXMl();

            myMapping.execute(in , out);

          } catch (Exception e) {

            e.printStackTrace();

          }

        }

      }

      Please note that this will just convert the data to XML format. Any further processing will need to be done using further java code or message mapping as I had mentioned earlier.

      Also, note that I had imported this jar: java-util-1.3.1.jar from here

      Regards,

      Sanjeev.

      Message was edited by: Sanjeev Shekhar Singh