Recent project required to do a REST Lookup inside Java Mapping.
At ESR side, the input stream Need to be in XML format. The output stream in this example is XML format as well.
At ID side, Beside creating receiver REST communication channel R_REST_TargetSystem_TargetRESTAPI, need to create receiver agreement for this communication channel. If the response from REST call is JSON and Java Mapping expect XML format, then tick “Convert to XML” and “Add Wrapper Element”, at Element name and namespace, put Message Type and corresponding namespace.
Can repackage below code inside an UDF too, it will work just fine.

public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException{
	try {
		InputStream inputStream = transformationInput.getInputPayload().getInputStream();
		OutputStream outputStream = transformationOutput.getOutputPayload().getOutputStream();
		DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		javax.xml.transform.Transformer transformer = TransformerFactory.newInstance().newTransformer();
		String restCC = "";
		Document inDoc = docBuilder.parse(inputStream);
		restCC = "R_REST_TargetSystem_TargetRESTAPI";
		getTrace().addDebugMessage("restCC = " + restCC);
		//Perform the rest look up
		String access_token = "";
		String requestxml = getStringFromDoc(inDoc);
		Channel channel = LookupService.getChannel("TargetSystem",restCC);
		SystemAccessor accessor = LookupService.getSystemAccessor(channel);
		InputStream requestInputStream = new ByteArrayInputStream(requestxml.getBytes());
		Payload requestPayload = LookupService.getXmlPayload(requestInputStream);
		Payload responsePayload = accessor.call(requestPayload);
		InputStream responseInputStream = responsePayload.getContent();
		//Parse response xml returned from communication channel
		Document outDoc = docBuilder.parse(responseInputStream);
		transformer.transform(new DOMSource(outDoc), new StreamResult(outputStream));
	} catch (Exception ex) {
		getTrace().addDebugMessage(ex.getMessage());
		throw new StreamTransformationException(ex.toString());
	}
}

public String getStringFromDoc(org.w3c.dom.Document doc){
	try
	{
       DOMSource domSource = new DOMSource(doc);
       StringWriter writer = new StringWriter();
       StreamResult result = new StreamResult(writer);
       TransformerFactory tf = TransformerFactory.newInstance();
       javax.xml.transform.Transformer transformer = tf.newTransformer();
       transformer.transform(domSource, result);
       writer.flush();
       return writer.toString();
    }
    catch(TransformerException ex)
    {
       ex.printStackTrace();
       return null;
    }
}

import added:

com.sap.aii.mapping.api.*
com.sap.aii.mapping.lookup.*
com.sap.aii.mappingtool.tf7.rt.*
java.io.*
java.lang.reflect.*
java.util.*
javax.xml.parsers.*
javax.xml.transform.*
javax.xml.transform.dom.*
javax.xml.transform.stream.*
org.w3c.dom.Document
org.w3c.dom.NodeList
org.w3c.dom.Node
org.w3c.dom.Element

Reference: https://blogs.sap.com/2015/09/11/yes-rest-lookup-is-possible-in-pi

REST Lookup using Java Mapping or UDF
Tagged on:         

Leave a Reply

Your email address will not be published.