Showing posts with label JAVA EMBEDDING. Show all posts
Showing posts with label JAVA EMBEDDING. Show all posts

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!!

Removing Namespaces from XML using JAVA Embedding



Hi,


Recently I came up with a requirement where I need to remove xmlns="" from below XML. So I did it using JAVA Embedding in BPEL.

Sample XML :

<?xml version="1.0" encoding="UTF-8"?>
<stores xmlns="http://www.demandware.com/xml/impex/store/2007-04-30">
<store store-id="R00591"  xmlns="">
<name>Irvine, CA</name>
<address1>YCC - IRVINE, CA #0591</address1>
<address2>735 SPECTRUM CENTER DRIVE</address2>
<city>IRVINE</city>
<postal-code>92618</postal-code>
<state-code>CA</state-code>
<country-code>US</country-code>
<phone>949-450-0725</phone>
<store-hours>Sunday 11:00 - 19:00|Monday 11:00 - 19:00|Tuesday 11:00 - 19:00|Wednesday 11:00 - 19:00|Thursday 11:00 - 19:00|Friday 11:00 - 20:00|Saturday 11:00 - 20:00</store-hours>
<latitude>33.6490286</latitude>
<longitude>-117.7449241</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/california/irvine-ca</custom-attribute>
</custom-attributes>
</store>
<store store-id="R00001"  xmlns="">
<name>Deerfield, MA</name>
<address1>YCC -SOUTH DEERFIELD, MA #0001</address1>
<address2>25 GREENFIELD ROAD</address2>
<city>SOUTH DEERFIELD</city>
<postal-code>01373</postal-code>
<state-code>MA</state-code>
<country-code>US</country-code>
<phone>413-665-2929</phone>
<store-hours>Sunday 10:00 - 18:00|Monday 10:00 - 18:00|Tuesday 10:00 - 18:00|Wednesday 10:00 - 18:00|Thursday 10:00 - 18:00|Friday 10:00 - 18:00|Saturday 10:00 - 18:00</store-hours>
<latitude>42.4744</latitude>
<longitude>-72.614073</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/massachusetts/south-deerfield-ma</custom-attribute>
</custom-attributes>
</store>
</stores>

We can remove  xmlns="" from payload using JAVA Embedding activity.ust follow below steps- 

Step 1. Assign "InvokeSFCCArchive_Write_InputVariable.body" Element type variable data as String using ora:getContentAsString() function to InputVar String type variable 

Example-

ora:getContentAsString($InvokeSFCCArchive_Write_InputVariable.body)   to $InputVar Type String


Step 2. Add below code to Java Embedding activity-

try{  

addAuditTrailEntry("JAVA activity Started");        

String inputXml = (String)getVariableData("InputVar");    

String input=inputXml.replace("xmlns=\"\"", "");

addAuditTrailEntry("String after Removing of xmlns  : "+input);      

setVariableData("tempXML",input.toString());  // Assigning processed xml to tempXML String Var

addAuditTrailEntry("inputtoString  : "+input.toString()); 

}   catch (Exception e) {           

addAuditTrailEntry("We got an Exception " + e.getMessage());           

}

Step 3. parseXML from tempXML to Element Type variable in Assign Activity

oraext:parseXML($tempXML)  to $InvokeSFCCArchive_Write_InputVariable.body


Processed XML :

<?xml version="1.0" encoding="UTF-8"?>
<stores xmlns="http://www.demandware.com/xml/impex/store/2007-04-30">
<store store-id="R00591">     <!-- No xmlns=""  -->
<name>Irvine, CA</name>
<address1>YCC - IRVINE, CA #0591</address1>
<address2>735 SPECTRUM CENTER DRIVE</address2>
<city>IRVINE</city>
<postal-code>92618</postal-code>
<state-code>CA</state-code>
<country-code>US</country-code>
<phone>949-450-0725</phone>
<store-hours>Sunday 11:00 - 19:00|Monday 11:00 - 19:00|Tuesday 11:00 - 19:00|Wednesday 11:00 - 19:00|Thursday 11:00 - 19:00|Friday 11:00 - 20:00|Saturday 11:00 - 20:00</store-hours>
<latitude>33.6490286</latitude>
<longitude>-117.7449241</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/california/irvine-ca</custom-attribute>
</custom-attributes>
</store>
<store store-id="R00001">  <!-- No xmlns=""  -->
<name>Deerfield, MA</name>
<address1>YCC -SOUTH DEERFIELD, MA #0001</address1>
<address2>25 GREENFIELD ROAD</address2>
<city>SOUTH DEERFIELD</city>
<postal-code>01373</postal-code>
<state-code>MA</state-code>
<country-code>US</country-code>
<phone>413-665-2929</phone>
<store-hours>Sunday 10:00 - 18:00|Monday 10:00 - 18:00|Tuesday 10:00 - 18:00|Wednesday 10:00 - 18:00|Thursday 10:00 - 18:00|Friday 10:00 - 18:00|Saturday 10:00 - 18:00</store-hours>
<latitude>42.4744</latitude>
<longitude>-72.614073</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/massachusetts/south-deerfield-ma</custom-attribute>
</custom-attributes>
</store>
</stores>


Your task is done!

Enjoy, Cheers !!


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...