Overview

Here, we will see an example of SAP-PI Java Mapping, in which we come to know

  • How to use Dyanmic Configuration variables in Java Map
  • How to Print Trace log
  • How to refer ResourceFile data included in JavaMap.Jar files

This SAP-PI Java map was required in below business scenario:

  • One SAP-PI Soap Synchronous web-service has been hosted, where:
    • In Soap-Request, client will send FileName starting key words
    • and in Soap-Response message, SAP-PI has to return FileContents of one File found in SAP directory with same FileNameKey
    • To achieve above, we use this example of JavaMap to populate Soap Response message.
  • This Java map is been applied in Response-Tab of Operation Mapping

Functionality of given SAP-PI Java map:

  1. Get File-Name-Key from SoapRequest payload using Dynamic Configuration
    • In Request Mapping, below UDF is been used to get FileNameKey from Soap Request Dynamic Configuration variable
    • String RequestDataStr = ""; RequestDataStr = ReqStr_FileName ; try { // set to dynamic config: DynamicConfiguration conf = (DynamicConfiguration) container .getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION); DynamicConfigurationKey key = DynamicConfigurationKey.create("https://sap.com/xi/XI/System/SOAP", "FileName_StartKey" ); if ( conf != null){ conf.put(key, RequestDataStr); } } catch (Exception ex) { ; } return RequestDataStr;
  2. Get FolderPath details from Resource file ‘FolderPath_details.xml‘ which is included with Jar File itself
  3. Search directory for files having starting name as of ‘FileNameKey’ and having extension ‘.CSV’ or ‘.csv’
  4. ‘If file found as per above criteria, extract its content and append it to Target Message format
  5. And Archive read file to other directory

 

 

Pre-requisites:

  • SAP-PI 7.1
  • Java Map Library (aii_map_api.jar)
  • Java Eclipse (to create Java Mapping program)
  • Jre 1.6

 

Steps to create Java Map:

  1. Create a Java Project in Eclipse
  2. Import External jar lib file ‘aii_map_api.jar
    • Go to -> right Click on Project fodler ‘JavaMap_Example-01’ -> Bulid Path -> Configure Build Path -> Tab ‘Libraries’ -> click button ‘Add External Jars’ to import file from local desktop
  3. Insert Resource File ‘FolderPath_details.xml’
    • Insert xml file having path information
  4. Create one Java Class
    • ight Click on Project fodler ‘JavaMap_Example-01’ -> src -> New -> Class -> create a new Java class
    • and write JavaMap Source code as given below
    • import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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.Attr; import org.w3c.dom.CDATASection; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.sap.aii.mapping.api.AbstractTrace; import com.sap.aii.mapping.api.DynamicConfiguration; import com.sap.aii.mapping.api.DynamicConfigurationKey; import com.sap.aii.mapping.api.StreamTransformation; import com.sap.aii.mapping.api.StreamTransformationConstants; import com.sap.aii.mapping.api.StreamTransformationException; public class Example_01 implements StreamTransformation { private String ArchivalPath = ""; private String SourcePath = ""; private AbstractTrace trace = null; private Map param; public void setParameter(Map map){ param = map; if (param == null) { param = new HashMap(); } } public void execute(InputStream in, OutputStream out) throws StreamTransformationException{ try{ trace = (AbstractTrace) param.get(StreamTransformationConstants.MAPPING_TRACE); trace.addInfo("Starting JavaMap 'Example_01'"); //Get FileNameKey using DynamicConfig String FileNameKey = ""; FileNameKey = getFileNameKey(); trace.addInfo("FileName starting key found as: " + FileNameKey); //Get FolderPath from ResourceFile 'FolderPath_details.xml' getFolderPath(); trace.addInfo("SourcePath: " + SourcePath); trace.addInfo("ArchivalPath: " + ArchivalPath); //----Start:Read File from SAPDirectory & Append it to OutputXml ------- String FileName = ""; String FileContent = ""; String ErrorStr = ""; File theDir = new File(SourcePath); if (!theDir.exists()){ //Error message 'SourcePath not found' ErrorStr = "SourcePath '" + SourcePath + "' not found"; }else{ //If directory found, then check if it is empty File[] dirFileList = new File(SourcePath).listFiles(); if (dirFileList.length 
  5. Create Jar File of Java Map
    • right Click on Project fodler ‘JavaMap_Example-01’ -> Export -> Java -> JAR File -> Click Next -> give Jar name -> select resource -> Finish
  6. Import JavaMap Jar file to SAP -PI as a ‘Imported Archive’

New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !