Friday 9 April 2021

Calling REST API to POST input parameters Data Using JAVA Embedding in BPEL

Hi , 

I came up with a requirement where I need to send Highlighted Parameters Data to below API. 

REST APIURI : 
https://api-abc.io/OrderService.asmx/SubmitNewOrdersWithPaymentAdvanced?ClientName=

When we assign the xml payload to String type XMLPayloadData variable , we use ora:getContentAsString($XMLPayload)   ---to-> XMLPayloadData   in Assign Activity



JAVA Embedding code : 

try {      
addAuditTrailEntry("JAVA activity Started");      
String lvar_ClientName = (String)getVariableData("ClientName");           
String lvar_GUID = (String)getVariableData("guid");           
String lvar_InboundXML = (String)getVariableData("XMLPayloadData");    
String lvar_ProcessingOptions = (String)getVariableData("ProcessingOptions");       
String lvar_EOMURL=(String)getVariableData("APIURI");     
addAuditTrailEntry("APIURI: " + lvar_EOMURL); 
     
 addAuditTrailEntry("JAVA activity Started  ---  1...");     
URL url = new URL(lvar_EOMURL);         
Map<String, Object> params = new LinkedHashMap<>();         
params.put("ClientName", lvar_ClientName);         
params.put("GUID", lvar_GUID);         
params.put("InboundXML",lvar_InboundXML);         
params.put("ProcessingOptions",  lvar_ProcessingOptions);         
 addAuditTrailEntry("JAVA activity Started  ---  2...");     
                addAuditTrailEntry(lvar_InboundXML);    
StringBuilder postData = new StringBuilder();         
for (Map.Entry<String, Object> param : params.entrySet()) {         
if (postData.length() != 0)         
postData.append('&');         
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));         
postData.append('=');         
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));         
}         
addAuditTrailEntry(postData.toString());    
addAuditTrailEntry("JAVA activity Started  ---  3...");     
byte[] postDataBytes = postData.toString().getBytes("UTF-8");         
     
HttpURLConnection conn = (HttpURLConnection) url.openConnection();         
conn.setRequestMethod("POST");         
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");         
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));         
conn.setDoOutput(true);         
conn.getOutputStream().write(postDataBytes);         
     
//Reader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));         
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));         
StringBuilder sb = new StringBuilder();         
String line;         
//for (int c; (c = in.read()) >= 0;)         
//System.out.print((char) c);         
while ((line = rd.readLine()) != null) {         
sb.append(line);         
}         
rd.close();         
addAuditTrailEntry(sb.toString());     
                setVariableData("APIResponse",sb.toString());   
 
} catch (MalformedURLException me) {         
addAuditTrailEntry("MalformedURLException " + me.getMessage());         
} catch (ProtocolException pe) {         
addAuditTrailEntry("ProtocolException " + pe.getMessage());         
} catch (IOException ioe) {         
addAuditTrailEntry("IOException" + ioe.getMessage());         
} catch (Exception e) {         
addAuditTrailEntry("We got an Exception " + e.getMessage());         
}



At last Do below in assign Activity -- APIResponse Type String

oraext:parseXML($APIResponse)   -- to -> $ResponseXML  Type Element

You will get the API response on $ResponseXML  Type Element of API response XSD


Task is done!
Cheers!!

No comments:

Post a Comment

String to QR Code Image Generator Using Java

 Hi , Hope You are doing well. I came up with the requirement, where I need to generate QR code Image file for the input String. package dem...