Skip to Content

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

Actual Payload Before Encoding

 

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 http://ostermiller.org/calc/encode.html  (http://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. <Details> node contains the actual encoded Information/XML payload. </p><p>!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!</p><p>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.</p><p> </p><p>package domDemo;</p><p>import java.io.ByteArrayInputStream;<br />import java.io.InputStream;<br />import java.io.OutputStream;<br />import java.util.Map;</p><p>import javax.xml.parsers.DocumentBuilder;<br />import javax.xml.parsers.DocumentBuilderFactory;<br />import javax.xml.transform.Transformer;<br />import javax.xml.transform.TransformerFactory;<br />import javax.xml.transform.dom.DOMSource;<br />import javax.xml.transform.stream.StreamResult;</p><p>import org.w3c.dom.Document;<br />import org.w3c.dom.NodeList;</p><p>import sun.misc.BASE64Decoder;<br />import com.sap.aii.mapping.api.StreamTransformation;</p><p>public class JMDecodeDetails implements StreamTransformation {<br /> private Map param=null;<br /> public void setParameter(Map param){<br />  this.param=param;<br /> }<br /> public void execute(InputStream in, OutputStream out){<br />  try{<br />      <br />   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();<br />   DocumentBuilder builder = factory.newDocumentBuilder();<br />   Document docOld = builder.parse(in);<br />   //Get the node name Details that contains the Based64 Encoded information.. <br />   NodeList details = docOld.getElementsByTagName(“Details”);<br />   //Get the Text of Node.<br />   String data = details.item(0).getChildNodes().item(0).getNodeValue();<br />   //Instantiate an object of BASE64Decoder…<br />   BASE64Decoder decoder =  new BASE64Decoder();<br />   //Decode the encoded information….<br />   byte b[] = decoder.decodeBuffer(data);<br />   //Parse the decoded data i.e. nothing but the real payload….<br />   Document docNew = builder.parse(new ByteArrayInputStream(b));<br />   <br />   TransformerFactory transformerFactory = TransformerFactory.newInstance();<br />   Transformer transformer =  transformerFactory.newTransformer();<br />   DOMSource source = new DOMSource(docNew);<br />   StreamResult result = new StreamResult(out);<br />   transformer.transform(source,result);<br />  }<br />  catch (Exception e){<br />   System.out.println(e);<br />  }  <br /> }<br />}<br /> </p><p>Step 3: Create the jar file, import it into IR and assign it in Interface Mapping</p><p>!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!</p><p> </p><p>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!</body>

To report this post you need to login first.

13 Comments

You must be Logged on to comment or reply to a post.

  1. Daniel Graversen
    Hi,

    Why do you convert the Base64 document to XML in the file adapter. If you place a java mapping you would be able to convert the docment directly to the target XML document.
    The best solution would probably be to convert the Base64 in an adapter module.

    (0) 
    1. Farooq Farooqui Post author
      Hi Daniel,

      If I directly send Encoded data i.e. series of character’s directly to the IE then how it will execute the JM, because the incoming Message type will not fit for it…so I create first Dummy MT and send XML from Adapter to IE.

      Right, AM is the best way to handle it. but as I mentioned in the both JM and Custom AM has there advantages and disadvantages…

      Thanks
      Farooq

      (0) 
      1. JaiShankar Ramakrishnan
        >>because the incoming Message type will not fit for it…

        No. In case of Java mappings the incoming/outgoing messages are not validated against the Message types. So you can pass the whole encoded message and let the Java mapping handle it.

        Thanks,
        Jaishankar

        (0) 
    1. Farooq Farooqui Post author
      Hi Amol,

      As I mentioned in the Blog we can use others API also…I took sun.misc.BASE64Decoder just to depicts the scenario…we can achieve the same with com.sap.aii.utilxi.base64.api.Base64 also.

      Thanks
      Farooq

      (0) 
    2. Farooq Farooqui Post author
      Hi Amol,

      As I mentioned in the Blog we can use others API also…I took sun.misc.BASE64Decoder just to depicts the scenario…we can achieve the same with com.sap.aii.utilxi.base64.api.Base64 also.

      Thanks
      Farooq

      (0) 
  2. Rodel Nanca
    If I’m not mistaken, the Java Mapping approach will only work if there are no receiver determination conditions set as it needs to inspect the payload to determine appropriate receivers. In this respect, Adapter Module development is the ONLY option as the payload will be decrypted already when is goes into the Receiver Determination stage.
    (0) 
      1. Henrique Pinto

        Three years later, I’ve noticed my message above doesn’t make any sense in the context and that Rodel’s message was completely right. My apologies.

        (0) 
  3. Jorge Garcia

    Hi Everybody!

    I’m testing the code, But I have problems right in this line:

    Document docNew = builder.parse (new ByteArrayInputStream(b));

    It looks like if can’t put the binaty table in the new document. Can anyone helpme?

    Thanks a lot!!

    (0) 

Leave a Reply