cancel
Showing results for 
Search instead for 
Did you mean: 

How to Convert Decoded format in sap pi

Former Member
0 Kudos

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.

 

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

Accepted Solutions (0)

Answers (4)

Answers (4)

former_member296836
Participant
0 Kudos

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


former_member194786
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Sanjeev,

Thanks for your suggestion.

Could you please provide Java Mapping .

Thanks,

Srinivas

former_member186851
Active Contributor
0 Kudos

Check the link  I shared Srinivas

former_member194786
Active Contributor
0 Kudos

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

former_member191435
Contributor
0 Kudos

Hi,

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

Code of encode you will get in Sritharan link.

Thanks,

Sreenivasa

former_member186851
Active Contributor
0 Kudos

Hello Srinivas,

Check the below discussion and see if it helps