cancel
Showing results for 
Search instead for 
Did you mean: 

I am facing error while to writing java code to implement rest lookup along with xml to string conve

Sreelekha
Newcomer
0 Kudos
161

Hi All,

We have a requirement like we are getting data from sap as idoc need to post this idoc to rest api using outh2.0 authentication.

To fetch token using oauth 2.0 of  grant type client credentials we need to send three inputs to token api as request in formurlencoded format(which i tried in rest channel but didn't work.

Initially wrote java code to implement token it worked but after that to simply requirement thought to write code to do restlookup and data conversion(xml to formurlencoded) in single program, in this process getting some errors please help me if i am missing something in code.

Code i am using

package Token;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
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.*;
import com.sap.aii.mapping.api.InputPayload;
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;
import org.json.*;
public class  AuthToken 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>";
  try {
  InputStream inputstream = in.getInputPayload().getInputStream();  
            String sourcexml = ""; String targetxml =""; String line="";
            BufferedReader br = new BufferedReader( new InputStreamReader(inputstream));
 
            while ((line = br.readLine()) != null) 
sourcexml +=line+"\n"; 
br.close();
//convert the xml Structure to JSON
            JSONObject xmlJSONObj = XML.toJSONObject(sourcexml);
            String jsonstring = xmlJSONObj.toString(0);
//formatting to convert string in required structure
//Required structure:transid=d&Action=d&BrowsePlanCategory=d&OperatorAlias=dd&RegionAlias=d
            
            jsonstring =jsonstring.replaceAll("\\{", "").replaceAll("\\}","");
            jsonstring=jsonstring.replace("\"", "");
            jsonstring=jsonstring.replace(":", "=");
            jsonstring=jsonstring.replace(",","&");
//If you are getting the response in correct format .you may need to do some changes here as per your //structure. 
for(int i=0;i<jsonstring.length();i++)
          {
          char value=jsonstring.charAt(i);
          if(value == '&')
          {
  jsonstring = jsonstring.substring(i+1,jsonstring.length());
          break;
          }
          }
 
            targetxml=jsonstring.trim();
            out.getOutputPayload().getOutputStream().write(targetxml.getBytes());
        } 
catch (Exception exception) {
            getTrace().addDebugMessage(exception.getMessage());
            throw new StreamTransformationException(exception.toString());
        }
  //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
}

Please help me if i am missing something, pasted error screenshot. 

Sreelekha_0-1707716148225.png

 

Accepted Solutions (0)

Answers (0)