Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Recently we had a requirement where we need to perform a look up for a rest service to obtain the token and then include the same in the future requests.

I did not find any document for REST look up in PI , so thought to create a new one so that it can help others.

For this document, I have used the below rest api

http://maps.googleapis.com/maps/api/geocode/json?address=10001


This service takes zip code as json input and returns status and result in json format.


So like other look up first thing we need to do is to create a new communication channel with type REST in ID




REST URL Tab:

Notice we have used a variable parameter {req_zipcode} in the URL.The value of this parameter will be fetched from the input xml message which we will be passing during the look up.

In the xpath expression we have used the value as //zipcode. So in the input xml there has to be a field with the name zipcode. Below is our input xml

Rest Operation tab:

Data Format tab:

Our input is xml but the service expects json so we have to choose the option 'Convert XML Payload To JSON'.

Similarly the service will return the output as JSON. So we have to choose the option 'Convert to XML'. Also we need to select the 'Add Wrapper Element'.

The final output in xml format will look like below

Communication channel is ready now. Next we need to create source and target structure in ESR

source:

target:

Java Mapping Code:


package com.test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
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;
import com.sap.aii.mapping.lookup.Channel;
import com.sap.aii.mapping.lookup.LookupService;
import com.sap.aii.mapping.lookup.Payload;
import com.sap.aii.mapping.lookup.SystemAccessor;
public class RestLookInPI extends AbstractTransformation {
  public void transform(TransformationInput arg0, TransformationOutput arg1)
  throws StreamTransformationException {
  this.execute(arg0.getInputPayload().getInputStream(), arg1
  .getOutputPayload().getOutputStream());
  }// end of transform
  public void execute(InputStream in, OutputStream out)
  throws StreamTransformationException {
  try {
  String status = "";
  // generate the input xml for rest look up
  String loginxml =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<zipcode>10001</zipcode>";
  //perform the rest look up
  Channel channel = LookupService.getChannel("BC_468470_Receiver","CC_Rest_Rcv");
  SystemAccessor accessor = null;
  accessor = LookupService.getSystemAccessor(channel);
  InputStream inputStream = new ByteArrayInputStream(loginxml.getBytes());
  Payload payload = LookupService.getXmlPayload(inputStream);
  Payload SOAPOutPayload = null;
  SOAPOutPayload = accessor.call(payload);
  InputStream inp = SOAPOutPayload.getContent();
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document document = builder.parse(inp);
  NodeList stats = document.getElementsByTagName("status");
  Node node = stats.item(0);
  if (node != null)
  {
  node = node.getFirstChild();
  if (node != null)
  {
  status = node.getNodeValue();
  }
  }
  Document targetDoc = builder.newDocument();
  Element targetRoot = (Element) targetDoc.createElement("ns0:MT_Output");
  targetRoot.setAttribute("xmlns:ns0","http://xxxxxxx/pi/468470/RestLookUp");
  Element stat = (Element) targetDoc.createElement("status");
  stat.setTextContent(status);
  targetRoot.appendChild(stat);
  targetDoc.appendChild(targetRoot);
   DOMSource domSource = new DOMSource(targetDoc);
           StreamResult result = new StreamResult(out);
           TransformerFactory tf = TransformerFactory.newInstance();
           Transformer transformer = tf.newTransformer();
           transformer.transform(domSource, result);
  } catch (Exception e) {
  e.printStackTrace();
  }
  } // end of execute
}

Test Result:

22 Comments
Labels in this area