Groovy Script
Copy
import com.apinizer.common.util.*
try{
// clear table
def get = new URL("http://192.168.100.100:30080/apigateway/personel/temizle").openConnection()
get.setRequestMethod("GET")
get.setDoOutput(true)
get.setRequestProperty("Content-Type", "application/json")
get.setRequestProperty("Username", "")
get.setRequestProperty("Password", "")
def getRC = get.getResponseCode()
if(getRC.equals(400)|| getRC.equals(401)) {
responseErrorMessageToClient="Yetkilendirme Hatası!"
}
}catch(Exception e){
}
int itemCount = responseBodyTextToClient.count("<item>")
String templateJson = "{" +
"\"sicil_no\":\"#sicil_no\", " +
"\"adi\" :\"#adi\"," +
"\"soyadi\" :\"#soyadi\"," +
"\"isyeri_kodu\" :\"#isyeri_kodu\"," +
"\"calistigi_isyeri\" :\"#calistigi_isyeri\"," +
"\"ik_bilgileri\" :\"#ik_bilgileri\"," +
"\"calistigi_birim\" :\"#calistigi_birim\"," +
"\"unvani\" :\"#unvani\"," +
"\"gorevi\" :\"gorevi\"," +
"\"dogum_gunu\" :\"#dogum_gunu\"," +
"\"ise_giris_tarihi\" :\"#ise_giris_tarihi\"," +
"\"yonetici_adi\" :\"#yonetici_adi\"," +
"\"yonetici_soyadi\" :\"#yonetici_soyadi\"," +
"\"yonetici_sicil\" :\"#yonetici_sicil\"," +
"\"eposta\" :\"#eposta\"," +
"\"telefon\" :\"#telefon\"," +
"\"tckimlikno\":\"#tckimlikno\"" +
"}"
String hataliIdStr=""
for(int i=1; i < itemCount+1; i++){
String valueJson = templateJson
String xpathId = "(/ArrayList/item)[" +i+ "]/id"
String valueId = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpathId, true)
try{
String xpath = "(/ArrayList/item)[" +i+ "]/secondaryNr"
String value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#sicil_no", value )
xpath = "(/ArrayList/item)[" +i+ "]/name"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#adi", value)
xpath = "(/ArrayList/item)[" +i+ "]/surname"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#soyadi", value)
xpath = "(/ArrayList/item)[" +i+ "]/bizUnit/code"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#isyeri_kodu", value)
xpath = "(/ArrayList/item)[" +i+ "]/bizUnit/description"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#calistigi_isyeri", value)
xpath = "(/ArrayList/item)[" +i+ "]/orgUnit/code"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#ik_bilgileri", value)
xpath = "(/ArrayList/item)[" +i+ "]/orgUnit/description"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#calistigi_birim", value)
xpath = "(/ArrayList/item)[" +i+ "]/profession"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#unvani", value)
xpath = "(/ArrayList/item)[" +i+ "]/jobTitle"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#gorevi", value)
xpath = "(/ArrayList/item)[" +i+ "]/birthday"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
if(org.apache.commons.lang3.StringUtils.isNotBlank(value) && value.length()>10){
value=value.substring(0,10)
}
valueJson = valueJson.replace("#dogum_gunu", value)
xpath = "(/ArrayList/item)[" +i+ "]/entryDate"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
if(org.apache.commons.lang3.StringUtils.isNotBlank(value) && value.length()>10){
value=value.substring(0,10)
}
valueJson = valueJson.replace("#ise_giris_tarihi", value)
xpath = "(/ArrayList/item)[" +i+ "]/supervisor/name"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#yonetici_adi", value)
xpath = "(/ArrayList/item)[" +i+ "]/supervisor/surname"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#yonetici_soyadi", value)
xpath = "(/ArrayList/item)[" +i+ "]/supervisor/secondaryNr"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#yonetici_sicil", value)
xpath = "(/ArrayList/item)[" +i+ "]/contactInfo/email"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#eposta", value)
xpath = "(/ArrayList/item)[" +i+ "]/contactInfo/mobile1"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#telefon", value)
xpath = "(/ArrayList/item)[" +i+ "]/officialId"
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true)
valueJson = valueJson.replace("#tckimlikno", value)
// POST to DB
def post = new URL("http://192.168.100.100:30080/apigateway/personel/kaydet").openConnection()
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")
post.setRequestProperty("Username", "")
post.setRequestProperty("Password", "")
post.getOutputStream().write(valueJson.getBytes("UTF-8"))
def postRC = post.getResponseCode()
if(postRC.equals(400)|| postRC.equals(401)) {
responseErrorMessageToClient="Yetkilendirme Hatası!"
break
}
if(!postRC.equals(200)) {
hataliIdStr=valueId+","+hataliIdStr
}
}catch(Exception e){
hataliIdStr=valueId+","+hataliIdStr
}
}
if(hataliIdStr.endsWith(",")){
hataliIdStr = hataliIdStr.substring(0,hataliIdStr.length() - 1)
}
if(org.apache.commons.lang3.StringUtils.isNotBlank(hataliIdStr)){
responseErrorMessageToClient=hataliIdStr+" id'li kayıtlar kaydedilemedi!"
}else{
responseBodyTextToClient="<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"<soap:Body>" +
" <result>OK</result>" +
"</soap:Body>" +
"</soap:Envelope>"
}
Explanation
This script performs the following operations:- XML Parse Operation: Counts
<item>elements in the response - Data Extraction: Extracts data for each item using XPath
- JSON Creation: Places extracted data into JSON template
- API Call: Sends the created JSON to another API with POST request
- Error Management: Creates error message in case of error, returns SOAP response in case of success
This script should be run on the response line (Response Policy) because it uses the
responseBodyTextFromTargetAPI and responseBodyTextToClient variables.
