Many times we have requirements to decode the “base64 encoded message” in XI/PI so it can be used in IE for message processing. In this blog I tried to demostrate about the steps that will help us to decode the data using Java Mapping

Actual source XML

 

After Base64 Encoding: (incoming message to XI/PI)

 

 

!https://weblogs.sdn.sap.com/weblogs/images/251803663/P2.JPG|height=94|alt=Payload after Base64 Encoding|width=593|src=https://weblogs.sdn.sap.com/weblogs/images/251803663/P2.JPG|border=0

 

 

+Help: For prototype purpose use the URL https://ostermiller.org/calc/encode.html  (https://ostermiller.org/calc/encode.html) to encode your actual payload. +

 

 

There are two ways to handle such scenarios 

 

  • Java Mapping : Easy to design, develop and able to test using Java IDE externally. Also possible to test it in Interface Mapping without executing/testing the whole interface.

So each of the above has there own advantages and disadvantages. Here with the help of Java mapping we will look how it is possible to implement such scenario. In the next blog I will demostrate the same using Custom Adapter Module.

*Step 1: Convert the input.txt information into and XML format (XML template) using File Content Conversion in the Sender File Adapter.

node contains the actual encoded Information/XML payload.

!https://weblogs.sdn.sap.com/weblogs/images/251803663/P3.JPG|height=99|alt=Template XML generated by using FCC.|width=647|src=https://weblogs.sdn.sap.com/weblogs/images/251803663/P3.JPG|border=0!

Step 2: Java code for Base64 decoding, this is simply lines of Java language just to demostrate about Base64 decoding in XI. You can uses other API’s in order to achieve Base64 decoding/Encoding.

 

package domDemo;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import sun.misc.BASE64Decoder;
import com.sap.aii.mapping.api.StreamTransformation;

public class JMDecodeDetails implements StreamTransformation {
 private Map param=null;
 public void setParameter(Map param){
  this.param=param;
 }
 public void execute(InputStream in, OutputStream out){
  try{
      
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document docOld = builder.parse(in);
   //Get the node name Details that contains the Based64 Encoded information..
   NodeList details = docOld.getElementsByTagName(“Details”);
   //Get the Text of Node.
   String data = details.item(0).getChildNodes().item(0).getNodeValue();
   //Instantiate an object of BASE64Decoder…
   BASE64Decoder decoder =  new BASE64Decoder();
   //Decode the encoded information….
   byte b[] = decoder.decodeBuffer(data);
   //Parse the decoded data i.e. nothing but the real payload….
   Document docNew = builder.parse(new ByteArrayInputStream(b));
   
   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Transformer transformer =  transformerFactory.newTransformer();
   DOMSource source = new DOMSource(docNew);
   StreamResult result = new StreamResult(out);
   transformer.transform(source,result);
  }
  catch (Exception e){
   System.out.println(e);
  }  
 }
}
 

Step 3: Create the jar file, import it into IR and assign it in Interface Mapping

!https://weblogs.sdn.sap.com/weblogs/images/251803663/P5.JPG|height=197|alt=Assign Java Class in Interface Mapping|width=692|src=https://weblogs.sdn.sap.com/weblogs/images/251803663/P5.JPG|border=0!

 

Step 4: *Test you Interface Mapping

!https://weblogs.sdn.sap.com/weblogs/images/251803663/P6.JPG|height=122|alt=Test your Interface mapping|width=700|src=https://weblogs.sdn.sap.com/weblogs/images/251803663/P6.JPG|border=0!

New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !